High-speed autonomous paths subject swerve drive wheels to intense lateral and longitudinal forces. When four drive wheels accelerate at 4.0 m/s² while changing heading at 720 deg/s, wheel scrub is inevitable. Uncorrected wheel odometry can drift by 0.2 to 0.5 meters over a ten-second autonomous period, causing the final scoring shot or game piece grab to miss completely.
This lesson details how to dynamically weight AprilTag vision estimates to eliminate odometry drift and implement terminal target-locking commands.
1. What to Learn First
Section titled “1. What to Learn First”Before configuring vision correction:
- Verify that your camera-to-robot coordinate transforms (3D pitch, roll, yaw, X, Y, Z offsets) are measured accurately to within 5 millimeters and 0.5 degrees as covered in AprilTag Pipelines.
- Verify that your coprocessor timestamps are synchronized with the roboRIO via NetworkTables 4 server time.
2. Dynamic Vision Standard Deviations
Section titled “2. Dynamic Vision Standard Deviations”A static vision measurement noise matrix causes problems:
- If noise standard deviation is set too low (e.g., 0.05 m), distant noisy tag detections (> 4.5 m) will pull the robot’s estimated pose around erratically.
- If noise standard deviation is set too high (e.g., 1.0 m), close tag detections (< 1.5 m) will fail to correct odometry drift.
WPILib’s SwerveDrivePoseEstimator allows updating vision standard deviations per measurement:
sigma_xy = k1 * (d^2 / N_tags)Where:
- is the distance from the camera to the target in meters.
N_tagsis the number of tags in the current coprocessor solve (multi-tag PnP solves provide much lower error than single-tag solves).k1is a team tuning scalar (typically ).
package frc.robot.subsystems;
import edu.wpi.first.math.VecBuilder;import edu.wpi.first.math.Matrix;import edu.wpi.first.math.numbers.N1;import edu.wpi.first.math.numbers.N3;import edu.wpi.first.math.geometry.Pose2d;
public class VisionStandardDeviations { /** * Calculates dynamic vision measurement standard deviations based on distance and tag count. */ public static Matrix<N3, N1> calculate(double distanceMeters, int tagCount, double ambiguity) { // Reject single-tag solutions with high pose ambiguity if (tagCount == 1 && ambiguity > 0.18) { return VecBuilder.fill(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE); }
// Base standard deviations for close-range multi-tag solve: 0.05m translation, 2 deg rotation double xyStdDev = 0.05; double rotStdDev = Math.toRadians(2.0);
if (tagCount >= 2) { // Multi-tag solve: quadratic distance scaling xyStdDev = 0.03 * (1.0 + (distanceMeters * distanceMeters) / 16.0); rotStdDev = Math.toRadians(1.5) * (1.0 + distanceMeters / 4.0); } else { // Single-tag solve: higher base noise xyStdDev = 0.12 * (1.0 + (distanceMeters * distanceMeters) / 8.0); rotStdDev = Math.toRadians(6.0) * (1.0 + distanceMeters / 3.0); }
return VecBuilder.fill(xyStdDev, xyStdDev, rotStdDev); }}3. Feeding Filtered Vision into the Pose Estimator
Section titled “3. Feeding Filtered Vision into the Pose Estimator”In your DrivetrainSubsystem.periodic():
public void updateVisionMeasurements(VisionEstimate estimate) { if (estimate == null || !estimate.hasValidTarget()) { return; }
// Sanity check: reject poses outside field perimeter boundaries Pose2d estimatedPose = estimate.getRobotPose(); if (estimatedPose.getX() < 0.0 || estimatedPose.getX() > 16.54 || estimatedPose.getY() < 0.0 || estimatedPose.getY() > 8.21) { return; }
Matrix<N3, N1> stdDevs = VisionStandardDeviations.calculate( estimate.getAverageTagDistance(), estimate.getTagCount(), estimate.getAmbiguity() );
// Apply measurement to WPILib SwerveDrivePoseEstimator m_poseEstimator.addVisionMeasurement( estimatedPose, estimate.getTimestampSeconds(), stdDevs );}4. Terminal Closed-Loop Alignment
Section titled “4. Terminal Closed-Loop Alignment”Rather than relying entirely on pre-baked path coordinates for the final scoring placement, use a closed-loop terminal alignment controller during the final 0.5 meters of approach:
package frc.robot.commands;
import edu.wpi.first.math.controller.PIDController;import edu.wpi.first.math.geometry.Pose2d;import edu.wpi.first.math.kinematics.ChassisSpeeds;import edu.wpi.first.wpilibj2.command.Command;import frc.robot.subsystems.SwerveSubsystem;
public class AlignToTargetPoseCommand extends Command { private final SwerveSubsystem m_swerve; private final Pose2d m_targetPose;
private final PIDController m_xController = new PIDController(4.0, 0.0, 0.1); private final PIDController m_yController = new PIDController(4.0, 0.0, 0.1); private final PIDController m_thetaController = new PIDController(5.0, 0.0, 0.15);
public AlignToTargetPoseCommand(SwerveSubsystem swerve, Pose2d targetPose) { m_swerve = swerve; m_targetPose = targetPose; m_thetaController.enableContinuousInput(-Math.PI, Math.PI); addRequirements(m_swerve); }
@Override public void execute() { Pose2d current = m_swerve.getPose();
double vx = m_xController.calculate(current.getX(), m_targetPose.getX()); double vy = m_yController.calculate(current.getY(), m_targetPose.getY()); double omega = m_thetaController.calculate( current.getRotation().getRadians(), m_targetPose.getRotation().getRadians() );
// Drive in field-relative coordinates toward target m_swerve.driveFieldRelative(new ChassisSpeeds(vx, vy, omega)); }
@Override public boolean isFinished() { return m_xController.atSetpoint() && m_yController.atSetpoint() && m_thetaController.atSetpoint(); }
@Override public void end(boolean interrupted) { m_swerve.driveRobotRelative(new ChassisSpeeds()); }}How Success is Checked
Section titled “How Success is Checked”- Clean Odometry Recovery:
- Drive the robot aggressively across the field in teleop to accumulate intentional wheel slip.
- Stop the robot in front of an AprilTag.
- Observe the AdvantageScope 2D field. The estimated pose should converge smoothly to the real physical position within 3 to 4 frames (60 to 80 milliseconds) without oscillations or snap jumps.
- Terminal Positioning Accuracy:
- Run the full autonomous routine 5 consecutive times on the practice field.
- Measure the mechanical position of the robot relative to the scoring target after each run.
- Placement error must remain under and .
Where to Go Next
Section titled “Where to Go Next”You have completed Autonomous Trajectory Engineering. To validate mechanism performance before deploying on the robot, take the Physics Simulation & Modern Control course.
Verified Sources & Standards
- WPILib SwerveDrivePoseEstimator Tuningdocs.wpilib.orgWPILib Standard
- PhotonVision AprilTag Pose Ambiguity Guidelinesdocs.photonvision.orgCommunity Reference