WPILibC++ 2024.3.2-10-g25ad6ea
frc2::CommandScheduler Class Referencefinal

The scheduler responsible for running Commands. More...

#include <frc2/command/CommandScheduler.h>

Inheritance diagram for frc2::CommandScheduler:
wpi::Sendable wpi::SendableHelper< CommandScheduler >

Public Types

using Action = std::function< void(const Command &)>
 
using InterruptAction = std::function< void(const Command &, const std::optional< Command * > &)>
 

Public Member Functions

 ~CommandScheduler () override
 
 CommandScheduler (const CommandScheduler &)=delete
 
CommandScheduleroperator= (const CommandScheduler &)=delete
 
void SetPeriod (units::second_t period)
 Changes the period of the loop overrun watchdog. More...
 
frc::EventLoopGetActiveButtonLoop () const
 Get the active button poll. More...
 
void SetActiveButtonLoop (frc::EventLoop *loop)
 Replace the button poll with another one. More...
 
frc::EventLoopGetDefaultButtonLoop () const
 Get the default button poll. More...
 
void Schedule (const CommandPtr &command)
 Schedules a command for execution. More...
 
void Schedule (Command *command)
 Schedules a command for execution. More...
 
void Schedule (std::span< Command *const > commands)
 Schedules multiple commands for execution. More...
 
void Schedule (std::initializer_list< Command * > commands)
 Schedules multiple commands for execution. More...
 
void Run ()
 Runs a single iteration of the scheduler. More...
 
void RegisterSubsystem (Subsystem *subsystem)
 Registers subsystems with the scheduler. More...
 
void UnregisterSubsystem (Subsystem *subsystem)
 Un-registers subsystems with the scheduler. More...
 
void RegisterSubsystem (std::initializer_list< Subsystem * > subsystems)
 
void RegisterSubsystem (std::span< Subsystem *const > subsystems)
 
void UnregisterSubsystem (std::initializer_list< Subsystem * > subsystems)
 
void UnregisterSubsystem (std::span< Subsystem *const > subsystems)
 
void UnregisterAllSubsystems ()
 Un-registers all registered Subsystems with the scheduler. More...
 
template<std::derived_from< Command > T>
void SetDefaultCommand (Subsystem *subsystem, T &&defaultCommand)
 Sets the default command for a subsystem. More...
 
void SetDefaultCommand (Subsystem *subsystem, CommandPtr &&defaultCommand)
 Sets the default command for a subsystem. More...
 
void RemoveDefaultCommand (Subsystem *subsystem)
 Removes the default command for a subsystem. More...
 
CommandGetDefaultCommand (const Subsystem *subsystem) const
 Gets the default command associated with this subsystem. More...
 
void Cancel (Command *command)
 Cancels commands. More...
 
void Cancel (const CommandPtr &command)
 Cancels commands. More...
 
void Cancel (std::span< Command *const > commands)
 Cancels commands. More...
 
void Cancel (std::initializer_list< Command * > commands)
 Cancels commands. More...
 
void CancelAll ()
 Cancels all commands that are currently scheduled. More...
 
bool IsScheduled (std::span< const Command *const > commands) const
 Whether the given commands are running. More...
 
bool IsScheduled (std::initializer_list< const Command * > commands) const
 Whether the given commands are running. More...
 
bool IsScheduled (const Command *command) const
 Whether a given command is running. More...
 
bool IsScheduled (const CommandPtr &command) const
 Whether a given command is running. More...
 
CommandRequiring (const Subsystem *subsystem) const
 Returns the command currently requiring a given subsystem. More...
 
void Disable ()
 Disables the command scheduler. More...
 
void Enable ()
 Enables the command scheduler. More...
 
void OnCommandInitialize (Action action)
 Adds an action to perform on the initialization of any command by the scheduler. More...
 
void OnCommandExecute (Action action)
 Adds an action to perform on the execution of any command by the scheduler. More...
 
void OnCommandInterrupt (Action action)
 Adds an action to perform on the interruption of any command by the scheduler. More...
 
void OnCommandInterrupt (InterruptAction action)
 Adds an action to perform on the interruption of any command by the scheduler. More...
 
void OnCommandFinish (Action action)
 Adds an action to perform on the finishing of any command by the scheduler. More...
 
void RequireUngrouped (const Command *command)
 Requires that the specified command hasn't already been added to a composition. More...
 
void RequireUngrouped (std::span< const std::unique_ptr< Command > > commands)
 Requires that the specified commands have not already been added to a composition. More...
 
void RequireUngrouped (std::initializer_list< const Command * > commands)
 Requires that the specified commands have not already been added to a composition. More...
 
void RequireUngroupedAndUnscheduled (const Command *command)
 Requires that the specified command has not already been added to a composition and is not currently scheduled. More...
 
void RequireUngroupedAndUnscheduled (std::span< const std::unique_ptr< Command > > commands)
 Requires that the specified commands have not already been added to a composition and are not currently scheduled. More...
 
void RequireUngroupedAndUnscheduled (std::initializer_list< const Command * > commands)
 Requires that the specified commands have not already been added to a composition and are not currently scheduled. More...
 
void InitSendable (wpi::SendableBuilder &builder) override
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from wpi::Sendable
virtual ~Sendable ()=default
 
virtual void InitSendable (SendableBuilder &builder)=0
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from wpi::SendableHelper< CommandScheduler >
 SendableHelper (const SendableHelper &rhs)=default
 
 SendableHelper (SendableHelper &&rhs)
 
SendableHelperoperator= (const SendableHelper &rhs)=default
 
SendableHelperoperator= (SendableHelper &&rhs)
 

Static Public Member Functions

static CommandSchedulerGetInstance ()
 Returns the Scheduler instance. More...
 

Friends

class CommandTestBase
 
template<typename T >
class CommandTestBaseWithParam
 

Additional Inherited Members

- Protected Member Functions inherited from wpi::SendableHelper< CommandScheduler >
 SendableHelper ()=default
 
 ~SendableHelper ()
 

Detailed Description

The scheduler responsible for running Commands.

A Command-based robot should call Run() on the singleton instance in its periodic block in order to run commands synchronously from the main loop. Subsystems should be registered with the scheduler using RegisterSubsystem() in order for their Periodic() methods to be called and for their default commands to be scheduled.

This class is provided by the NewCommands VendorDep

Member Typedef Documentation

◆ Action

using frc2::CommandScheduler::Action = std::function<void(const Command&)>

◆ InterruptAction

using frc2::CommandScheduler::InterruptAction = std::function<void(const Command&, const std::optional<Command*>&)>

Constructor & Destructor Documentation

◆ ~CommandScheduler()

frc2::CommandScheduler::~CommandScheduler ( )
override

◆ CommandScheduler()

frc2::CommandScheduler::CommandScheduler ( const CommandScheduler )
delete

Member Function Documentation

◆ Cancel() [1/4]

void frc2::CommandScheduler::Cancel ( Command command)

Cancels commands.

The scheduler will only call Command::End() method of the canceled command with true, indicating they were canceled (as opposed to finishing normally).

Commands will be canceled even if they are not scheduled as interruptible.

Parameters
commandthe command to cancel

◆ Cancel() [2/4]

void frc2::CommandScheduler::Cancel ( const CommandPtr command)

Cancels commands.

The scheduler will only call Command::End() method of the canceled command with true, indicating they were canceled (as opposed to finishing normally).

Commands will be canceled even if they are not scheduled as interruptible.

Parameters
commandthe command to cancel

◆ Cancel() [3/4]

void frc2::CommandScheduler::Cancel ( std::initializer_list< Command * >  commands)

Cancels commands.

The scheduler will only call Command::End() method of the canceled command with true, indicating they were canceled (as opposed to finishing normally).

Commands will be canceled even if they are not scheduled as interruptible.

Parameters
commandsthe commands to cancel

◆ Cancel() [4/4]

void frc2::CommandScheduler::Cancel ( std::span< Command *const >  commands)

Cancels commands.

The scheduler will only call Command::End() method of the canceled command with true, indicating they were canceled (as opposed to finishing normally).

Commands will be canceled even if they are not scheduled as interruptible.

Parameters
commandsthe commands to cancel

◆ CancelAll()

void frc2::CommandScheduler::CancelAll ( )

Cancels all commands that are currently scheduled.

◆ Disable()

void frc2::CommandScheduler::Disable ( )

Disables the command scheduler.

◆ Enable()

void frc2::CommandScheduler::Enable ( )

Enables the command scheduler.

◆ GetActiveButtonLoop()

frc::EventLoop * frc2::CommandScheduler::GetActiveButtonLoop ( ) const

Get the active button poll.

Returns
a reference to the current frc::EventLoop object polling buttons.

◆ GetDefaultButtonLoop()

frc::EventLoop * frc2::CommandScheduler::GetDefaultButtonLoop ( ) const

Get the default button poll.

Returns
a reference to the default frc::EventLoop object polling buttons.

◆ GetDefaultCommand()

Command * frc2::CommandScheduler::GetDefaultCommand ( const Subsystem subsystem) const

Gets the default command associated with this subsystem.

Null if this subsystem has no default command associated with it.

Parameters
subsystemthe subsystem to inquire about
Returns
the default command associated with the subsystem

◆ GetInstance()

static CommandScheduler & frc2::CommandScheduler::GetInstance ( )
static

Returns the Scheduler instance.

Returns
the instance

◆ InitSendable()

void frc2::CommandScheduler::InitSendable ( wpi::SendableBuilder builder)
overridevirtual

Initializes this Sendable object.

Parameters
buildersendable builder

Implements wpi::Sendable.

◆ IsScheduled() [1/4]

bool frc2::CommandScheduler::IsScheduled ( const Command command) const

Whether a given command is running.

Note that this only works on commands that are directly scheduled by the scheduler; it will not work on commands inside of CommandGroups, as the scheduler does not see them.

Parameters
commandthe command to query
Returns
whether the command is currently scheduled

◆ IsScheduled() [2/4]

bool frc2::CommandScheduler::IsScheduled ( const CommandPtr command) const

Whether a given command is running.

Note that this only works on commands that are directly scheduled by the scheduler; it will not work on commands inside of CommandGroups, as the scheduler does not see them.

Parameters
commandthe command to query
Returns
whether the command is currently scheduled

◆ IsScheduled() [3/4]

bool frc2::CommandScheduler::IsScheduled ( std::initializer_list< const Command * >  commands) const

Whether the given commands are running.

Note that this only works on commands that are directly scheduled by the scheduler; it will not work on commands inside of CommandGroups, as the scheduler does not see them.

Parameters
commandsthe command to query
Returns
whether the command is currently scheduled

◆ IsScheduled() [4/4]

bool frc2::CommandScheduler::IsScheduled ( std::span< const Command *const >  commands) const

Whether the given commands are running.

Note that this only works on commands that are directly scheduled by the scheduler; it will not work on commands inside of CommandGroups, as the scheduler does not see them.

Parameters
commandsthe command to query
Returns
whether the command is currently scheduled

◆ OnCommandExecute()

void frc2::CommandScheduler::OnCommandExecute ( Action  action)

Adds an action to perform on the execution of any command by the scheduler.

Parameters
actionthe action to perform

◆ OnCommandFinish()

void frc2::CommandScheduler::OnCommandFinish ( Action  action)

Adds an action to perform on the finishing of any command by the scheduler.

Parameters
actionthe action to perform

◆ OnCommandInitialize()

void frc2::CommandScheduler::OnCommandInitialize ( Action  action)

Adds an action to perform on the initialization of any command by the scheduler.

Parameters
actionthe action to perform

◆ OnCommandInterrupt() [1/2]

void frc2::CommandScheduler::OnCommandInterrupt ( Action  action)

Adds an action to perform on the interruption of any command by the scheduler.

Parameters
actionthe action to perform

◆ OnCommandInterrupt() [2/2]

void frc2::CommandScheduler::OnCommandInterrupt ( InterruptAction  action)

Adds an action to perform on the interruption of any command by the scheduler.

The action receives the interrupted command and an optional containing the interrupting command, or nullopt if it was not canceled by a command (e.g., by Cancel()).

Parameters
actionthe action to perform

◆ operator=()

CommandScheduler & frc2::CommandScheduler::operator= ( const CommandScheduler )
delete

◆ RegisterSubsystem() [1/3]

void frc2::CommandScheduler::RegisterSubsystem ( std::initializer_list< Subsystem * >  subsystems)

◆ RegisterSubsystem() [2/3]

void frc2::CommandScheduler::RegisterSubsystem ( std::span< Subsystem *const >  subsystems)

◆ RegisterSubsystem() [3/3]

void frc2::CommandScheduler::RegisterSubsystem ( Subsystem subsystem)

Registers subsystems with the scheduler.

This must be called for the subsystem's periodic block to run when the scheduler is run, and for the subsystem's default command to be scheduled. It is recommended to call this from the constructor of your subsystem implementations.

Parameters
subsystemthe subsystem to register

◆ RemoveDefaultCommand()

void frc2::CommandScheduler::RemoveDefaultCommand ( Subsystem subsystem)

Removes the default command for a subsystem.

The current default command will run until another command is scheduled that requires the subsystem, at which point the current default command will not be re-scheduled.

Parameters
subsystemthe subsystem whose default command will be removed

◆ RequireUngrouped() [1/3]

void frc2::CommandScheduler::RequireUngrouped ( const Command command)

Requires that the specified command hasn't already been added to a composition.

Parameters
commandThe command to check
Exceptions
ifthe given commands have already been composed.

◆ RequireUngrouped() [2/3]

void frc2::CommandScheduler::RequireUngrouped ( std::initializer_list< const Command * >  commands)

Requires that the specified commands have not already been added to a composition.

Parameters
commandsThe commands to check
Exceptions
IllegalArgumentExceptionif the given commands have already been composed.

◆ RequireUngrouped() [3/3]

void frc2::CommandScheduler::RequireUngrouped ( std::span< const std::unique_ptr< Command > >  commands)

Requires that the specified commands have not already been added to a composition.

Parameters
commandsThe commands to check
Exceptions
ifthe given commands have already been composed.

◆ RequireUngroupedAndUnscheduled() [1/3]

void frc2::CommandScheduler::RequireUngroupedAndUnscheduled ( const Command command)

Requires that the specified command has not already been added to a composition and is not currently scheduled.

Parameters
commandThe command to check
Exceptions
IllegalArgumentExceptionif the given command has already been composed or scheduled.

◆ RequireUngroupedAndUnscheduled() [2/3]

void frc2::CommandScheduler::RequireUngroupedAndUnscheduled ( std::initializer_list< const Command * >  commands)

Requires that the specified commands have not already been added to a composition and are not currently scheduled.

Parameters
commandsThe commands to check
Exceptions
IllegalArgumentExceptionif the given commands have already been composed or scheduled.

◆ RequireUngroupedAndUnscheduled() [3/3]

void frc2::CommandScheduler::RequireUngroupedAndUnscheduled ( std::span< const std::unique_ptr< Command > >  commands)

Requires that the specified commands have not already been added to a composition and are not currently scheduled.

Parameters
commandsThe commands to check
Exceptions
IllegalArgumentExceptionif the given commands have already been composed.

◆ Requiring()

Command * frc2::CommandScheduler::Requiring ( const Subsystem subsystem) const

Returns the command currently requiring a given subsystem.

Null if no command is currently requiring the subsystem

Parameters
subsystemthe subsystem to be inquired about
Returns
the command currently requiring the subsystem

◆ Run()

void frc2::CommandScheduler::Run ( )

Runs a single iteration of the scheduler.

The execution occurs in the following order:

Subsystem periodic methods are called.

Button bindings are polled, and new commands are scheduled from them.

Currently-scheduled commands are executed.

End conditions are checked on currently-scheduled commands, and commands that are finished have their end methods called and are removed.

Any subsystems not being used as requirements have their default methods started.

◆ Schedule() [1/4]

void frc2::CommandScheduler::Schedule ( Command command)

Schedules a command for execution.

Does nothing if the command is already scheduled. If a command's requirements are not available, it will only be started if all the commands currently using those requirements have been scheduled as interruptible. If this is the case, they will be interrupted and the command will be scheduled.

Parameters
commandthe command to schedule

◆ Schedule() [2/4]

void frc2::CommandScheduler::Schedule ( const CommandPtr command)

Schedules a command for execution.

Does nothing if the command is already scheduled. If a command's requirements are not available, it will only be started if all the commands currently using those requirements are interruptible. If this is the case, they will be interrupted and the command will be scheduled.

Parameters
commandthe command to schedule

◆ Schedule() [3/4]

void frc2::CommandScheduler::Schedule ( std::initializer_list< Command * >  commands)

Schedules multiple commands for execution.

Does nothing for commands already scheduled.

Parameters
commandsthe commands to schedule

◆ Schedule() [4/4]

void frc2::CommandScheduler::Schedule ( std::span< Command *const >  commands)

Schedules multiple commands for execution.

Does nothing for commands already scheduled.

Parameters
commandsthe commands to schedule

◆ SetActiveButtonLoop()

void frc2::CommandScheduler::SetActiveButtonLoop ( frc::EventLoop loop)

Replace the button poll with another one.

Parameters
loopthe new button polling loop object.

◆ SetDefaultCommand() [1/2]

void frc2::CommandScheduler::SetDefaultCommand ( Subsystem subsystem,
CommandPtr &&  defaultCommand 
)

Sets the default command for a subsystem.

Registers that subsystem if it is not already registered. Default commands will run whenever there is no other command currently scheduled that requires the subsystem. Default commands should be written to never end (i.e. their IsFinished() method should return false), as they would simply be re-scheduled if they do. Default commands must also require their subsystem.

Parameters
subsystemthe subsystem whose default command will be set
defaultCommandthe default command to associate with the subsystem

◆ SetDefaultCommand() [2/2]

template<std::derived_from< Command > T>
void frc2::CommandScheduler::SetDefaultCommand ( Subsystem subsystem,
T &&  defaultCommand 
)
inline

Sets the default command for a subsystem.

Registers that subsystem if it is not already registered. Default commands will run whenever there is no other command currently scheduled that requires the subsystem. Default commands should be written to never end (i.e. their IsFinished() method should return false), as they would simply be re-scheduled if they do. Default commands must also require their subsystem.

Parameters
subsystemthe subsystem whose default command will be set
defaultCommandthe default command to associate with the subsystem

◆ SetPeriod()

void frc2::CommandScheduler::SetPeriod ( units::second_t  period)

Changes the period of the loop overrun watchdog.

This should be kept in sync with the TimedRobot period.

◆ UnregisterAllSubsystems()

void frc2::CommandScheduler::UnregisterAllSubsystems ( )

Un-registers all registered Subsystems with the scheduler.

All currently registered subsystems will no longer have their periodic block called, and will not have their default command scheduled.

◆ UnregisterSubsystem() [1/3]

void frc2::CommandScheduler::UnregisterSubsystem ( std::initializer_list< Subsystem * >  subsystems)

◆ UnregisterSubsystem() [2/3]

void frc2::CommandScheduler::UnregisterSubsystem ( std::span< Subsystem *const >  subsystems)

◆ UnregisterSubsystem() [3/3]

void frc2::CommandScheduler::UnregisterSubsystem ( Subsystem subsystem)

Un-registers subsystems with the scheduler.

The subsystem will no longer have its periodic block called, and will not have its default command scheduled.

Parameters
subsystemthe subsystem to un-register

Friends And Related Function Documentation

◆ CommandTestBase

friend class CommandTestBase
friend

◆ CommandTestBaseWithParam

template<typename T >
friend class CommandTestBaseWithParam
friend

The documentation for this class was generated from the following file: