Lab 3: Four Modules and Encoder Offsets

Goal: make "0°" mean "straight forward" on every module, then point all four wheels at any angle from the dashboard.

Why: offsets cause most swerve bugs

Each absolute encoder was installed at some random rotation. When the wheel points straight forward, FL might read 37°, FR 212°, and so on. (In Lab 0 you saw exactly that: the simulated encoders are mounted at random angles, like real ones.) The offset is that reading:

angle = rawAngle − offset

If one offset is wrong by 10°, that wheel always points 10° off from the others. The robot still drives, but it scrubs, pulls to one side, and odometry drifts. No amount of tuning or kinematics fixes a bad offset.

Which way is "forward"? A swerve wheel looks the same pointing forward or backward, but it isn't: the drive direction flips. On a real robot you pick a reference you can see, like all bevel gears facing the robot's left side, and use it for every module every time you calibrate.

Calibrate

In the simulator, every wheel starts pointing straight forward when the simulation starts, just as if you'd lined them up with a straightedge.

  1. Restart the simulation so the wheels are straight. Don't enable.
  2. Read each module's RawAngleDeg. (Check with Sim/TrueModuleStates in AdvantageScope that every wheel really points forward.)
  3. Put them in Constants.Swerve, in radians:
// Raw absolute-encoder angle when the wheel points straight forward.
// Measured on <date>. Re-measure if a module is removed or an encoder is moved.
public static final double[] ENCODER_OFFSETS_RADIANS = {
    Math.toRadians(0.0),   // FL TODO
    Math.toRadians(0.0),   // FR TODO
    Math.toRadians(0.0),   // BL TODO
    Math.toRadians(0.0),   // BR TODO
};

On the real robot (Lab 11) it's the same process: robot on blocks, line the wheels up with a straightedge, bevel gears on the same side, then read the angles.

Pass the offset into each SwerveModule (add a constructor parameter) and subtract it:

public Rotation2d getAngle() {
  // Rotation2d wraps the result into (−π, π] for us.
  return Rotation2d.fromRadians(getRawAngleRadians() - offsetRadians);
}

Code

Add to SwerveSubsystem:

/** Point every wheel at the same angle. No driving. */
public void pointAllModules(Rotation2d angle) {
  for (SwerveModule module : modules) {
    module.setSteerTarget(angle);
  }
}

And in RobotContainer:

swerve.setDefaultCommand(Commands.run(
    () -> swerve.pointAllModules(Rotation2d.fromDegrees(Tunable.number("Lab3/TargetDeg", 0))),
    swerve));

Try it

  1. Enable with TargetDeg = 0. All four wheels should point straight forward. Check with Sim/TrueModuleStates.
  2. Try 90° (all wheels point left), −90°, 45°.
  3. Do all four roll forward? Temporarily give each module 1 V of drive in pointAllModules and watch the true speeds. If one rolls backward, its offset is probably 180° off. Fix the offset, not the inversion.

Experiments

Change Predict What you'll see
Add 10° to FL's offset Which way does FL point at target 0°? 10° off from the others. Remember what that looks like!
Add 180° to FL's offset Which way does FL point? Which way does it roll? Points the same way, rolls backward. Sneaky bug.
Target 0°, then push one module with the sim hand, release It snaps back. All four use the same PID gains.
Target 0° → 180° How much does each module rotate? 180°. Lab 4 shows how to avoid that.

Checkpoint

Summary: Every module's 0° is straight forward with consistent bevel gears, and all four steer together.

Quiz

  1. What does the offset represent, physically?
  2. Why does a 180° offset error give a wheel that points right but drives backward?
  3. On a real robot, why use the bevel-gear side as a reference?
  4. When do you need to re-calibrate offsets?

What could go wrong in the real world: re-assembling a module after a repair and forgetting to re-measure; a set screw slipping on the encoder magnet so the offset drifts during a match; calibrating with one bevel gear flipped.