Search in sources :

Example 1 with RPLidarA1

use of com.spartronics4915.lib.hardware.sensors.RPLidarA1 in project 2022-RapidReact by Spartronics4915.

the class TestObjectFinder method interactivePointcloudTest.

@Tag("hardwareDependant")
@Test
public void interactivePointcloudTest() {
    final ObjectFinder finder = new ObjectFinder(0.01);
    final double circleRadiusMeters = Units.inchesToMeters(3.5);
    var pointCloudCanvas = new Canvas() {

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            try {
                synchronized (mPointcloud) {
                    if (mPointcloud.size() <= 0) {
                        return;
                    }
                    mPointcloud.forEach((p) -> drawPoint(p, 255, g));
                    int circleDiameterCentimeters = (int) Math.round(circleRadiusMeters * 100.0 * 2.0);
                    // var centers = finder.findSquares(pointcloud, new Translation2d(), 0.28, mNumVotesNeeded, 3, 0.3);
                    var centers = finder.findCircles(mPointcloud, circleRadiusMeters, mNumVotesNeeded, 3);
                    var center = mTargetTracker.update(centers);
                    if (center == null) {
                        System.out.println("No target found");
                        return;
                    }
                    // for (Translation2d center : centers) {
                    System.out.println(center);
                    g.setColor(Color.BLUE);
                    g.drawOval(toScreenCoordinates(center.getX() - circleRadiusMeters, true), toScreenCoordinates(center.getY() - circleRadiusMeters, false), circleDiameterCentimeters, circleDiameterCentimeters);
                    // break;
                    // }
                    g.drawString("Edge: " + mEdgeDetectorValue + ", Votes: " + mNumVotesNeeded, 10, 10);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // Screen coordinates correspond to centimeters
        private int toScreenCoordinates(double coordMeters, boolean isX) {
            Dimension size = this.getSize();
            coordMeters *= 100;
            coordMeters += (isX ? size.getWidth() : size.getHeight()) / 2;
            return (int) Math.round(coordMeters);
        }

        private void drawPoint(Translation2d point, int quality, Graphics g) {
            g.setColor(new Color(255 - quality, 0, 0));
            g.drawOval(toScreenCoordinates(point.getX(), true), toScreenCoordinates(point.getY(), false), 1, 1);
        }
    };
    pointCloudCanvas.setSize(6000, 6000);
    pointCloudCanvas.setBackground(Color.WHITE);
    var frame = new Frame();
    frame.add(pointCloudCanvas);
    frame.setSize(6000, 6000);
    frame.setVisible(true);
    frame.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent k) {
        }

        @Override
        public void keyReleased(KeyEvent k) {
            System.out.println(k.getKeyChar());
            if (k.getKeyChar() == '-' || k.getKeyChar() == '+') {
                double toAdd = (k.getKeyChar() == '+' ? 1 : -1);
                if (k.isAltDown()) {
                    mEdgeDetectorValue += toAdd;
                } else if (k.isControlDown()) {
                    mNumVotesNeeded += toAdd;
                }
            } else if (k.getKeyChar() == 'q') {
                System.exit(0);
            }
        }

        @Override
        public void keyTyped(KeyEvent k) {
        }
    });
    RPLidarA1 lidar = new RPLidarA1();
    lidar.setCallback((List<Translation2d> pointcloud) -> {
        synchronized (mPointcloud) {
            if (System.currentTimeMillis() - mLastResetTime > 0) {
                mPointcloud = pointcloud;
                mLastResetTime = System.currentTimeMillis();
            } else {
                mPointcloud.addAll(pointcloud);
            }
        }
    }, new RobotStateMap(), new Transform2d(new Translation2d(Units.inchesToMeters(-5.5), Units.inchesToMeters(-14)), Rotation2d.fromDegrees(180)));
    lidar.start();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        lidar.stop();
        System.out.println("Graceful shutdown complete");
    }));
    while (true) {
        try {
            Thread.sleep(500);
            synchronized (mPointcloud) {
                pointCloudCanvas.repaint();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Frame(java.awt.Frame) RobotStateMap(com.spartronics4915.lib.subsystems.estimator.RobotStateMap) Transform2d(edu.wpi.first.math.geometry.Transform2d) Canvas(java.awt.Canvas) Color(java.awt.Color) Dimension(java.awt.Dimension) Graphics(java.awt.Graphics) KeyEvent(java.awt.event.KeyEvent) Translation2d(edu.wpi.first.math.geometry.Translation2d) RPLidarA1(com.spartronics4915.lib.hardware.sensors.RPLidarA1) KeyListener(java.awt.event.KeyListener) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Aggregations

RPLidarA1 (com.spartronics4915.lib.hardware.sensors.RPLidarA1)1 RobotStateMap (com.spartronics4915.lib.subsystems.estimator.RobotStateMap)1 Transform2d (edu.wpi.first.math.geometry.Transform2d)1 Translation2d (edu.wpi.first.math.geometry.Translation2d)1 Canvas (java.awt.Canvas)1 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 Frame (java.awt.Frame)1 Graphics (java.awt.Graphics)1 KeyEvent (java.awt.event.KeyEvent)1 KeyListener (java.awt.event.KeyListener)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Tag (org.junit.jupiter.api.Tag)1 Test (org.junit.jupiter.api.Test)1