In WPILib’s Command-Based template, Robot.java manages basic match mode transitions, while RobotContainer.java owns the actual robot architecture: instantiating subsystems, binding driver controls, and defining autonomous routines.
This lesson walks through wiring multiple subsystems and gamepads into a coherent, collision-safe RobotContainer.
1. Structure of RobotContainer
Section titled “1. Structure of RobotContainer”A clean RobotContainer separates responsibilities into four distinct phases:
- Subsystem Instantiation: Private final instances of all subsystems.
- Controller Instantiation: Gamepads for the driver (port 0) and operator (port 1).
- Default Commands: Behaviors that run whenever a subsystem is idle.
- Trigger Bindings: Connecting physical buttons and sensors to mechanism commands.
2. Complete RobotContainer Implementation
Section titled “2. Complete RobotContainer Implementation”package frc.robot;
import edu.wpi.first.wpilibj2.command.Command;import edu.wpi.first.wpilibj2.command.Commands;import edu.wpi.first.wpilibj2.command.button.CommandXboxController;import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.robot.subsystems.ArmSubsystem;import frc.robot.subsystems.IntakeSubsystem;import frc.robot.subsystems.CommandSwerveDrivetrain;
import static edu.wpi.first.units.Units.*;
public class RobotContainer { // 1. Hardware Subsystems private final ArmSubsystem m_arm = new ArmSubsystem(14, 24, "rio"); private final IntakeSubsystem m_intake = new IntakeSubsystem(15, "rio");
// 2. Controllers private final CommandXboxController m_driver = new CommandXboxController(0); private final CommandXboxController m_operator = new CommandXboxController(1);
// 3. Autonomous Chooser private final SendableChooser<Command> m_autoChooser = new SendableChooser<>();
public RobotContainer() { configureDefaultCommands(); configureBindings(); configureAutonomous(); }
/** Configures baseline behavior when no active command requires a subsystem. */ private void configureDefaultCommands() { // Arm defaults to holding its current position m_arm.setDefaultCommand(m_arm.holdPositionCommand()); }
/** Configures driver and operator gamepad trigger bindings. */ private void configureBindings() { // Operator Bumper: Run intake rollers while held; stop on release m_operator.rightBumper().whileTrue(m_intake.intakeCommand());
// Operator A: Stow Arm (0 degrees) m_operator.a().onTrue(m_arm.moveToAngleCommand(Degrees.of(0.0)));
// Operator Y: Score Arm (85 degrees) m_operator.y().onTrue(m_arm.moveToAngleCommand(Degrees.of(85.0)));
// Operator Left Trigger: Manual arm jog with 10% deadband m_operator.axisGreaterThan(1, 0.1).whileTrue( m_arm.manualJogCommand(() -> -m_operator.getLeftY()) ); }
/** Publishes autonomous routines to the dashboard. */ private void configureAutonomous() { m_autoChooser.setDefaultOption("Do Nothing", Commands.none()); m_autoChooser.addOption("Score and Stow", Commands.sequence( m_arm.moveToAngleCommand(Degrees.of(85.0)), m_intake.ejectCommand().withTimeout(Seconds.of(1.0)), m_arm.moveToAngleCommand(Degrees.of(0.0)) ) ); SmartDashboard.putData("Auto Mode", m_autoChooser); }
public Command getAutonomousCommand() { return m_autoChooser.getSelected(); }}3. Mechanism Safety Interlocks
Section titled “3. Mechanism Safety Interlocks”When multiple mechanisms share physical workspace (for example, an intake that swings through the path of a cascading elevator), software must prevent collisions:
// Example: Only allow intake to deploy if the arm is above 20 degreesCommand safeIntakeDeploy = Commands.either( m_intake.deployCommand(), Commands.print("WARNING: Cannot deploy intake while arm is below 20 deg!"), () -> m_arm.getAngle().in(Degrees) > 20.0);
m_operator.rightTrigger().onTrue(safeIntakeDeploy);Commands.either(trueCommand, falseCommand, conditionSupplier) checks the condition dynamically right before scheduling the action, preventing catastrophic mechanical collisions.
Keep Robot.java lightweight. All subsystem instantiations must reside inside RobotContainer. This prevents lifecycle bugs where subsystems attempt to talk to CAN devices before the roboRIO OS network interface is initialized.
Common Failure Modes
Section titled “Common Failure Modes”1. NullPointerException on Startup
Section titled “1. NullPointerException on Startup”- Symptom: Code crashes immediately upon boot with
NullPointerException in configureBindings(). - Cause: Calling a subsystem method in
configureBindings()before instantiating the subsystem field (order of declarations). - Fix: Ensure all
private final Subsystemfield declarations appear at the top of the class before the constructor runs.
2. Autonomous Chooser Empty in Driver Station
Section titled “2. Autonomous Chooser Empty in Driver Station”- Symptom: Dashboard displays no selectable options for autonomous.
- Cause: Forgot to call
SmartDashboard.putData("Auto Mode", m_autoChooser). - Fix: Verify the
putDataline exists inconfigureAutonomous().
Check Your Work
Section titled “Check Your Work”- Run
./gradlew buildin your terminal to verify zero compilation errors. - Launch simulation (F5).
- In the SimGUI NetworkTables view, verify:
Auto Modeoptions appear under SmartDashboard.- Pressing gamepad buttons schedules the corresponding commands without crashes.
Verified Sources & Standards
- WPILib RobotContainer Structure & Dependency Injectiondocs.wpilib.orgWPILib Standard