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. Tuner X Swerve Generator Setup
Section titled “1. Tuner X Swerve Generator Setup”- Open Phoenix Tuner X on a laptop connected to the CANivore bus.
- Select the Swerve Project Generator from the left navigation panel.
- 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:
Mechanical Squaring Procedure:
Section titled “Mechanical Squaring Procedure:”- Place the robot on a solid cart or blocks so all four wheels spin freely.
- 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.
- In the Tuner X Swerve Generator, click Zero Encoders.
- Tuner X reads the current magnetic position of each CANcoder and calculates the required
MagnetOffset(between -0.5 and +0.5 rotations). - Click Generate Project to export the code.
// TunerConstants.java generated offset exampleprivate 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;3. Verifying Inversions on the Test Cart
Section titled “3. Verifying Inversions on the Test Cart”Before placing the robot on the carpet, verify motor directions on the cart:
- Enable the robot in Teleop.
- 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.
- 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.
4. Heading Stabilization PID
Section titled “4. Heading Stabilization PID”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 controllerprivate 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;}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!
Common Failure Modes
Section titled “Common Failure Modes”1. Wheels Flop 180° When Driving Forward
Section titled “1. Wheels Flop 180° When Driving Forward”- 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
kPis too high, or steer gear ratio in configuration does not match physical hardware. - Fix: Lower steer
kPinTunerConstants.javaby 30%.
Check Your Work
Section titled “Check Your Work”- Power cycle the robot with all four wheels turned in random directions.
- Enable teleop. All four wheels should snap smoothly to 0° forward within 200 ms.
- Drive forward, backward, left, and right on the carpet. The robot should track cleanly along cardinal directions.
Verified Sources & Standards
- CTRE Phoenix 6 Swerve API & Tuner X Swerve Generatorv6.docs.ctr-electronics.comHardware Vendor
- WPILib Units Librarydocs.wpilib.orgWPILib Standard