In legacy FRC programming, teams often wrote large if / else statements inside teleopPeriodic() to check controller buttons every cycle. This led to complex state tracking and bugs where multiple buttons commanded conflicting motor voltages.
Modern WPILib uses Event-Driven Triggers. You declare relationships between input events and commands once during startup, and the CommandScheduler handles scheduling and cancellation automatically.
1. CommandXboxController
Section titled “1. CommandXboxController”WPILib provides the CommandXboxController class, which wraps physical controller buttons in reactive Trigger objects:
package frc.robot;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
public class RobotContainer { // Port 0 is typically the driver; Port 1 is the mechanism operator private final CommandXboxController m_driver = new CommandXboxController(0); private final CommandXboxController m_operator = new CommandXboxController(1);
private void configureBindings() { // Triggers are bound here during robot startup }}2. Trigger Binding Methods
Section titled “2. Trigger Binding Methods”A Trigger wraps a boolean condition evaluated every 20ms. Bind commands to triggers using four standard methods:
| Binding Method | Behavior | Typical Mechanism Use Case |
|---|---|---|
.onTrue(Command) | Schedules command on rising edge (0 -> 1). Command runs until its own isFinished() returns true. | Score preset, zero gyro heading, fire note. |
.whileTrue(Command) | Runs command while condition is true. Cancels command and calls end(true) when released. | Run intake rollers while holding bumper, manual arm jog. |
.onFalse(Command) | Schedules command on falling edge (1 -> 0). | Stowing mechanism when a button is released. |
.toggleOnTrue(Command) | Starts command on first press; cancels it on second press. | Toggling intake deploy/stow or limelight LED mode. |
Code Example:
Section titled “Code Example:”// Hold right bumper to run intake; release to stopm_operator.rightBumper().whileTrue(m_intake.intakeCommand());
// Tap 'A' button to move elevator to low scoring presetm_operator.a().onTrue(m_elevator.moveToHeightCommand(Inches.of(15.0)));
// Tap 'Y' button to move elevator to high scoring presetm_operator.y().onTrue(m_elevator.moveToHeightCommand(Inches.of(42.0)));3. Creating Custom Sensor Triggers
Section titled “3. Creating Custom Sensor Triggers”Triggers are not limited to physical gamepad buttons. Any Java BooleanSupplier can become a reactive trigger:
1. Beam-Break Detection Trigger
Section titled “1. Beam-Break Detection Trigger”Trigger an automated indexing routine the instant a game piece breaks an optical sensor:
Trigger gamePieceDetected = new Trigger(m_intake::isSensorTripped);
// When piece enters intake, automatically run feeder to index it into shootergamePieceDetected.onTrue(m_indexer.indexToShooterCommand());2. Motor Stator Current Spike Trigger
Section titled “2. Motor Stator Current Spike Trigger”Detect when a roller intake has captured a game piece by monitoring motor load:
Trigger rollerStall = new Trigger(() -> m_intakeMotor.getStatorCurrent().getValueAsDouble() > 35.0);
// Stop intake and rumble operator controller when piece is clampedrollerStall.onTrue( Commands.runOnce(() -> m_operator.getHID().setRumble(RumbleType.kBothRumble, 1.0)));3. Analog Trigger Deadbands
Section titled “3. Analog Trigger Deadbands”Convert analog joystick triggers or axes into digital triggers with a deadband threshold:
// Trigger fires when left analog trigger is pulled past 50%Trigger leftTriggerPulled = m_driver.leftTrigger(0.5);leftTriggerPulled.whileTrue(m_shooter.spinUpCommand());Common Failure Modes
Section titled “Common Failure Modes”1. Repeated Instantiation Inside Lambdas
Section titled “1. Repeated Instantiation Inside Lambdas”- Symptom: RoboRIO CPU utilization spikes, garbage collection pauses loop cycles.
- Cause: Creating
new Command()instances repeatedly inside an execution loop instead of binding once inconfigureBindings(). - Fix: Declare triggers and bind commands once inside
RobotContainer.configureBindings().
2. Button Press Triggers Nothing
Section titled “2. Button Press Triggers Nothing”- Symptom: Pressing gamepad buttons in teleop has no effect.
- Cause: The gamepad index in Driver Station does not match the port number in code (
new CommandXboxController(0)). - Fix: Verify in the Driver Station USB tab that your gamepad is dragged into slot 0.
Check Your Work
Section titled “Check Your Work”- In desktop simulation (SimGUI), open the Joysticks view.
- Click button 1 (‘A’) on the virtual joystick.
- Verify in the NetworkTables tree that the bound command transitions to scheduled and updates its subsystem.
Verified Sources & Standards
- WPILib Trigger Bindings & Event-Driven Controldocs.wpilib.orgWPILib Standard