Unit 6: Intermediate FRC Coding
Welcome to the framework that brings it all together. The Command-Based model is the official, modern, and powerful way to structure robot code for clarity, reliability, and scalability. This is the standard we use on FRC 2910.
Imagine trying to code a full competition robot inside a single `Robot.java` file. It would become a tangled mess, impossible to debug or for multiple programmers to work on. The Command-Based model solves this by enforcing a strict separation of concerns.
The entire framework is built on this simple separation:
A Command uses one or more Subsystems to accomplish its goal.
You'll work with three main components in a Command-Based project.
A subsystem class is a container for all the hardware (motors, sensors) related to a single mechanism. Its job is to provide simple methods to control its hardware. A subsystem should never know about joysticks or buttons.
public class IntakeSubsystem extends SubsystemBase {
private final Spark m_intakeMotor = new Spark(5);
public void setMotorSpeed(double speed) {
m_intakeMotor.set(speed);
}
}
A command class defines a single, self-contained action. This is where your logic, like state machines, will live. A command "requires" the subsystem(s) it needs, which prevents two commands from controlling the same mechanism at once.
public class IntakeCommand extends Command {
private final IntakeSubsystem m_intake;
public IntakeCommand(IntakeSubsystem intake) {
m_intake = intake;
addRequirements(m_intake); // Declare that this command uses the intake
}
@Override
public void execute() {
m_intake.setMotorSpeed(0.8);
}
// ... other command methods ...
}
This class is the central hub where everything is put together. Its job is to create instances of your subsystems and controllers, and most importantly, to bind commands to controller buttons.
public class RobotContainer {
// Create subsystems
private final IntakeSubsystem m_intake = new IntakeSubsystem();
// Create controllers
private final XboxController m_operatorController = new XboxController(1);
public RobotContainer() {
configureButtonBindings();
}
private void configureButtonBindings() {
// When the 'A' button is held, run a new IntakeCommand
new JoystickButton(m_operatorController, XboxController.Button.kA.value)
.whileTrue(new IntakeCommand(m_intake));
}
}
Question: In the Command-Based model, which component is responsible for creating the motor controller objects for the intake mechanism?