Lab 11: Move to the Real Robot

Goal: run the exact code from Labs 0–10 on our real swerve: NEOs on SPARK MAXes, with Thrifty absolute encoders on the roboRIO's analog inputs. Only the hardware layer changes.

Safety first

From here on, code moves a real robot that can go in any direction instantly.

  • Robot on blocks, wheels off the ground, until the test order below says otherwise.
  • One person on the E-stop (space bar on the Driver Station disables; Enter is E-stop). Say "enabling" out loud before you enable.
  • Low limits first: Drive/MaxSpeedMps 0.5, Steer/MaxVolts 4. Raise them only after it works.
  • Hands, hair, and cables away from the wheels. When anything surprises you: disable first, think second.

Why: the payoff of ModuleIO

Since Lab 0, our code has only used ModuleIO and GyroIO. Now we write a second implementation that talks to real hardware. SwerveModule, kinematics, odometry, and commands don't change at all.

SwerveModule ──► ModuleIO ──┬─► ModuleIOSim       (simulator)
                            └─► ModuleIOSparkMax  (real robot)   ← this lab

What does change is the numbers: the real robot has its own gear ratios, dimensions, inversions, offsets, and friction. The simulator taught us how to find each one.

Before you start

  1. Fill in Our Hardware: module type, gear ratios, wheel diameter, wheelbase, trackwidth, gyro, CAN IDs, analog channels.
  2. Update the SPARK MAXes to current firmware with the REV Hardware Client, and set their CAN IDs. Write the IDs on tape on each controller.
  3. In WPILib VS Code: WPILib: Set Team Number.
  4. Install the vendor library for our gyro (Manage Vendor Libraries).

Code

sim/ModuleIOSparkMax.java (it's in sim/ next to ModuleIO; rename the package to io if you like):

package frc.robot.sim;

import com.revrobotics.PersistMode;
import com.revrobotics.ResetMode;
import com.revrobotics.spark.SparkLowLevel.MotorType;
import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;
import com.revrobotics.spark.config.SparkMaxConfig;
import edu.wpi.first.wpilibj.AnalogInput;

/** One real module: two NEOs on SPARK MAXes and a Thrifty absolute encoder on a roboRIO analog input. */
public class ModuleIOSparkMax implements ModuleIO {
  private final SparkMax driveMotor;
  private final SparkMax steerMotor;
  private final AnalogInput absoluteEncoder;

  public ModuleIOSparkMax(int driveCanId, int steerCanId, int encoderAnalogChannel) {
    driveMotor = new SparkMax(driveCanId, MotorType.kBrushless);
    steerMotor = new SparkMax(steerCanId, MotorType.kBrushless);
    absoluteEncoder = new AnalogInput(encoderAnalogChannel);

    // Brake mode holds the robot still when disabled; current limits protect motors and battery.
    // No inversion here: inversions stay in SwerveModule (Lab 1), in one place.
    SparkMaxConfig driveConfig = new SparkMaxConfig();
    driveConfig.idleMode(IdleMode.kBrake).smartCurrentLimit(40); // amps
    driveMotor.configure(driveConfig, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);

    SparkMaxConfig steerConfig = new SparkMaxConfig();
    steerConfig.idleMode(IdleMode.kBrake).smartCurrentLimit(20); // steering needs much less
    steerMotor.configure(steerConfig, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
  }

  @Override public double getDriveMotorRotations() { return driveMotor.getEncoder().getPosition(); }
  @Override public double getDriveMotorRPM() { return driveMotor.getEncoder().getVelocity(); }
  @Override public double getSteerMotorRotations() { return steerMotor.getEncoder().getPosition(); }
  @Override public double getAbsoluteEncoderVolts() { return absoluteEncoder.getVoltage(); }
  @Override public void setDriveVoltage(double volts) { driveMotor.setVoltage(volts); }
  @Override public void setSteerVoltage(double volts) { steerMotor.setVoltage(volts); }
}

Check current docs

These are the 2026 REVLib names (ResetMode/PersistMode moved to com.revrobotics in 2026). If it doesn't compile, check the REVLib docs.

In SwerveSubsystem, pick the hardware at startup:

// Only create the simulated world when simulating.
private final SimWorld simWorld = RobotBase.isSimulation() ? new SimWorld() : null;

// in the constructor:
ModuleIO io = RobotBase.isReal()
    ? new ModuleIOSparkMax(Constants.Swerve.DRIVE_CAN_IDS[i], Constants.Swerve.STEER_CAN_IDS[i],
                           Constants.Swerve.ENCODER_ANALOG_CHANNELS[i])
    : simWorld.getModuleIO(i);

// simulationPeriodic() only runs in simulation, so it can keep calling simWorld.update(0.02).

Add the CAN IDs and analog channels to Constants.Swerve. Do the same for the gyro: write a GyroIO for ours. For example, a Pigeon 2 (Phoenix 6) has getRotation2d(), which is already CCW-positive. Check the current vendor docs for ours.

Put the real gear ratios, wheel diameter, and dimensions in Constants.Swerve. Keep the simulated robot's numbers somewhere (for example a SIM_ copy) if you want the simulator to keep matching the old lab results.

Bring-up order (robot on blocks)

Each step repeats a simulator lab, now on hardware:

Step Repeat Check
1. Deploy, stay disabled, turn wheels by hand Lab 0 Every RawAngleDeg changes for the right module. A stuck value = loose wire or wrong analog port.
2. One module, raw voltage Lab 1 Stick up = wheel rolls forward; stick left = module turns CCW and angle goes up. Fix with the INVERTED arrays.
3. Steer PID, one module Lab 2 Starts at your sim kP; real friction may need more. If it spins forever: disable, encoder direction is wrong.
4. Offsets Lab 3 Line up all four wheels with a straightedge, bevel gears all facing the same side. Read RawAngleDeg, put in ENCODER_OFFSETS_RADIANS.
5. Translation, then rotation Labs 4–5 Wheels match the AdvantageScope arrows. Pure rotation makes a diamond.
6. On the ground, 0.5 m/s Open area. Drive forward, sideways, spin.
7. Closed-loop speed, gyro, odometry Labs 6–8 kS is usually much higher on carpet. Gyro: rotate CCW by hand, heading goes up. Odometry: use tape and a tape measure.
8. SysId and auto Labs 9–10 Lots of room for SysId.

NEO velocity lag (Lab 6): the SPARK MAX averages velocity over a long window by default. REVLib's encoder config has settings for the measurement period and averaging depth; check the current docs if your drive kP oscillates.

Optional: steer PID on the SPARK MAX. Our steer PID runs on the roboRIO at 50 Hz. The SPARK MAX can run it at 1000 Hz with the NEO's own encoder: at boot, read the Thrifty angle and seed the NEO encoder with it (converted with the steer ratio), then use the SPARK MAX closed-loop controller with position wrapping. Compare the step response to the roboRIO version.

Debugging checklist

Symptom First thing to check
A module never moves CAN ID, firmware, breaker, REVLib warning in the console
Module spins forever Encoder direction vs. motor direction (Lab 1–2)
One wheel points off from the rest Its offset (Lab 3)
One wheel points right but drives backward Offset 180° off, or that module's DRIVE_INVERTED
Field-relative fine at 0°, wrong at 90° Gyro sign (Lab 7)
Brownouts when pushing Current limits

Checkpoint

Summary: Because our code only used ModuleIO and GyroIO, moving to real hardware meant one new class, new constants, and re-measuring what the simulator taught us to measure.

Quiz

  1. Which classes changed to run on the real robot? Which didn't?
  2. Which numbers did you have to measure again on the real robot, and why couldn't you reuse the simulator's?
  3. Why bring up one module at a time, on blocks?
  4. What's different between friction in our simulator and on carpet?

What could go wrong in the real world: a loose CAN wire cutting out a module mid-drive; a SPARK MAX that lost its config after a reset (that's why we persist parameters); brownouts from missing current limits.

Coming in 2027 (SystemCore): analog inputs read 0–3.3 V instead of 0–5 V (check the Thrifty encoder's output before wiring it), and WPILib packages and some method names change.