Search in sources :

Example 6 with Transform2d

use of edu.wpi.first.math.geometry.Transform2d in project Robot-2022 by frc6995.

the class LimelightS method periodic.

/**
 * Adds the latest PhotonVision result to the filters.
 */
@Override
public void periodic() {
    // This method will be called once per scheduler run
    PhotonTrackedTarget target = limelight.getLatestResult().hasTargets() ? limelight.getLatestResult().getBestTarget() : new PhotonTrackedTarget(0, 0, 0, 0, new Transform2d(), new ArrayList<TargetCorner>());
    this.filterValues = new FilterValues(xOffsetFilter.calculate(target.getYaw()), yOffsetFilter.calculate(target.getPitch()));
    // TODO Oblog
    SmartDashboard.putNumber("x offset", this.filterValues.getFilteredXOffset());
    SmartDashboard.putNumber("y offset", this.filterValues.getFilteredYOffset());
}
Also used : PhotonTrackedTarget(org.photonvision.targeting.PhotonTrackedTarget) Transform2d(edu.wpi.first.math.geometry.Transform2d) ArrayList(java.util.ArrayList)

Example 7 with Transform2d

use of edu.wpi.first.math.geometry.Transform2d in project SnobotSimV2 by snobotsim.

the class BaseCameraSimulatorTest method doubleTargetTest.

@Test
public void doubleTargetTest() {
    List<TargetLocation> targets = new ArrayList<>();
    targets.add(new TargetLocation(new Pose2d(10, 10, Rotation2d.fromDegrees(0))));
    targets.add(new TargetLocation(new Pose2d(10, 20, Rotation2d.fromDegrees(0))));
    BaseCameraSimulator sim = new BaseCameraSimulator(targets, new Transform2d(), 50, Double.MAX_VALUE);
    TreeMap<CameraToTargetDelta, TargetLocation> visibleTargets;
    Map.Entry<CameraToTargetDelta, TargetLocation> firstEntry;
    Map.Entry<CameraToTargetDelta, TargetLocation> secondEntry;
    // Start at origin. Should only see target 1
    visibleTargets = sim.getValidTargets(new Pose2d(0, 0, Rotation2d.fromDegrees(0)));
    firstEntry = visibleTargets.firstEntry();
    assertEquals(1, visibleTargets.size());
    assertEquals(Math.sqrt(200), firstEntry.getKey().mDistance);
    assertEquals(10, firstEntry.getValue().getPosition().getY());
    // In front of first target, should see both
    visibleTargets = sim.getValidTargets(new Pose2d(0, 10, Rotation2d.fromDegrees(0)));
    firstEntry = visibleTargets.firstEntry();
    secondEntry = visibleTargets.lastEntry();
    assertEquals(2, visibleTargets.size());
    assertEquals(Math.sqrt(100), firstEntry.getKey().mDistance);
    assertEquals(10, firstEntry.getValue().getPosition().getY());
    assertEquals(Math.sqrt(200), secondEntry.getKey().mDistance);
    assertEquals(20, secondEntry.getValue().getPosition().getY());
    // In front of Second target, should see both
    visibleTargets = sim.getValidTargets(new Pose2d(0, 20, Rotation2d.fromDegrees(0)));
    firstEntry = visibleTargets.firstEntry();
    secondEntry = visibleTargets.lastEntry();
    assertEquals(2, visibleTargets.size());
    assertEquals(Math.sqrt(100), firstEntry.getKey().mDistance);
    assertEquals(20, firstEntry.getValue().getPosition().getY());
    assertEquals(Math.sqrt(200), secondEntry.getKey().mDistance);
    assertEquals(10, secondEntry.getValue().getPosition().getY());
}
Also used : Transform2d(edu.wpi.first.math.geometry.Transform2d) Pose2d(edu.wpi.first.math.geometry.Pose2d) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 8 with Transform2d

use of edu.wpi.first.math.geometry.Transform2d in project SnobotSimV2 by snobotsim.

the class TargetLocation method isInVisiblePosition.

public CameraToTargetDelta isInVisiblePosition(Pose2d robotPose, double horizontalFov, double maxDistance) {
    // Alias for readability
    double robotX = robotPose.getX();
    double robotY = robotPose.getY();
    if (robotX > mVisibleMaxX || robotX < mVisibleMinX) {
        return null;
    }
    if (robotY > mVisibleMaxY || robotY < mVisibleMinY) {
        return null;
    }
    double dx = robotPose.getX() - mPosition.getX();
    double dy = robotPose.getY() - mPosition.getY();
    double angleDelta = 0;
    if (mPosition.getRotation() != null) {
        Transform2d diff = mPosition.minus(robotPose);
        Translation2d trans = diff.getTranslation();
        Rotation2d angleFromRobot = new Rotation2d(trans.getX(), trans.getY());
        angleDelta = angleFromRobot.getDegrees();
        if (Math.abs(angleDelta) > horizontalFov) {
            return null;
        }
    }
    double distance = Math.sqrt(dx * dx + dy * dy);
    if (distance > maxDistance) {
        return null;
    }
    return new CameraToTargetDelta(distance, angleDelta);
}
Also used : Transform2d(edu.wpi.first.math.geometry.Transform2d) Translation2d(edu.wpi.first.math.geometry.Translation2d) Rotation2d(edu.wpi.first.math.geometry.Rotation2d)

Example 9 with Transform2d

use of edu.wpi.first.math.geometry.Transform2d in project SnobotSimV2 by snobotsim.

the class LimelightSimulatorTest method bestTargetTest.

@SuppressWarnings("PMD.ShortVariable")
@Test
public void bestTargetTest() {
    List<TargetLocation> targets = new ArrayList<>();
    targets.add(new TargetLocation(new Pose2d(10, 10, Rotation2d.fromDegrees(0))));
    targets.add(new TargetLocation(new Pose2d(10, 20, Rotation2d.fromDegrees(0))));
    try (NetworkTableInstance testInstance = NetworkTableInstance.create()) {
        NetworkTable networkTable = testInstance.getTable("limelight");
        NetworkTableEntry visible = networkTable.getEntry("tv");
        NetworkTableEntry tx = networkTable.getEntry("tx");
        NetworkTableEntry ty = networkTable.getEntry("ty");
        LimelightSimulator sim = new LimelightSimulator(targets, new Transform2d(), Units.feetToMeters(10), Double.MAX_VALUE, networkTable);
        testInstance.flush();
        assertEquals(0.0, visible.getNumber(-1));
        // Out of FOV
        sim.update(new Pose2d(0, 0, Rotation2d.fromDegrees(0)));
        testInstance.flush();
        assertEquals(0.0, visible.getNumber(-1));
        sim.update(new Pose2d(0, 0, Rotation2d.fromDegrees(40)));
        testInstance.flush();
        assertEquals(1.0, visible.getNumber(-1));
        assertEquals(-5.0, tx.getNumber(-100).doubleValue(), 1e-6);
        assertEquals(12.162_69, ty.getNumber(-100).doubleValue(), 1e-3);
        sim.update(new Pose2d(0, 10, Rotation2d.fromDegrees(0)));
        testInstance.flush();
        assertEquals(1.0, visible.getNumber(-1));
        assertEquals(0.0, tx.getNumber(-100).doubleValue(), 1e-6);
        assertEquals(16.951_22, ty.getNumber(-100).doubleValue(), 1e-3);
    }
}
Also used : NetworkTableInstance(edu.wpi.first.networktables.NetworkTableInstance) Transform2d(edu.wpi.first.math.geometry.Transform2d) Pose2d(edu.wpi.first.math.geometry.Pose2d) ArrayList(java.util.ArrayList) NetworkTableEntry(edu.wpi.first.networktables.NetworkTableEntry) NetworkTable(edu.wpi.first.networktables.NetworkTable) Test(org.junit.jupiter.api.Test)

Example 10 with Transform2d

use of edu.wpi.first.math.geometry.Transform2d in project 2022-Rapid-React by Manchester-Central.

the class SwerveDriveModule method updatePosition.

public void updatePosition(Pose2d robotPose) {
    Pose2d modulePose = robotPose.transformBy(new Transform2d(m_location, Rotation2d.fromDegrees(m_targetAngle)));
    m_field.setRobotPose(modulePose);
}
Also used : Transform2d(edu.wpi.first.math.geometry.Transform2d) Pose2d(edu.wpi.first.math.geometry.Pose2d)

Aggregations

Transform2d (edu.wpi.first.math.geometry.Transform2d)15 Pose2d (edu.wpi.first.math.geometry.Pose2d)9 Translation2d (edu.wpi.first.math.geometry.Translation2d)6 ArrayList (java.util.ArrayList)6 Rotation2d (edu.wpi.first.math.geometry.Rotation2d)5 Test (org.junit.jupiter.api.Test)4 ChassisSpeeds (edu.wpi.first.math.kinematics.ChassisSpeeds)2 RPLidarA1 (com.spartronics4915.lib.hardware.sensors.RPLidarA1)1 RobotStateMap (com.spartronics4915.lib.subsystems.estimator.RobotStateMap)1 MatBuilder (edu.wpi.first.math.MatBuilder)1 Twist2d (edu.wpi.first.math.geometry.Twist2d)1 DifferentialDriveKinematics (edu.wpi.first.math.kinematics.DifferentialDriveKinematics)1 N1 (edu.wpi.first.math.numbers.N1)1 N3 (edu.wpi.first.math.numbers.N3)1 TrajectoryConfig (edu.wpi.first.math.trajectory.TrajectoryConfig)1 NetworkTable (edu.wpi.first.networktables.NetworkTable)1 NetworkTableEntry (edu.wpi.first.networktables.NetworkTableEntry)1 NetworkTableInstance (edu.wpi.first.networktables.NetworkTableInstance)1 CameraPosition (frc.robot.VisionConstants.CameraPosition)1 TimestampedTranslation2d (frc.robot.subsystems.vision.Vision.TimestampedTranslation2d)1