Skip to content
learnFRC.org
programminglesson30 min

AprilTag Geometry & MegaTag Algorithms

3D coordinate systems, camera calibration, mounting offsets, Perspective-n-Point solvers, and MegaTag1 vs MegaTag2.

What you will learn

  • Measure 3D camera extrinsic offsets (X, Y, Z, Roll, Pitch, Yaw) from robot center.
  • Understand Perspective-n-Point (PnP) geometry and tag ambiguity resolution.
  • Compare MegaTag1 single-tag solves with MegaTag2 gyro-assisted multi-tag algorithms.

Prerequisites:Stage 04 - Swerve Drivetrain & Autonomous Routines

Official competition fields place 2D fiducial markers called AprilTags at known positions. By observing these tags and matching them against the published field layout, an optical camera calculates the robot’s absolute 3D pose.

This lesson covers camera mounting calibration, Perspective-n-Point (PnP) solving, and Limelight’s MegaTag algorithms.


1. Camera Mounting Transformation (Extrinsics)

Section titled “1. Camera Mounting Transformation (Extrinsics)”

Before a camera can locate the robot on the field, software must know where the camera is located relative to the robot’s physical center of rotation:

  • Forward (XX): Distance from robot center to camera lens (+Forward / -Backward).
  • Right (YY): Distance from robot center to camera lens (+Right / -Left).
  • Up (ZZ): Height of camera lens above carpet.
  • Roll: Tilt around forward axis.
  • Pitch: Upward angle of camera lens (+Up / -Down, typically 15° to 25°).
  • Yaw: Pan angle relative to robot forward (+Clockwise).

Enter these measured numbers into your camera configuration web UI (e.g. http://limelight.local:5801 or http://photonvision.local:5800).

Measure Within Millimeters

A 1-degree error in camera pitch or a 2 cm error in camera offset produces a 15 cm localization error when viewing a tag 4 meters away. Measure your camera mounting bracket with calipers or reference your CAD model directly.


When the camera detects an AprilTag, it identifies the four pixel corners in the image. Knowing the focal length of the camera lens and the physical 8.125-inch dimension of the tag, the camera’s coprocessor solves a Perspective-n-Point (PnP) mathematical system to compute the 3D translation and rotation vector from camera to tag.

  • Single-Tag Observation: Optical perspective distortion can cause “pose ambiguity” where the solver cannot determine whether the tag is slightly tilted left or right. This causes the calculated pose to flip intermittently.
  • Multi-Tag Observation: When two or more tags are in the camera’s field of view simultaneously, ambiguity is mathematically zero. The multi-tag PnP solver locks the 3D pose with millimeter precision.

Limelight cameras support two distinct 3D localization pipelines:

  • Solves 3D pose using optical tag corners alone.
  • Excellent when 2 or more tags are visible.
  • If only one tag is visible, optical tilt ambiguity can cause slight rotational jumps.
  • Ingests the robot’s high-precision IMU yaw angle (Pigeon 2.0) into the PnP solver as a known mathematical constraint.
  • Because the robot heading is known with 0.1° accuracy, the solver eliminates single-tag ambiguity entirely.
  • Extremely stable even while the robot is accelerating violently, bumping over obstacles, or being defended.
// Inside robot periodic loop: send IMU yaw to Limelight for MegaTag2
double yawDegrees = m_pigeon2.getYaw().getValueAsDouble();
LimelightHelpers.SetRobotOrientation("limelight", yawDegrees, 0.0, 0.0, 0.0, 0.0, 0.0);

In the Limelight web interface:

  1. Set Pipeline Type to Fiducial Markers (AprilTags).
  2. Reduce Exposure to 1000 to 2500 microseconds. The image will look dark to human eyes, but high contrast between white and black squares eliminates motion blur while driving at 4.5 m/s.
  3. Set Gain low to prevent digital noise on tag edges.

  • Symptom: Tags are recognized while stationary, but drop out as soon as the robot accelerates.
  • Cause: Exposure is too high, creating motion blur across tag borders.
  • Fix: Lower exposure until only the high-contrast tag squares are clearly visible.
  • Symptom: In MegaTag2 mode, rotating the robot causes the field position to jump 1 meter sideways.
  • Cause: Camera mounting offset (X,YX, Y) was measured from the edge of the chassis instead of the center of rotation.
  • Fix: Re-measure camera coordinates relative to the center of rotation between the four swerve modules.

  1. Place the robot at a known tape mark on the field (e.g. 2.0 meters directly in front of Tag 7).
  2. Open the camera web interface.
  3. Confirm that the calculated camera distance matches your physical tape measurement within 2 cm.

Verified Sources & Standards

Evergreen Standard Verified: Sep 25, 2026