Phase 5: Four Modules + Teleop in Simulation¶
Goals¶
SwerveSubsystemwith four modules and the kinematics.TeleopSwervecommand: joystick → deadband → squared/cubed inputs → slew rate limiters → scale to max speeds →ChassisSpeeds.- Drive robot-relative first.
- Visualize module states in AdvantageScope's swerve view.
Theory: the joystick pipeline¶
raw stick ─► deadband ─► curve (square/cube, keep sign) ─► slew limiter ─► × max speed ─► ChassisSpeeds
- Deadband: sticks never rest at exactly 0. Ignore small values.
- Curve: squaring or cubing the input gives fine control at low speed while keeping full speed at full deflection. Keep the sign (
Math.copySign). - Slew rate limiter: limits how fast the command can change (units per second). It prevents wheel slip and tipping from instant acceleration.
- Scale: multiply by max linear speed (m/s) and max angular speed (rad/s).
Note
Joysticks report +Y = pushed back, +X = pushed right. WPILib's robot frame is +X forward, +Y left. Negate and swap axes accordingly. This is a classic source of bugs.
Build it¶
SwerveSubsystemholding fourSwerveModules in order FL, FR, BL, BR.drive(ChassisSpeeds)→ kinematics → desaturate → optimize → modulesetDesiredState.TeleopSwervewith suppliers for the three axes.- Log module states and chassis speeds; view them in AdvantageScope's Swerve tab.
Checkpoint¶
Summary: We can drive the simulated robot and the modules behave correctly.
Quiz
- Why use a deadband?
- Why does squaring the input help the driver?
- What does a slew rate limiter protect against?
- Which joystick axis maps to
vx, and with what sign? - Why drive robot-relative before field-relative?
What could go wrong in the real world: swapped or inverted axes; no deadband → slow creeping; limiters too slow → sluggish robot.