RobotContainer & Bindings
Overview
RobotContainer is the central hub of command-based programming. It creates subsystems, configures default commands, and binds controller inputs to commands.RobotContainer Responsibilities
This class:
- Creates Subsystems: Instantiate all subsystem objects.
- Sets Default Commands: Define what happens when no command is running.
- Binds Controls: Connect buttons/triggers to commands.
- Provides Auto Commands: Return autonomous routines.
Basic RobotContainer
package frc.robot;
import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.subsystems.Intake;
public class RobotContainer {
private final Intake m_intake = new Intake();
private final CommandXboxController m_controller = new CommandXboxController(1);
public RobotContainer() {
m_intake.setDefaultCommand(new RunCommand(() -> m_intake.stop(), m_intake));
m_controller.a().whileTrue(new RunCommand(() -> m_intake.run(0.6), m_intake));
}
}Button Bindings
CommandXboxController provides trigger methods:whileTrue(): Run command while button held
whileFalse(): Run command while button not held
whenPressed(): Run command once when pressed
whenReleased(): Run command once when released
toggleOnTrue(): Toggle command on/off
Button Binding Examples
// Run intake while A button held
m_controller.a().whileTrue(new RunCommand(() -> m_intake.run(0.6), m_intake));
// Run intake reverse while B button held
m_controller.b().whileTrue(new RunCommand(() -> m_intake.run(-0.4), m_intake));
// Toggle intake on/off with X button
m_controller.x().toggleOnTrue(new RunCommand(() -> m_intake.run(0.6), m_intake));Default Commands
Default commands run when no other command requires the subsystem. Common examples: stop motors, maintain a position, or follow driver input. Set with
subsystem.setDefaultCommand(command).Default Commands in RobotContainer
package frc.robot;
import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.subsystems.Drivetrain;
import frc.robot.subsystems.Intake;
public class RobotContainer {
private final Drivetrain m_drivetrain = new Drivetrain();
private final Intake m_intake = new Intake();
private final CommandXboxController m_controller = new CommandXboxController(0);
public RobotContainer() {
// Default command: Stop intake when no other command is running
m_intake.setDefaultCommand(new RunCommand(() -> m_intake.stop(), m_intake));
// Default command: Drive with joystick input continuously
m_drivetrain.setDefaultCommand(new RunCommand(
() -> m_drivetrain.arcadeDrive(m_controller.getLeftY(), m_controller.getRightX()),
m_drivetrain
));
}
}