Timeline ~2 weeks.
What is swerve? Swerve is a drivetrain where each wheel can rotate independently, allowing the robot to spin and translate simultaneously. It's what we use on most of our competition robots.
Ask a lead if you're not sure what the swerve is.
NOTE: Create a new project for this exercise and install the CTRE Phoenix 6 vendor library.
Skim through the file "How to Structure a Custom Swerve Drivetrain.md" to get the basic idea of how swerve works.
There is a sample project in the Supplementals folder. You really should use this only as a last resort, try to figure everything out yourself or ask a lead for help. Do not copy it.
We don't manually configure swerve modules — CTRE's Tuner X does it for us.
- Run the Swerve Project Generator in Tuner X (docs)
- When done, click "Only Generate TunerConstants" and save the file to your
constants/folder - Also click "Save as JSON" and save that file somewhere safe as a backup
The generated TunerConstants.java contains all your hardware IDs, gear ratios, PID gains, and a nested class called TunerSwerveDrivetrain — a typed wrapper around CTRE's SwerveDrivetrain.
Create SwerveDrive.java in subsystems/. This class should extend TunerConstants.TunerSwerveDrivetrain and implement PeriodicSubsystem.
IMPORTANT: Read the CTRE Swerve API overview before writing this class. Focus on how
SwerveDrivetrain,SwerveRequest, and telemetry work.
Add a PeriodicIO inner class to hold all your state. At minimum you need:
- A master
SwerveRequest— this is what gets sent to the drivetrain each loop, initialized asSwerveRequest.Idle - A
FieldCentricrequest — for driver-relative driving - A
RobotCentricrequest — for robot-relative driving
public class PeriodicIO {
public SwerveRequest masterRequest = new SwerveRequest.Idle();
public SwerveRequest.FieldCentric fieldCentric = new SwerveRequest.FieldCentric();
public SwerveRequest.RobotCentric robotCentric = new SwerveRequest.RobotCentric();
}The constructor takes a SwerveDrivetrainConstants and varargs SwerveModuleConstants<?, ?, ?>... and passes them to super(). After calling super():
- Call
registerTelemetry(this::setStates)to wire up state logging - Reset the pose and gyro to zero
public SwerveDrive(
SwerveDrivetrainConstants drivetrainConstants,
SwerveModuleConstants<?, ?, ?>... modules) {
super(drivetrainConstants, modules);
registerTelemetry(this::setStates);
}registerTelemetry requires a method that accepts a SwerveDriveState. Use it to cache the current module states and pose for logging:
public void setStates(SwerveDriveState state) {
periodicIO.states = state.ModuleStates;
periodicIO.target = state.ModuleTargets;
periodicIO.pose = state.Pose;
}readPeriodicInputs: log your pose, module states, and module targets with AdvantageKitwritePeriodicOutputs: sendperiodicIO.masterRequestto the hardware by callingsetControl(periodicIO.masterRequest)
Each drive method modifies the appropriate specific request, then sets masterRequest equal to it. writePeriodicOutputs sends it to the hardware.
Field-relative drive (most common for teleop):
public void driveFieldCentric(double vx, double vy, double rotationRate) {
periodicIO.fieldCentric
.withVelocityX(vx)
.withVelocityY(vy)
.withRotationalRate(rotationRate);
periodicIO.masterRequest = periodicIO.fieldCentric;
}Robot-relative drive:
public void driveRobotCentric(double vx, double vy, double rotationRate) {
periodicIO.robotCentric
.withVelocityX(vx)
.withVelocityY(vy)
.withRotationalRate(rotationRate);
periodicIO.masterRequest = periodicIO.robotCentric;
}Create DrivetrainCommands.java in commands/. This is pretty much the same as the minibot's DrivetrainCommands.
You should be able to figure this out yourself.
Scale your velocity inputs by the robot's max speed (e.g. TunerConstants.kSpeedAt12Volts). For field-relative drive, flip the sign when on the Red alliance so the driver's "forward" always matches their perspective.
Instantiate SwerveDrive with the constants from TunerConstants:
final SwerveDrive swervedrive = new SwerveDrive(
TunerConstants.DrivetrainConstants,
TunerConstants.FrontLeft,
TunerConstants.FrontRight,
TunerConstants.BackLeft,
TunerConstants.BackRight
);Register the default command.
-
TunerConstants.javais generated and in yourconstants/folder -
SwerveDriveextendsTunerConstants.TunerSwerveDrivetrainand implementsPeriodicSubsystem -
PeriodicIOhas a master request, aFieldCentricrequest, and aRobotCentricrequest -
registerTelemetryis called in the constructor - Pose and module states are logged in AdvantageScope
-
writePeriodicOutputscallssetControl(periodicIO.masterRequest) - Field-relative and robot-relative drive methods work correctly
-
DrivetrainCommandswraps both drive methods withDoubleSupplierparameters -
RobotContainerinstantiates the drivetrain and sets the default command - Robot drives correctly in field-relative mode from a joystick
Tell a lead when you're done so they can verify the robot drives correctly.