Gray Matter LogoGray Matter Workshop

Triggers

Triggers - Connecting User Input to Commands

Triggers link user inputs (buttons, joysticks, sensors) to commands. They define when and how commands should run based on controller input or robot state.

Key Concept: Use onTrue() to run commands when buttons are pressed, and onFalse() to run commands when buttons are released.

Trigger Implementation & Examples

🎯 Trigger Examples - Binding Input to Commands
RobotContainer.java - configureBindings()JAVA

🎮 onTrue() Trigger

Run a command once when a button is pressed or condition becomes true. The command completes its full lifecycle (initialize, execute, end).
controller.a()
  .onTrue(command);

🔽 onFalse() Trigger

Run a command once when a button is released or condition becomes false. Perfect for stopping motors or returning to safe positions.
controller.a()
  .onFalse(command);

🔄 Chaining Triggers

Chain onTrue() and onFalse() together to define different actions for press and release, giving you full control over button behavior.
.onTrue(cmd1)
.onFalse(cmd2);

🎯 Sensor Triggers

Triggers can be created from any boolean condition - sensors, limit switches, or custom logic - not just controller buttons.
new Trigger(
  () => sensor.get())
  .onTrue(cmd);

🔄 Before → After: Implementation

📋 Before

  • • Empty RobotContainer constructor
  • • No controller declared
  • • No configureBindings() method
  • • Commands exist but can't be triggered

✅ After

  • • CommandXboxController instantiated
  • • configureBindings() method created
  • • Button triggers bound to commands
  • • Robot responds to controller input

🎯 Final Implementation & GitHub Changes

Loading file...

🚀 Advanced Command Patterns

ℹ️ Advanced Topics - Beyond This Workshop

This workshop uses simplified patterns (runOnce(), onTrue(), and onFalse()) for easier learning. The examples below show advanced command patterns that are powerful for competition but not required for this workshop's scope.

💡 Feel free to explore these after completing the workshop fundamentals!

Extending WPILib Command

For more complex commands, extend Command directly instead of using inline methods.See WPILib documentation →

Complex Command Groups

Combine sequences and parallel actions to coordinate subsystems.

Composition Strategies

Use fluent helpers to assemble commands from smaller pieces.

Common Pitfalls

Always declare subsystem requirements to avoid unexpected conflicts.

Advanced Triggers

Create triggers from sensor conditions or button combinations.

Real-World Scenario

Combine patterns to build robust autonomous routines.