Check for a valid target
Before reading any target data, confirm the camera has a valid detection.
getTV returns true when at least one target is present in the active pipeline.Robot.java
Read horizontal and vertical offsets
getTX returns the horizontal angle from the crosshair to the primary target in degrees. getTY returns the vertical angle. Both values are relative to the configured crosshair position.Robot.java
tx to steer toward a target and ty to estimate distance with trigonometry.Get a MegaTag2 pose estimate
MegaTag2 fuses your robot’s gyroscope heading with AprilTag observations to produce accurate field-relative pose estimates. You must call
SetRobotOrientation with the current heading every loop before calling the MegaTag2 method.Call
SetRobotOrientation at the top of periodic() on every loop cycle, not just when you plan to use the pose estimate. The Limelight uses the orientation data internally to run the fusion algorithm.Robot.java
getBotPoseEstimate_wpiBlue_MegaTag2 returns a PoseEstimate in the WPILib Blue alliance coordinate system, which is the standard for WPILib odometry. Use getBotPoseEstimate_wpiRed_MegaTag2 if your match logic requires red-alliance coordinates.Add the measurement to your pose estimator
Pass the
PoseEstimate to WPILib’s SwerveDrivePoseEstimator or DifferentialDrivePoseEstimator via addVisionMeasurement. Always check that the result is non-null and that at least one tag was seen.Robot.java
mt2.timestampSeconds is already latency-corrected — the library adjusts the NetworkTables server timestamp by the pipeline and capture latency before returning it.Complete periodic() example
Here is a fullperiodic() method combining all four steps:
Robot.java