Manual alignment wastes match time. Automated alignment locks the drivetrain heading to the scoring target at the press of a button, letting the driver focus entirely on translation and timing.
This lesson implements auto-aiming heading lock, lookup tables for distance compensation, and on-the-fly drive-to-pose navigation.
1. Auto-Aiming Heading Lock
Section titled “1. Auto-Aiming Heading Lock”The most effective teleoperated assist is Heading Lock toward Goal. The driver commands forward and strafe speeds with the left stick, while a PID controller automatically commands rotational velocity () to face the scoring target:
package frc.robot.commands;
import edu.wpi.first.math.controller.PIDController;import edu.wpi.first.math.geometry.Pose2d;import edu.wpi.first.math.geometry.Rotation2d;import edu.wpi.first.math.kinematics.ChassisSpeeds;import edu.wpi.first.wpilibj2.command.Command;import frc.robot.subsystems.CommandSwerveDrivetrain;import java.util.function.DoubleSupplier;
public class AlignToTargetCommand extends Command { private final CommandSwerveDrivetrain m_drivetrain; private final DoubleSupplier m_vxSupplier; private final DoubleSupplier m_vySupplier; private final Pose2d m_targetLocation;
private final PIDController m_headingPID = new PIDController(5.0, 0.0, 0.2);
public AlignToTargetCommand( CommandSwerveDrivetrain drivetrain, DoubleSupplier vx, DoubleSupplier vy, Pose2d targetLocation ) { m_drivetrain = drivetrain; m_vxSupplier = vx; m_vySupplier = vy; m_targetLocation = targetLocation;
m_headingPID.enableContinuousInput(-Math.PI, Math.PI); addRequirements(m_drivetrain); }
@Override public void execute() { Pose2d currentPose = m_drivetrain.getPose();
// 1. Calculate vector from robot to target double dx = m_targetLocation.getX() - currentPose.getX(); double dy = m_targetLocation.getY() - currentPose.getY();
// 2. Compute desired heading angle Rotation2d targetHeading = new Rotation2d(Math.atan2(dy, dx));
// 3. Compute corrective rotation rate double currentHeadingRad = currentPose.getRotation().getRadians(); double omega = m_headingPID.calculate(currentHeadingRad, targetHeading.getRadians());
// 4. Drive field-oriented with driver translation + auto rotation m_drivetrain.driveFieldOriented( m_vxSupplier.getAsDouble(), m_vySupplier.getAsDouble(), omega ); }
@Override public void end(boolean interrupted) { m_headingPID.reset(); }}2. Dynamic Distance Lookups (InterpolatingDoubleTreeMap)
Section titled “2. Dynamic Distance Lookups (InterpolatingDoubleTreeMap)”As distance to the target changes, the arm angle and flywheel RPM required to score also change. WPILib includes InterpolatingDoubleTreeMap to interpolate smooth values between measured test distances:
import edu.wpi.first.math.interpolation.InterpolatingDoubleTreeMap;
public class ShooterSubsystem extends SubsystemBase { // Distance (meters) -> Flywheel Target (rot/s) private final InterpolatingDoubleTreeMap m_flywheelMap = new InterpolatingDoubleTreeMap();
// Distance (meters) -> Arm Angle (degrees) private final InterpolatingDoubleTreeMap m_armMap = new InterpolatingDoubleTreeMap();
public void configureLookupTables() { // Distance, Flywheel Velocity m_flywheelMap.put(1.5, 45.0); // 1.5m -> 45 rot/s m_flywheelMap.put(2.5, 52.0); // 2.5m -> 52 rot/s m_flywheelMap.put(4.0, 68.0); // 4.0m -> 68 rot/s
// Distance, Arm Angle m_armMap.put(1.5, 30.0); m_armMap.put(2.5, 42.0); m_armMap.put(4.0, 58.0); }
public double getTargetRPM(double distanceMeters) { return m_flywheelMap.get(distanceMeters); }}3. On-The-Fly Pathfinding to Pose
Section titled “3. On-The-Fly Pathfinding to Pose”Using PathPlanner’s dynamic pathfinding, you can command the robot to drive autonomously to any pose on the field, dynamically routing around obstacles:
public Command driveToScoringStationCommand(Pose2d scoringPose) { return AutoBuilder.pathfindToPose( scoringPose, new PathConstraints( MetersPerSecond.of(3.5), MetersPerSecondPerSecond.of(3.0), DegreesPerSecond.of(360.0), DegreesPerSecondPerSecond.of(720.0) ), 0.0 // Goal end velocity (stop at station) );}Always allow the driver to cancel automated alignment immediately. Binding auto-align to whileTrue() ensures that releasing the button instantly returns full manual control to the driver sticks.
Common Failure Modes
Section titled “Common Failure Modes”1. Robot Aims Away from Target (180° Error)
Section titled “1. Robot Aims Away from Target (180° Error)”- Symptom: Activating alignment makes the robot point its back bumper at the target.
- Cause: Shooter or intake is mounted on the back of the robot, but heading math assumed front (+X).
- Fix: Add
Rotation2d.fromDegrees(180)totargetHeading.
2. Aiming Oscillates at Distance
Section titled “2. Aiming Oscillates at Distance”- Symptom: Close to the target alignment is smooth, but far away the robot wobbles back and forth.
- Cause: Heading PID
kPis tuned too aggressively, or vision updates are lagging. - Fix: Reduce heading
kPand add a small derivative gain (kD = 0.15) for damping.
Check Your Work
Section titled “Check Your Work”- Place the robot at various positions across the practice field.
- Hold the auto-align button while driving around.
- Verify the robot maintains alignment pointing directly at the target center while smoothly translating.
Verified Sources & Standards
- Limelight MegaTag2 Robot-Space Trackingdocs.limelightvision.ioHardware Vendor
- WPILib ProfiledPIDController Auto-Targetingdocs.wpilib.orgWPILib Standard