Lab 0: Setup and a Tour of the Simulated Robot

Goal: run the robot code in the simulator, find every sensor value in AdvantageScope, and "turn the wheels by hand" to see what each sensor reports. Our code doesn't move anything yet.

Setup (once)

  1. Install the current WPILib release (VS Code + tools). It includes AdvantageScope and Elastic.
  2. Open the robot/ folder (not the repository root) in WPILib VS Code. It's a Command Robot project with REVLib installed and the Lab 0 code already in it.
  3. Command palette → WPILib: Simulate Robot Code → pick Sim GUI. A window opens with the robot state (Disabled / Autonomous / Teleoperated), joysticks, and NetworkTables.
  4. Open AdvantageScope → File → Connect to Simulator.
  5. Optional: plug an Xbox controller into the laptop. In the Sim GUI, drag it from System Joysticks to Joystick 0. (No controller? The Sim GUI can map keyboard keys to a joystick.)

Why: how the robot program runs

Robot.java            ← entry point; runs the scheduler every 20 ms
  └─ RobotContainer   ← creates subsystems, binds buttons to commands
       ├─ Subsystems  ← own hardware (motors, sensors)
       └─ Commands    ← actions that use subsystems

periodic() methods run 50 times per second. Anything slow in them makes the whole robot late.

Why: the simulated robot

robot/src/main/java/frc/robot/sim/ is a fake robot: four swerve modules with NEO motor physics, absolute encoders, a gyro, and the robot's true position on the field. Treat it like the real robot: our code may only talk to it through two interfaces.

our code (subsystems, commands)
        │  only through ModuleIO and GyroIO
        ▼
 ModuleIOSim / GyroIOSim  ── today: the simulator
 ModuleIOSparkMax         ── Lab 11: the real robot, same interface

That's a hardware-abstraction interface. Because our code only uses ModuleIO, the same code drives the real robot later.

The sensors (the same as on our real robot):

  • Motor encoders count motor rotations (position) and RPM (speed). They start at 0 every boot, so they can't tell you which way the wheel points.
  • Absolute encoders (like Thrifty encoders) output a voltage: about 0 V at one angle, rising to about 5 V just before a full turn. They do know the angle at power-up.
angle = (voltage / 5 V) × 2π radians

We divide by RobotController.getVoltage5V() instead of exactly 5.0. In sim it's exactly 5; on a real roboRIO it's often 4.9-something.

Code

This code is already in robot/src/main/java/frc/robot/. Read through it: every later lab builds on it.

  • Constants.java: the simulated robot's gear ratios, wheel size, and module positions, in module order FL, FR, BL, BR.
  • lib/Tunable.java: live dashboard knobs (see the Lab Toolkit).
  • subsystems/SwerveModule.java: one module. Today it only reads its ModuleIO and logs the values.
  • subsystems/SwerveSubsystem.java: creates the simulated world and four SwerveModules. Its simulationPeriodic() steps the physics every 20 ms.
/** Module angle straight from the absolute encoder, radians in [0, 2π). No offset yet. */
public double getRawAngleRadians() {
  double fractionOfTurn = io.getAbsoluteEncoderVolts() / RobotController.getVoltage5V();
  return fractionOfTurn * 2.0 * Math.PI;
}

Try it

  1. Start the simulation. Leave it Disabled.
  2. In AdvantageScope, drag SmartDashboard/Swerve/FL/RawAngleDeg onto a line graph.
  3. Turn a wheel "by hand": the simulator has a "hand" that pushes one module even while the robot is disabled. In Elastic (or the Sim GUI's NetworkTables view), set Tune/Sim/Hand/SteerVolts to 1. The FL module starts turning. Set it back to 0 to let go.
  4. Do the same with Tune/Sim/Hand/DriveVolts to roll the wheel. Tune/Sim/Hand/ModuleIndex picks the module (0 = FL, 1 = FR, 2 = BL, 3 = BR).

Experiments

Do this Predict first Then check
Turn FL with the hand until RawAngleDeg passes 360 What happens to RawAngleDeg? It jumps from ~360 back to 0. Why will that be a problem for a controller later?
Hand SteerVolts = +1 Does the module turn CCW or CW? Does the angle go up or down? Positive turns CCW (viewed from above) and the angle goes up. Lab 1 makes sure that stays true.
Before anything moves, read all four RawAngleDeg Every wheel starts pointing straight forward. Do they read 0? No: 37°, 212°, ... Each encoder was mounted at a random angle. Lab 3 fixes that.
Turn FL exactly one full module turn (watch RawAngleDeg come back to where it started) and read the change in SteerMotorRotations How many motor rotations per module turn? ~21.4. That's the steer gear ratio. Compare to Constants.
Hand DriveVolts = 1, then 2, then 4. Read the steady DriveMotorRPM each time Roughly proportional to voltage (minus a bit for friction). Motors are close to "speed ∝ voltage".
Hand SteerVolts = 0.1 Does it move? No: 0.1 V isn't enough to beat friction. Remember this for Lab 6.
Enable the robot (Teleoperated) Does anything change? No. Our code doesn't command the motors yet.

Checkpoint

Summary: We can run the simulator, find every sensor value, and know what units each one reports.

Quiz

  1. The drive motor turned 6.75 rotations. How far did the wheel travel? (You need the wheel diameter.)
  2. Why can't the motor encoder tell us which way the wheel points at power-up?
  3. Why divide by getVoltage5V() instead of 5.0?
  4. What happens to the absolute angle reading when the module crosses a full turn?
  5. Why does our code go through ModuleIO instead of talking to the simulator directly?

What could go wrong in the real world: a CAN ID set twice (two controllers fight; one disappears); an analog cable in the wrong port; firmware that doesn't match the REVLib version. (Lab 11 covers these.)