Skip to content
learnFRC.org
programminglesson35 min

Swerve Project Generation & Calibration

Generating the CTRE Phoenix 6 Swerve / REV MAXSwerve project, measuring CANcoder absolute magnet offsets, and heading lock PID control.

What you will learn

  • Generate a verified swerve project in Phoenix Tuner X from chassis geometry.
  • Measure absolute CANcoder zero-offsets using physical alignment jigs.
  • Verify drive wheel inversions and azimuth steer directions on the test stand.

Prerequisites:Stage 03 - Mechanism Programming & Simulation

Tools like the Phoenix Tuner X Swerve Generator generate complete kinematics, module configurations, and odometry classes directly from your chassis dimensions and gear ratios.

This lesson covers generating the project, calibrating CANcoder absolute offsets, and verifying module inversions.


  1. Open Phoenix Tuner X on a laptop connected to the CANivore bus.
  2. Select the Swerve Project Generator from the left navigation panel.
  3. Configure your chassis mechanical parameters:
    • Module Type: Select your physical hardware (e.g. SDS MK4i, SDS MK4, WCP SwerveX).
    • Drive & Steer Motors: Kraken X60 or Falcon 500.
    • Azimuth Encoders: CANcoder on CANivore bus.
    • Drive Gear Ratio: e.g., L2 (6.75:1) or L3 (6.12:1).
    • Trackwidth & Wheelbase: Measure center-to-center distance between wheel contact patches (in inches or meters).

2. Calibrating CANcoder Absolute Magnet Offsets

Section titled “2. Calibrating CANcoder Absolute Magnet Offsets”

When the robot powers on, the steer motors do not know which way the wheels are pointing until they read the absolute position from the CANcoders. Calibrating the magnet offset tells software where physical forward is located:

  1. Place the robot on a solid cart or blocks so all four wheels spin freely.
  2. Align all four wheels so they face true forward (+X direction):
    • Use a long aluminum 1x2 extrusion or straightedge pressed firmly against the outer bevel gears of the left wheels, then the right wheels.
    • Bevel Gear Rule: For SDS MK4i modules, standard convention dictates that all four bevel gears face toward the left side of the chassis.
  3. In the Tuner X Swerve Generator, click Zero Encoders.
  4. Tuner X reads the current magnetic position of each CANcoder and calculates the required MagnetOffset (between -0.5 and +0.5 rotations).
  5. Click Generate Project to export the code.
// TunerConstants.java generated offset example
private static final double kFrontLeftEncoderOffset = -0.3125;
private static final double kFrontRightEncoderOffset = 0.1843;
private static final double kBackLeftEncoderOffset = 0.4218;
private static final double kBackRightEncoderOffset = -0.0912;

Before placing the robot on the carpet, verify motor directions on the cart:

  1. Enable the robot in Teleop.
  2. Push the left translation joystick forward:
    • All four wheels must rotate forward.
    • If one wheel spins backward, its drive motor inversion or CANcoder offset is 180° out of phase.
  3. Push the right rotation joystick to the left (counter-clockwise):
    • Front Left wheel points toward back-left.
    • Front Right wheel points toward front-left.
    • Back Left wheel points toward back-right.
    • Back Right wheel points toward front-right.
    • Wheels should trace the circumference of a circle centered on the robot chassis.

When driving at high speeds, wheel slip or carpet grain friction causes the robot to rotate slightly off course. Adding a Heading Lock PID Controller maintains the driver’s desired heading automatically:

// Target heading controller
private final PIDController m_headingPID = new PIDController(4.5, 0.0, 0.2);
public ChassisSpeeds applyHeadingLock(ChassisSpeeds desiredSpeeds, Rotation2d targetHeading) {
m_headingPID.enableContinuousInput(-Math.PI, Math.PI);
// Only apply heading lock if driver is not commanding rotation
if (Math.abs(desiredSpeeds.omegaRadiansPerSecond) < 0.05) {
double currentHeading = m_pigeon2.getRotation2d().getRadians();
double correctiveOmega = m_headingPID.calculate(currentHeading, targetHeading.getRadians());
desiredSpeeds.omegaRadiansPerSecond = correctiveOmega;
}
return desiredSpeeds;
}
Enable Continuous Input

Always call m_headingPID.enableContinuousInput(-Math.PI, Math.PI). Without continuous input, passing through 180° causes the controller to see a 360° error step, spinning the robot violently in a full circle!


  • Symptom: Wheels point backward instead of forward, or turn the opposite direction.
  • Cause: Bevel gears were aligned on the wrong side during the zeroing procedure.
  • Fix: Re-square the wheels ensuring bevel gears point to the designated side, and re-run Zero Encoders.

2. Drivetrain Vibrates Violently on Carpet

Section titled “2. Drivetrain Vibrates Violently on Carpet”
  • Symptom: As soon as the robot is enabled on carpet, steer motors jitter uncontrollably.
  • Cause: Steer motor kP is too high, or steer gear ratio in configuration does not match physical hardware.
  • Fix: Lower steer kP in TunerConstants.java by 30%.

  1. Power cycle the robot with all four wheels turned in random directions.
  2. Enable teleop. All four wheels should snap smoothly to 0° forward within 200 ms.
  3. Drive forward, backward, left, and right on the carpet. The robot should track cleanly along cardinal directions.

Verified Sources & Standards

Evergreen Standard Verified: Sep 25, 2026