Skip to content
learnFRC.org
programminglesson35 min

Automated Alignment & Dynamic Targeting

Target tracking, heading lock PID, on-the-fly drive-to-pose commands, and speaker/reef scoring alignment.

What you will learn

  • Implement heading-lock closed-loop controllers using ProfiledPIDController.
  • Construct dynamic interpolation lookup tables for shooter distance compensation.
  • Generate on-the-fly path trajectories to navigate directly into scoring reef/feeder stations.

Prerequisites:Swerve Pose Estimator & Vision Fusion

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.


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 (ω\omega) 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);
}
}

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)
);
}
Driver Override

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.


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) to targetHeading.
  • Symptom: Close to the target alignment is smooth, but far away the robot wobbles back and forth.
  • Cause: Heading PID kP is tuned too aggressively, or vision updates are lagging.
  • Fix: Reduce heading kP and add a small derivative gain (kD = 0.15) for damping.

  1. Place the robot at various positions across the practice field.
  2. Hold the auto-align button while driving around.
  3. Verify the robot maintains alignment pointing directly at the target center while smoothly translating.

Verified Sources & Standards

Evergreen Standard Verified: Sep 25, 2026