Skip to content
learnFRC.org
programmingreference

Vision Localization & AprilTag Fusion

3D perspective-n-point pose estimation, Limelight MegaTag algorithms, and fusing vision measurements into swerve pose estimators.

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.

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.


  • 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.
  • 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 deviations
double 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);
Dynamic Standard Deviations

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

Evergreen Standard Verified: Sep 24, 2026