Lab 1: One Module, Raw Power

Goal: spin one module's drive and steer motors from the joystick. Make the directions right, and turn motor rotations into meters and radians.

Why: directions and units

Directions. In our frame (+X forward, +Y left, +θ counterclockwise), "positive" must mean the same thing everywhere:

  • Positive drive voltage → wheel rolls forward (when the module points forward).
  • Positive steer voltage → module turns counterclockwise viewed from above, and the absolute angle goes up.

On a real robot these are often backwards (a motor mounted the other way, an encoder reading the other way). Fix it in one place (an INVERTED constant per module) so every later lab can trust it.

Units. The motors count motor rotations. We want wheel meters and module radians.

metersPerDriveMotorRotation = (π × wheelDiameterMeters) / driveGearRatio
radiansPerSteerMotorRotation = 2π / steerGearRatio

Example: 4 in wheel (0.1016 m), 6.75:1 drive → π × 0.1016 / 6.75 = 0.0473 m per motor rotation.

Theoretical top speed. A NEO spins about 5676 RPM with no load:

maxSpeed = (5676 / 60) × metersPerDriveMotorRotation

Work these out on paper for the simulated robot (Constants.Swerve) before writing code.

Code

Add to Constants.Swerve:

public static final double DRIVE_METERS_PER_MOTOR_ROTATION =
    Math.PI * WHEEL_DIAMETER_METERS / DRIVE_GEAR_RATIO;
public static final double NEO_FREE_SPEED_RPM = 5676;
public static final double MAX_THEORETICAL_SPEED_METERS_PER_SECOND =
    NEO_FREE_SPEED_RPM / 60.0 * DRIVE_METERS_PER_MOTOR_ROTATION;

// Flip one if that module's direction is backwards (see "Try it"). Order: FL, FR, BL, BR.
public static final boolean[] DRIVE_INVERTED = {false, false, false, false};
public static final boolean[] ABS_ENCODER_INVERTED = {false, false, false, false};

SwerveModule's constructor takes two more parameters, boolean driveInverted, boolean absEncoderInverted, and stores them. SwerveSubsystem passes Constants.Swerve.DRIVE_INVERTED[i] and Constants.Swerve.ABS_ENCODER_INVERTED[i].

In SwerveModule, add unit conversions and raw-voltage methods. Inversions live here and nowhere else:

public double getRawAngleRadians() {
  double fractionOfTurn = io.getAbsoluteEncoderVolts() / RobotController.getVoltage5V();
  if (absEncoderInverted) {
    fractionOfTurn = 1.0 - fractionOfTurn;   // count the other way around the circle
  }
  return fractionOfTurn * 2.0 * Math.PI;
}

private double driveSign() {
  return driveInverted ? -1.0 : 1.0;
}

/** Wheel distance traveled, meters. */
public double getDriveDistanceMeters() {
  return driveSign() * io.getDriveMotorRotations() * Constants.Swerve.DRIVE_METERS_PER_MOTOR_ROTATION;
}

/** Wheel speed, m/s. The motor reports RPM of the MOTOR shaft. */
public double getDriveVelocityMetersPerSecond() {
  return driveSign() * io.getDriveMotorRPM() / 60.0 * Constants.Swerve.DRIVE_METERS_PER_MOTOR_ROTATION;
}

/** Raw voltages, only for bring-up labs. */
public void setRawVoltages(double driveVolts, double steerVolts) {
  io.setDriveVoltage(driveSign() * driveVolts);
  io.setSteerVoltage(steerVolts);
}

public void stop() {
  io.setDriveVoltage(0);
  io.setSteerVoltage(0);
}

Log the new values in log() (DriveDistanceMeters, DriveVelocityMps).

Why voltage and not "percent output"? 50% of a fresh battery is ~6.3 V; 50% of a tired one is less. Asking for volts gives the same behavior every time, so gains we tune today still work next week.

In RobotContainer, drive one module (picked from the dashboard) with the sticks:

private final CommandXboxController controller = new CommandXboxController(0);

private void configureBindings() {
  swerve.setDefaultCommand(Commands.run(() -> {
    int index = (int) Tunable.number("Lab1/ModuleIndex", 0);   // 0=FL 1=FR 2=BL 3=BR
    double maxVolts = Tunable.number("Lab1/MaxVolts", 3.0);
    double drive = -controller.getLeftY() * maxVolts;    // stick up = forward
    double steer = -controller.getRightX() * maxVolts;   // stick left = CCW
    for (int i = 0; i < 4; i++) {
      if (i == index) {
        swerve.getModule(i).setRawVoltages(drive, steer);
      } else {
        swerve.getModule(i).stop();
      }
    }
  }, swerve));
}

Why the minus signs? Xbox sticks report up as negative Y and left as negative X. Our robot frame wants up = forward = +X and left = CCW = +θ.

Try it

  1. Enable Teleoperated in the Sim GUI.
  2. In AdvantageScope, open a Swerve tab and add Sim/TrueModuleStates: the simulator's view of where each wheel really points and how fast it really rolls.
  3. For each module, check:
    • Left stick up → the wheel's true speed is positive (rolling forward)?
    • Right stick left → module turns CCW and RawAngleDeg goes up?

With a clean simulator, everything should already be right.

Experiments: break it on purpose

Real robots arrive with wiring and mounting mistakes. The simulator can fake them: turn on a fault, find out what's wrong from the sensors, then fix it with an INVERTED constant.

Turn on (Tune/Sim/Faults/...) Predict What you'll see / how to fix
FL/DriveReversed Stick up on FL? The wheel's true speed is negative: it rolls backward, but DriveVelocityMps says positive. Our sensors fooled us! Fix: set FL's entry in DRIVE_INVERTED to true.
FL/EncoderReversed Turn CCW: angle goes...? Down. Fix: set FL's entry in ABS_ENCODER_INVERTED to true.
Turn both faults off again and set the constants back to false Leave the robot "clean" for the next lab

Units:

Change Predict What you'll see
Full stick up at 3 V, read DriveVelocityMps About 3/12 of MAX_THEORETICAL_SPEED A bit less: friction eats some voltage. This is kS (Lab 6).
Lab1/MaxVolts = 6 Velocity doubles? Roughly
Lab1/MaxVolts = 0.2, push the stick gently Does the wheel move at all? Below some voltage nothing moves. That threshold is kS. Write it down.
Hold the steer stick and watch RawAngleDeg A sawtooth (0→360→0) This wraparound is why we need continuous input in Lab 2

Checkpoint

Summary: One module moves in the right direction, and we can convert motor rotations to wheel meters and module radians.

Quiz

  1. Our drive conversion factor is ____ m per motor rotation. Show the math.
  2. Our theoretical top speed is ____ m/s.
  3. Why is the stick's Y negated?
  4. Why fix inversions in one place instead of with a minus sign in each later lab?
  5. Why command volts instead of percent output?

What could go wrong in the real world: a wrong gear ratio (odometry off by a constant factor); tread wear shrinking the wheel diameter; one module assembled differently from the other three.