Command

enum class CommandCancelBehavior

Enum for different cancel behaviors for Commands.

Cancel Incoming causes newly scheduled commands to fail, keeping the current command running until it ends uninterrupted. Cancel Running caused the currently running command to yield to the newly scheduled command and end interrupted.

Values:

enumerator CancelIncoming

Causes the newly scheduled command(s) to fail upon attempts to schedule them while a command with CancelRunning is reserving the subsystem.

enumerator CancelRunning

Causes the currently running command to yield to the newly scheduled command upon scheduling.

class Command

Abstract Command class for commands.

Commands are the base of the command based structure, they compartmentalize robot behaviors and provide the structure necessary to ensure that multiple commands are attempting to access the same hardware at the same time. Generally in projects you can use the classes such as FunctionalCommand, or RunCommand for simple Command behaviors, and you can override this class to create more complex behaviors

Subclassed by ConditionalCommand, FunctionalCommand, ParallelCommandGroup, ParallelRaceGroup, ProxyCommand, RepeatCommand, Sequence, WaitCommand

Public Functions

inline virtual void initialize()

Called before every time the command is used. Users can override this to create starting behaviors for custom commands.

void initialize() override {
   // Initialization tasks here, for example setting motor position PID commands
   motor.move_absolute(5, 200);
}
inline virtual void execute()

Execute is run every frame in the command scheduler(10ms/100Hz or user selected) This can be useful for updating feedback controllers such as PID or Pure Pursuit.

// Example override case for PID control
void execute() override {
    motor.move_voltage(pid.update(motor.get_position()));
}
inline virtual bool isFinished()

Command can override this function to have custom ending behaviors.

Returns:

true if the Command is finished, false if it is still running

inline virtual void end(bool interrupted)

Stops the currently running command, this can be used to clean up resources or finish up certain behaviors.

Parameters:

interrupted – true if the command was stopped before isFinished returned true, false otherwise

inline virtual std::vector<Subsystem*> getRequirements()

This function returns the necessary subsystems needed to do run command. The scheduler will work to free these requirements before running the behavior to ensure no tasks overlap.

Warning

You must ensure all subsystems neccesary are captured by this function or else multiple commands will overlap and run at the same time

Returns:

A vector of all the subsystem requirements for this class

inline virtual CommandCancelBehavior getCancelBehavior()

Returns the cancel behavior for this class, defaults to CommandCancelBehavior::CancelRunning.

Returns:

Desired CommandCancelBehavior for the class

inline void schedule()

Schedule this command with the CommandScheduler.

// Declare command(example)
Command* command;

// Use shorthand to schedule the command
command->schedule();

// command is now scheduled
inline void cancel()

Cancel this command if it is currently scheduled in the CommandScheduler.

// Declare command(example)
Command* command;

// Use shorthand to schedule the command
command->schedule();

// command is now scheduled

// Cancel the command
command->cancel();

// command is no longer running
inline bool scheduled() const

See if the command is currently scheduled in the CommandScheduler.

// Declare command(example)
Command* command;

// Use shorthand to schedule the command
command->schedule();

// command is now scheduled
assert(command->scheduled() == true);

// Cancel the command
command->cancel();

// command is no longer running
assert(command->scheduled() == false);
inline Command *andThen(Command *other)

Create a Sequence with 2 commands.

Parameters:

other – The command to run after the current command

Returns:

A Sequence with this running first and other running after

inline Command *withTimeout(units::QTime duration)

Make a timeout on this command.

Parameters:

duration – The maximum running duration of the Command

Returns:

ParallelRaceGroup with this and WaitCommand of the desired duration

inline Command *until(const std::function<bool()> &isFinish)

Run the command until a condition is met.

Parameters:

isFinish – When this condition returns true the command will stop

Returns:

ParallelRaceGroup with this and WaitUntilCommand with the desired isFinish

inline Command *with(Command *other)

Create a ParallelCommandGroup with this and other.

Parameters:

other – Other command for the ParallelCommandGroup

Returns:

ParallelCommandGroup with this and other

inline Command *race(Command *other)

Create a ParallelRaceGroup with this and other.

Parameters:

other – Other command for the ParallelRaceGroup

Returns:

ParallelRaceGroup with this and other

inline Command *repeatedly()

Create a RepeatCommand with this.

Returns:

RepeatCommand with this

inline Command *asProxy()

Create a ProxyCommand with this.

Warning

Only use ProxyCommand where ABSOLUTELY necessary, it can have unintended side effects. The side effects are listed in the class documentation

Returns:

ProxyCommand with this