Skip to content

Advanced command-based programming is an invaluable tool for FRC teams aiming to orchestrate sophisticated autonomous and tele-operated behaviors. Delving beyond the basics, this lesson will unravel the nuances of command-grouping, command scheduling, triggers, dependencies, and command lifecycle management to maximize robot performance.

Command Groups

Command Groups are the linchpin for structuring multiple commands, be it running them sequentially or in parallel.

Sequencing Commands

Allows for a series of commands to be executed one after the other. The sequential group finishes after the last command in the sequence finishes. For smooth operation, each command within the sequence must conclude; otherwise, subsequent commands won't initiate.

import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;

public class DriveThenTurn extends SequentialCommandGroup {
    public DriveThenTurn(Drivetrain drivetrain) {
        addCommands(
            new DriveForward(drivetrain),
            new TurnRight(drivetrain)
        );
    }
}

Utilize the andThen() and beforeStarting() decorators to build a sequence composition using infix syntax.

new Command1().andThen(new Command2());

Parallelizing Commands

Commands can run simultaneously, bringing diverse actions to life at the same time. The group completes when the longest-running command finalizes. Care is required to ensure concurrent commands don't interfere with each other.

Make use of the alongWith() decorator for parallel command execution.

Example:

new Command1().alongWith(new Command2());

Using Conditions to Decide Command Flow

Introduce dynamic decisions in command execution based on real-time conditions. The command chosen depends on the outcome of the condition set.

The ConditionalCommand class provides this functionality.

Example:

new ConditionalCommand(new Command1(), new Command2(), () -> someCondition);

Command Scheduling

The Command Scheduler

Acting as the engine of the command-based framework, the Command Scheduler ensures smooth command initiation and termination. It runs in the backdrop, managing command processes seamlessly.

Scheduling Commands: Manually and through Triggers

Commands are scheduled either directly or via triggers such as joystick buttons. The scheduler's innate intelligence dictates the best initiation moment, guaranteeing precise command execution.

Example:

new JoystickButton(joystick, Button.kA.value).whenPressed(new Command1());

Triggers

Definition and Use Cases

Triggers are conditions or inputs that prompt command execution. Whether it's a joystick movement or a sensor reading, they offer dynamic control over the robot's actions.

Built-in Triggers in WPILib

WPILib offers a rich collection of triggers, such as joystick buttons, facilitating robot interactions.

Example:

new JoystickButton(joystick, Button.kB.value).toggleWhenPressed(new Command2());

Creating Custom Triggers

Tailor-make triggers for specific requirements, allowing deeper robot control and behavior intricacy.

Example:

new Trigger(() -> customCondition).whenActive(new Command1());

Command Dependencies

Understanding Command-Subsystem Dependencies Commands have associated subsystems denoting which robot components they control. This association guarantees mutual exclusivity, ensuring two commands can't conflict.

Ensuring Conflicting Commands Don't Run Together

Safety and predictability are paramount. The Command Scheduler prevents simultaneous execution of conflicting commands, avoiding potential disruptions.

Example:

new Command1().withRequirements(subsystem);

Command Lifecycle & Interrupting Commands

Lifecycle Phases

Commands undergo distinct phases: initialization, execution, and termination, each pivotal in understanding the command's life.

Interrupting Commands

There are moments when halting a command becomes crucial. The Command Scheduler provides the tools to safely interrupt any running command.

Example:

scheduler.cancel(new Command1());

Tasks

  1. Enhance the Drivetrain Subsystem: Integrate functionalities for turning.
  2. Design a Command Group: Sequentially move forward followed by a turn.
  3. Implement Joystick Controls: Integrate joystick inputs to navigate the robot.

Additional Resources

WPILib Command-Based User Guide

Sequencing and Parallelizing

Command Scheduler Documentation

Joysticks and Command-Based Programming

Creating Custom Triggers