Gray Matter
WorkshopSwerve Calibration
Rough draft: nobody has reviewed this lesson yet, and it may not be how things are done this season.
LESSON 25

Swerve Calibration

The project you generated holds offsets from wheels you held straight by eye. This lesson zeroes the four modules against a straight edge and tunes the motors that steer them. No Java.

12 minutes
You’ll need
  • A swerve robot you can drive, from Swerve Project Generator.
  • AdvantageScope connected to the robot, for live plots.
  • Phoenix Tuner X, and a long straight edge.

Three kinds of zeroing

Three operations in the swerve code all get called resetting or seeding. Mix them up and you get a robot that drives beautifully and has no idea where it is.

seedFieldCentric(), on the left bumper, changes which way the sticks call forward: whatever the robot faces now becomes forward. Every loop, applyOperatorPerspective() sets that same forward from alliance color: 0° on blue and 180° on red. Neither supplies an x or a y.

resetPose(Pose2d) moves the pose itself: x, y and heading in meters from the blue corner. It exists on the Phoenix 6 drivetrain, but DriveMechanism does not expose it and nothing calls it. Until Vision in Workshop 6, read Drivetrain/Pose as distance traveled since boot.

Zero the modules

Each module has a CANcoder reading which way its wheel points, and an offset saying which reading counts as straight ahead. The four k*EncoderOffset constants in your file came from the wheels you held straight by eye in the generator. A straight edge does better.

  1. Disable the robot. Nothing should be commanding a module while you set it.
  2. Press a straight edge flat along one side so both wheels on that side sit against it, then do the other side. By eye is not close enough: half a degree of steering error walks the robot 5 cm sideways over six meters.
  3. Check which way each module faces. CTRE marks this one important: every module's bevel gear has to face the vertical center of the robot. A module that is straight but flipped zeroes 180° off, and the drive verification tests fail later without saying why.
  4. Holding the wheels straight, run the calibration step in the Tuner X swerve generator. It reads all four CANcoders where they sit and writes the offset constants for you.

Generate TunerConstants.java again and replace your copy, the way you did on the last page, then redeploy.

CTRE: Tuner X Swerve Project Generator

Tune the steer gains

A steering motor holds an angle. That is a position loop with no gravity to fight, so skip kG and start at kS. The generator gave you real numbers, kP 100, kD 0.5, kS 0.1 and kV 1.91, and they are often close. Adjust them rather than starting from zero.

Follow the order from the PID page. kS is the smallest output that breaks the module loose. Raise kP until it oscillates, then back off. Add kD, as much as you can get without jitter. Change one number per test.

DriveMechanism publishes Drivetrain/ModuleStates and Drivetrain/ModuleTargets. Put the angle from both on one AdvantageScope plot and flick the right stick. Tuned looks like two traces on top of each other. Untuned lags, overshoots, or buzzes.

Check your work

Put the robot on the floor with a carpet seam or a taped line ahead of it, and keep a hand on disable.

  1. Enable, push the left stick forward, and sight along the line for a few meters.
  2. Flick the right stick back and forth while you watch the angle plot.
  3. Turn 90° in place and press the left bumper. Watch Drivetrain/Pose.
Check

You should see

The robot tracking the line with no steady curve to either side. Commanded and measured module angles sitting on top of each other through every flick. After the bumper, forward moves to the new facing, and x and y do not change.

A consistent curve to one side means a module is zeroed wrong, so put the straight edge back on and re-save. A wander that comes and goes is the steer gains. With both clean, go on to Swerve Drive Tuning for the measurements that need the carpet.

Check yourself

After zeroing, the robot drives a steady curve to the left on every run. What do you do?

One module is straight against the edge, but its bevel gear faces outward. What happens if you save the zeros like that?

You flick the right stick and plot commanded against measured module angle. What does a tuned steer loop look like?

Which call moves the robot's x and y in Drivetrain/Pose?

You are on the red alliance. What does applyOperatorPerspective() set forward to?

Pick an answer for each.