Unit 6: Intermediate FRC Coding
The Operator Interface (OI) is where we bridge the gap between human input and robot action. In the Command-Based framework, we centralize all our button, joystick, and trigger logic in one place to create a clean and organized control system.
In a modern command-based project, the `RobotContainer.java` class is the designated home for the OI. This class is responsible for creating instances of all your robot's subsystems and controllers and, most importantly, mapping driver inputs to specific commands.
This special method inside `RobotContainer` is where all the magic happens. Its single, crucial job is to define what happens when a button is pressed, a joystick is moved, or a trigger is pulled. This keeps all control logic in one easy-to-find location.
WPILib provides powerful tools to make button binding simple and intuitive. The primary object you'll work with is a `Trigger`, which represents a condition (like a button press). You then "bind" a `Command` to that `Trigger`.
Once you have a `Trigger` object (like a `JoystickButton`), you can call methods on it to define when a command should run:
Let's create a binding that runs our `IntakeCommand` while the 'A' button on an Xbox controller is held down.
public class RobotContainer {
// 1. Create instances of your subsystems and controllers
private final IntakeSubsystem m_intakeSubsystem = new IntakeSubsystem();
private final XboxController m_operatorController = new XboxController(1); // Port 1
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
// 2. Call the binding method from the constructor
configureButtonBindings();
}
/**
* Use this method to define your button->command mappings.
*/
private void configureButtonBindings() {
// 3. Create a Trigger and bind a Command to it
// This line reads: "Create a trigger for the 'A' button on the operator controller.
// While that trigger is true (held down), run a new IntakeCommand."
new JoystickButton(m_operatorController, XboxController.Button.kA.value)
.whileTrue(new IntakeCommand(m_intakeSubsystem));
}
}
Question: In a command-based project, which class is the single, centralized place for defining all your button-to-command mappings?