Session 4: Controllers, Commands, and Simulation¶
Today the robot moves, on screen. You write the math that turns a controller stick into a motor speed, write the command that drives the Romi, bind buttons to behaviors, and run it all in the WPILib simulator on a programming laptop.
๐ฏ Objectives¶
By the end of this session you will be able to:
- Trace a stick movement from the controller, through
RobotContainer, a command, and a subsystem, to a motor. - Write a method that takes a parameter and returns a value, and test it.
- Write a command class with a constructor, attributes,
execute(),end(), andisFinished(). - Bind a controller button to a command, and explain what a lambda is.
- Run the robot program in the WPILib simulator, switch modes, and watch motor outputs and dashboard values change.
โ Before you arrive¶
- Session 3's branch is pushed. Today starts from a fresh
main, which already has Sessions 2 and 3 in it. - Mentors:
solution/session-3is merged intomain. - A programming laptop with WPILib 2026 installed, the repository cloned, and an Xbox controller. Mentors set these up. You will share them.
๐๏ธ Agenda¶
| Time | Activity | Page |
|---|---|---|
| 0:00 | Recap: subsystems, commands, scheduler. Questions | |
| 0:05 | The routine in your Codespace: refresh main, branch yourname/session-4 |
The Routine |
| 0:10 | Controllers to motors: the end-to-end path; PWM Spark vs SPARK MAX | Controllers to Motors |
| 0:20 | Methods with parameters, lambdas, constants | Methods and Lambdas |
| 0:30 | Exercise part 1: DriveInput passes its tests |
Exercise: Drive Input |
| 0:40 | Exercise part 2: ArcadeDriveCommand and button bindings |
Exercise: Arcade Drive |
| 0:55 | Push, then simulate on a laptop | Simulate It |
| 1:15 | Wrap up: what Session 5 adds. The challenge is for home | Challenge: Turn Around |
Mentor pacing note
Part 1 is small on purpose: three methods, tests already written. Keep it to ten minutes so the command and the simulator get the time. If laptops are scarce, have pairs push from their Codespaces and take turns at a laptop while others finish.
๐งช Exercises¶
- The Routine - refresh
mainand createyourname/session-4. - Exercise: Drive Input - deadband and sign math, tested.
- Exercise: Arcade Drive - the command, the default command, two button bindings, dashboard values.
- Simulate It - run it on a laptop with a controller.
๐ Challenge (optional)¶
Turn Around - one button press spins the Romi 180 degrees and stops on its own. A command that finishes, with its numbers in Constants.
๐ Reference¶
- Session 4 Reference - controller map,
Triggermethods, Sim GUI windows. - Java Cheat Sheet - now with methods, lambdas, and constants.
โญ๏ธ Next session¶
Session 5 adds sensors: encoders and the gyro tell the robot where it is, the numbers go to AdvantageScope, and the code finally runs on the real Romi.