Command-Based Robot Structure
The command-based framework organizes code into:- Subsystems: Hardware components (drivetrain, elevator, etc.)
- Commands: Actions that use subsystems
- RobotContainer: Binds commands to buttons and manages subsystems
- Robot: Runs the CommandScheduler
Main Robot Class
- Java
- C++
RobotContainer Class
- Java
What This Example Demonstrates
CommandScheduler
The heart of the command-based framework. InrobotPeriodic():
- Polls all button bindings
- Runs all scheduled commands
- Calls
periodic()on all subsystems - Removes finished commands
- Handles command interruption
Default Commands
Subsystems can have a default command that runs when no other command is using the subsystem:Button Bindings
Bind commands to controller buttons:Command Factories
TheCommands class provides factories for common command patterns:
Commands.run(): Runs continuously (never ends)Commands.runOnce(): Runs once and immediately endsCommands.sequence(): Runs commands in orderCommands.parallel(): Runs commands simultaneouslyCommands.waitSeconds(): Waits for a duration
Subsystem Requirements
Commands declare which subsystems they use. The scheduler ensures only one command can use a subsystem at a time:Autonomous vs Teleop
autonomousInit(): Schedules the autonomous commandteleopInit(): Cancels autonomous, allowing default commands to run
Data Logging
Running in Simulation
- Deploy the code
- In autonomous: Watch the autonomous command execute
- In teleop:
- Joystick controls the drivetrain
- Circle button grabs hatch
- Square button releases hatch
- R1 button toggles half-speed mode
Source Location
- Java:
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hatchbotinlined/ - C++:
wpilibcExamples/src/main/cpp/examples/HatchbotInlined/