Skip to content
learnFRC.org
programminglesson35 min

RobotContainer Wiring & Default Commands

Central architecture in RobotContainer, default commands, mechanism safety interlocks, and subsystem dependency injection.

What you will learn

  • Structure RobotContainer with subsystem instances and controller ports.
  • Configure default commands for continuous subsystem behaviors.
  • Implement mechanism safety interlocks to prevent physical self-collisions.

Prerequisites:Writing Mechanism Commands

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.


A clean RobotContainer separates responsibilities into four distinct phases:

  1. Subsystem Instantiation: Private final instances of all subsystems.
  2. Controller Instantiation: Gamepads for the driver (port 0) and operator (port 1).
  3. Default Commands: Behaviors that run whenever a subsystem is idle.
  4. Trigger Bindings: Connecting physical buttons and sensors to mechanism commands.

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();
}
}

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 degrees
Command 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.

Never Instantiate Subsystems in Robot.java

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.


  • 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 Subsystem field 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 putData line exists in configureAutonomous().

  1. Run ./gradlew build in your terminal to verify zero compilation errors.
  2. Launch simulation (F5).
  3. In the SimGUI NetworkTables view, verify:
    • Auto Mode options appear under SmartDashboard.
    • Pressing gamepad buttons schedules the corresponding commands without crashes.

Verified Sources & Standards

Evergreen Standard Verified: Sep 25, 2026