Swerve Drive Tuning
The generator gave you a TunerConstants.java with guessed gains and measurements. This lesson tunes the steer motors, checks the wheel radius, measures top speed and slip current, then closes the drive loop.
- A swerve robot you can drive, from Swerve Project Generator.
- AdvantageScope connected to the robot, for live plots.
- Phoenix Tuner X, and six meters of clear carpet.
- A tape measure, and a wall you may push against.
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. Adjust the generator's numbers rather than starting from zero.
private static final Slot0Configs steerGains = new Slot0Configs() .withKP(100) .withKI(0) .withKD(0.5) .withKS(0.1) .withKV(1.91) .withKA(0) .withStaticFeedforwardSign(StaticFeedforwardSignValue.UseClosedLoopSign);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, and redeploy after each.
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.
Wheel radius
The generator asked for your wheel size, so kWheelRadius is already close. Measure it only if you changed wheels, or if odometry reads long or short against a tape measure.
private static final Distance kWheelRadius = Inches.of(2.167);- Tape the floor at the robot's front edge. Restart the code so
Drivetrain/Posereads (0, 0). - Drive straight forward, slowly, about five meters.
- Tape the front edge again. The gap between marks is the actual distance, and
Drivetrain/Poseat the end of the run is the reported one.
Run it three times in each direction and average. Put the result in kWheelRadius, redeploy, and repeat until tape and odometry agree.
Top speed and slip current
kSpeedAt12Volts is the speed at 12 volts applied, and full stick asks the velocity loop for exactly that speed. Use six meters of clear floor on the surface you compete on.
- Full stick forward. Hold until the speed stops climbing, then let go well before the wall.
- Plot
Drivetrain/TranslationSpeedMpsand read the plateau, not the spike. If it sits right onkSpeedAt12Volts, the loop is holding your number, not the robot's limit. Raise the number and run again until the plateau falls short of it.
A stator current limit caps how hard a wheel twists. Set it where the tire loses grip.
- Drive the robot against a wall on carpet and square it up so all four wheels point into the wall.
- In Tuner X, open one drive motor on Voltage Out and plot its velocity and its stator current.
- Ramp the voltage up slowly from zero, watching both traces.
Current climbs while velocity sits at zero. Then velocity jumps and current drops, which is the tire letting go. Read the current at the top of the climb, just before the drop. If the trace flattens at the shipped 120 A and the wheel never breaks loose, raise the constant and ramp again.
Stalled motors get hot fast
Keep each ramp short and back off to zero between attempts. One person on the disable, somebody else on the laptop.
Both numbers go in the same file.
private static final Current kSlipCurrent = Amps.of(120); // ... public static final LinearVelocity kSpeedAt12Volts = MetersPerSecond.of(4.54);Close the drive loop
Every drive request in this course is DriveRequestType.Velocity: the drive motors hold a wheel speed with the drive gains below. CTRE's generator writes OpenLoopVoltage, which sends the stick straight to volts and ignores those gains. The workshop project already changes it in Robot.java.
On a velocity loop the feedforward does nearly all of the work. The order is kV, then kS, then kP. Tune on the ground, not on blocks: a wheel in the air carries no load.
private static final Slot0Configs driveGains = new Slot0Configs().withKP(0.2).withKI(0).withKD(0).withKS(0).withKV(0.124);The request has no deadband of its own, because Phoenix's version cuts off every speed under it. The controller ignores the bottom 10% of each stick instead and stretches the rest back over the full range. To shrink it, put the robot on blocks, take your hands off the controller, and watch the speed in Drivetrain/ModuleTargets. Halve the deadband in the Robot constructor and redeploy until those targets start twitching, then go back one value.
// In the Robot constructor, above setDefaultCommanddriver.getController().setLeftXDeadband(0.05);driver.getController().setLeftYDeadband(0.05);driver.getController().setRightXDeadband(0.05);Check your work
Use the surface you compete on. Tape a start mark, restart the code so Drivetrain/Pose reads (0, 0), and drive a square: three meters forward, three left, three back, three right.
You should see
The robot back on the tape, and Drivetrain/Pose near (0, 0). Commanded and measured module angles on top of each other through all four corners.
Check yourself
The joystick request uses DriveRequestType.Velocity. What does that change compared with CTRE's generated OpenLoopVoltage?
Tape says 4.80 m, odometry says 5.00 m, and kWheelRadius is 2.167 in. What goes in the file?
You flick the right stick and plot commanded against measured module angle. What does a tuned steer loop look like?
Current climbs while velocity sits at zero, then velocity jumps and current drops. What goes in kSlipCurrent?
Why does the course put the stick deadband on the controller instead of on the FieldCentric request?