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, the driver station, and six meters of 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.
We can also use a stator current limit to keep a wheel from slipping when the robot accelerates hard. You find the limit by driving, with the controller.
- Drive the robot up to a wall on carpet and square it up so all four wheels point into the wall.
- In Tuner X, open one drive motor and plot its stator current and its velocity.
- Push the stick forward slowly, a little at a time, and watch both traces.
Current climbs while velocity sits at zero. When the velocity goes up, the tire has let go, and the stator current at that moment is your slip current. Set kSlipCurrent slightly below it. If the trace flattens at the shipped 120 A and the wheel never breaks loose, raise the constant and try again.
Stalled motors get hot fast
Keep each push short and let go of the stick 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 find your own deadband, open the driver station, take your hands off the controller, and look at what each stick axis reads. Flick each stick and let it snap back a few times. Take the largest reading you see at rest, ignoring its sign, and add a little to it. That keeps the robot from driving when the driver is not touching the stick. Set it in the Robot constructor.
// 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?
Against the wall, current climbs while velocity sits at zero, then the velocity goes up. What goes in kSlipCurrent?
Why does the course put the stick deadband on the controller instead of on the FieldCentric request?