Wheel odometry drifts over time due to wheel slip, carpet grain resistance, and collisions with other robots. AprilTag vision localization provides absolute, drift-free field measurements that anchor the robot’s coordinate frame throughout a match.
How 3D AprilTag Localization Works
Section titled “How 3D AprilTag Localization Works”Each AprilTag on the competition field has a precisely measured, 3D field coordinate (X, Y, Z, roll, pitch, yaw). When an onboard camera (Limelight or PhotonVision) detects the 4 corners of an AprilTag, it solves a mathematical Perspective-n-Point (PnP) equation to compute the camera’s exact 3D position relative to the tag.
MegaTag1 vs. MegaTag2 (Limelight)
Section titled “MegaTag1 vs. MegaTag2 (Limelight)”MegaTag1 (Classic Multi-Tag PnP)
Section titled “MegaTag1 (Classic Multi-Tag PnP)”- Solves 3D robot pose using all visible tags simultaneously.
- When multiple tags are visible, ambiguity is mathematically eliminated.
- When only a single tag is visible, optical tilt ambiguity can produce slight rotational noise.
MegaTag2 (Gyro-Constrained Localization)
Section titled “MegaTag2 (Gyro-Constrained Localization)”- Ingests the robot’s high-precision IMU yaw angle into the PnP solver as an absolute known constraint.
- Eliminates single-tag ambiguity and remains remarkably stable even when the robot is pitching over obstacles or defending aggressively.
Fusing Vision with SwerveDrivePoseEstimator
Section titled “Fusing Vision with SwerveDrivePoseEstimator”WPILib’s SwerveDrivePoseEstimator uses an Extended Kalman Filter (EKF) to fuse high-frequency wheel odometry (250 Hz) with latency-compensated vision measurements:
// Adding vision measurement with dynamic standard deviationsdouble distanceToTag = getAverageTagDistance();double xyStdDev = 0.05 * Math.pow(distanceToTag, 2); // Trust closer tags significantly more
m_poseEstimator.setVisionMeasurementStdDevs(VecBuilder.fill(xyStdDev, xyStdDev, 9999999));m_poseEstimator.addVisionMeasurement(visionPose, timestampSeconds);Never use static vision confidence. As the robot moves farther away from an AprilTag, pixel resolution drops and angular error multiplies with the square of distance. Scaling standard deviations with distance ensures robot odometry does not jump erratically when viewing distant tags.
Verified Sources & Standards
- Limelight Vision Processing Documentationdocs.limelightvision.ioHardware Vendor
- PhotonVision Documentationdocs.photonvision.orgCommunity Reference