Commands
Commands - Coordinating Robot Actions
Commands are the "actions" that your robot performs. They use subsystems to accomplish tasks and can be triggered by user input, sensors, or automated sequences.
🎯 Key Concept: Commands tell subsystems what action to run.
Command Structure & Examples
🎮 Inline Command Methods Example
Subsystem Command MethodsJAVA
1// In your Arm subsystem - add these command methods:
2
3public Command moveUp() {
4 return startEnd(() -> setVoltage(6), () -> stop());
5}
6
7public Command moveDown() {
8 return startEnd(() -> setVoltage(-6), () -> stop());
9}
10
11public Command stopArm() {
12 return runOnce(() -> stop());
13}
14
15
🎯 Trigger Examples - Binding Input to Commands
RobotContainer.java - configureBindings()JAVA
1package frc.robot;
2
3import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
4import frc.robot.subsystems.Arm;
5
6public class RobotContainer {
7 // Hardware - controllers and subsystems
8 private final CommandXboxController controller = new CommandXboxController(0);
9 private final Arm armSubsystem = new Arm();
10
11 public RobotContainer() {
12 configureBindings();
13 }
14
15 private void configureBindings() {
16 // 🎮 BUTTON TRIGGERS - Run command while button is held
17 controller.a().whileTrue(armSubsystem.moveUp());
18
19 // 🔄 BUTTON TRIGGERS - Run command while button is held
20 controller.b().whileTrue(armSubsystem.moveDown());
21 }
22}
🏠 Default Commands
Default commands run when no other command is using the subsystem. They are set in the subsystem constructor.
setDefaultCommand(stopCommand());
🎮 Trigger Types
Different trigger types for different behaviors: onTrue (once), whileTrue (continuous), toggleOnTrue (toggle).
controller.a().whileTrue(command);
🚀 Motor Configuration
Motor configuration code should be wrapped properly to fit in configuration sections.
motor.getConfigurator()
.apply(config);
Workshop Implementation
🔄 Before → After: Implementation
📋 Before
- • Arm subsystem with basic voltage control
- • No user input integration
- • No commands to coordinate actions
- • Manual method calls only
✅ After
- • Enhanced Arm subsystem methods
- • Xbox controller integration
- • Commands for moveUp(), moveDown()
- • RobotContainer with proper binding
- • Default command for safety
Loading file...
🔍 Code Walkthrough
New Subsystem Methods:
- • moveUp(): Positive voltage for upward movement
- • moveDown(): Negative voltage for downward movement
Command Integration:
- • RobotContainer: Xbox controller instantiation
- • Button Bindings: A/B buttons control arm direction
- • Default Command: Stop arm when no input
💡 Next Step: Our Arm now responds to user input! Next, we'll verify mechanism setup before implementing precise PID position control.
🤖 RobotContainer Implementation
Loading file...