AprilTag Pose Estimation
This tutorial fuses Limelight MegaTag2 vision measurements into a WPILib pose estimator, the way DrivebaseSubsystem does in YALL's example project.
1. Set up the camera offset once
MegaTag needs to know where the camera sits relative to robot center. Do this at construction time:
Pose3d cameraOffset = new Pose3d(Inches.of(5).in(Meters),
Inches.of(5).in(Meters),
Inches.of(5).in(Meters),
Rotation3d.kZero);
limelight = new Limelight("limelight");
limelight.getSettings()
.withLimelightLEDMode(LEDMode.PipelineControl)
.withCameraOffset(cameraOffset)
.save();
poseEstimator = limelight.createPoseEstimator(EstimationMode.MEGATAG2);2. Submit robot orientation every loop
MegaTag2 fuses AprilTag detections with your robot's current heading. This only works if you tell the Limelight your heading every periodic loop, before reading a pose estimate:
3. Read and filter the pose estimate
poseEstimator above is your WPILib SwerveDrivePoseEstimator / DifferentialDrivePoseEstimator, a different object than YALL's LimelightPoseEstimator. Filtering on tag count, distance, and ambiguity before calling addVisionMeasurement keeps a single noisy or distant reading from corrupting your odometry.
Alternative: the BotPose enum directly
If you don't need a LimelightPoseEstimator instance around, BotPose gives the same data directly:
Or fetch the alliance-relative estimate without checking DriverStation yourself:
Next steps
Pose Estimation & Ambiguity: what each
PoseEstimatefield means and how to tune your filter thresholds.How do I mount a Limelight on a moving mechanism?: updating
withCameraOffsetdynamically for a turret.Simulating a Limelight: exercise this fusion code in desktop simulation without hardware.
AprilTag & MegaTag2 Localization Tuning: IMU modes, assist alpha, and the setup mistakes that silently break MegaTag2.
ChArUco Camera Calibration: improves the accuracy of every pose this tutorial produces.
Last updated
Was this helpful?