Swerve Project Generator
Tuner X measures your drivetrain and writes one file. The rest of the swerve project is already written, so you swap that file in and drive.
- An assembled drivetrain: eight TalonFX, four CANcoders, one Pigeon 2, one CANivore.
- Phoenix Tuner X connected, with firmware current on every device.
- Module anatomy and field-centric driving from Swerve Basics.
- A tape measure, and the robot up on blocks.
Start from the workshop project
Download the swerve project below. It is the code on this page, and steps 5 and 6 are the only two edits it needs.
Download Swerve Project (v3.2, Commands v3)What the generator writes
The generator can write a whole project. We already have one made for you, so we only care about one file it writes, TunerConstants.java.
Six steps, in order
- Put every device on one CAN bus with a unique ID, and write the IDs down corner by corner. You want thirteen listed and no duplicate-ID warning.
- Open the swerve project generator, under Mechanisms, behind a New Project button.
- Measure the robot and enter four numbers. Take them off the real robot, not the CAD.
- Wheelbase, labeled FL to BL: front-to-back distance between module centers.
- Trackwidth, labeled FL to FR: side-to-side distance between module centers.
- Wheel radius: half the wheel's diameter.
- Drive gear ratio: motor rotations per wheel rotation, off the module's spec sheet.
- For each module, pick its CANcoder and two motors. Then run Encoder Calibration with the wheel straight and the bevel gear facing the center of the robot. With the robot on blocks, run the steer and drive checks. If the corner moving is not the one the wizard named, its devices are assigned to the wrong module.
- Generate only the constants. Do not select Generate Project. Overwrite
src/main/java/frc/robot/generated/TunerConstants.javawith the file it writes. You should see your own CAN IDs and akCANBusline naming your CANivore. - Change
teamNumberin.wpilib/wpilib_preferences.jsonfrom5712to your team number, and deploy to SystemCore.
Check your work
Robot on blocks for the first two checks, then on the floor with room around it. Keep a hand on disable.
- Select Teleop and enable, hands off the controller. All four wheels stay still and each module holds the angle it had.
- Push the left stick forward: four modules pointing the same way, four wheels turning the same direction. Push the right stick sideways and the modules splay into a rotation pattern.
- On the floor, point the robot away from you and push forward. It drives away in a straight line. Turn it 90 degrees in place, push forward again, and it goes the same direction across the floor. Press the left bumper and forward becomes whatever way it is facing now.
- Let go of everything. The robot stops and stays stopped, because its default command is asking for zero.
Full simulation
Hardware Simulation ran your code on the laptop against real motors on the bench. Simulate Robot Code runs it with no hardware at all.
- Choose Simulate Robot Code from the same toolbar menu, the line above Hardware Sim Robot Code.
- In the simulation window, drag your controller onto Joystick[0], pick Teleoperated, and select the Teleop OpMode.
- Open AdvantageScope, connect it to the simulator, and drag
Drivetrain/Poseonto a 2D field view.
You should see: the robot drawn on the field, moving when you push the stick and stopping when you let go. Every later swerve page that says "in the simulator" means this.
The drivetrain default command
Your own drivetrain code goes in DriveMechanism.java, which is what an OpMode talks to. That keeps it out of the files Tuner X writes.
A canceled command leaves its last request in the motors, so a drivetrain would keep rolling. The Robot constructor makes the joystick drive the drivetrain's default command, in every mode.
public class Robot extends OpModeRobot { public final DriveMechanism drivetrain = new DriveMechanism(); public final CommandXboxController driver = new CommandXboxController(0); private final double maxSpeed = TunerConstants.kSpeedAt12Volts.in(MetersPerSecond); // top speed private final double maxAngularRate = RotationsPerSecond.of(0.75).in(RadiansPerSecond); // 3/4 turn per second private final SwerveRequest.FieldCentric drive = new SwerveRequest.FieldCentric() .withDriveRequestType(DriveRequestType.Velocity); // closed loop, uses the drive gains public Robot() { // Joystick drive, in every mode. Set here instead of in an OpMode so it always exists: when // any other drivetrain command ends or is interrupted, the drivetrain goes back to the sticks. // In WPILib, X points forward and Y points left. The sticks read the other way around, so // each axis below gets a minus sign. The controller already ignores the bottom 10% of each // stick, so a stick at rest reads 0. drivetrain.setDefaultCommand( drivetrain.applyRequest( () -> drive .withVelocityX(-driver.getLeftY() * maxSpeed) // left stick up = forward .withVelocityY(-driver.getLeftX() * maxSpeed) // left stick left = left .withRotationalRate( -driver.getRightX() * maxAngularRate))); // right stick left = turn left // ... the disabled Idle binding, unchanged } // ... simulationInit and robotPeriodic, unchanged}applyRequest(...) is built on runRepeatedly, so it re-reads the sticks and sends a fresh request every loop. Let go and the command is still running, asking for zero.
TeleopOpMode keeps only its buttons. The left bumper runs seedFieldCentric(), which resets only the rotation of the pose. The way the robot faces right now becomes forward for the driver. X and Y do not change.
@Teleop(name = "Teleop")public class TeleopOpMode extends PeriodicOpMode { public TeleopOpMode(Robot robot) { // Left bumper: make the robot's current facing the new "forward". robot.driver.leftBumper().onTrue(robot.drivetrain.seedFieldCentric()); }}Check yourself
You finish the Tuner X wizard. Which file do you take back to the workshop project?
The wizard says it is testing the front-left module, and the back-right wheel turns. What is wrong?
You want a command that drives the robot in a square. Which file does it talk to?
In teleop you let go of every stick. Why does the robot stop?
You press the left bumper, which runs seedFieldCentric(). What changed?