![]() |
WPILibC++ 2027.0.0-alpha-4
|
The scheduler responsible for running Commands. More...
#include <wpi/commands2/CommandScheduler.hpp>
Public Types | |
| using | Action = std::function<void(const Command&)> |
| using | InterruptAction |
Public Member Functions | |
| ~CommandScheduler () override | |
| CommandScheduler (const CommandScheduler &)=delete | |
| CommandScheduler & | operator= (const CommandScheduler &)=delete |
| void | SetPeriod (wpi::units::second_t period) |
| Changes the period of the loop overrun watchdog. | |
| wpi::EventLoop * | GetActiveButtonLoop () const |
| Get the active button poll. | |
| void | SetActiveButtonLoop (wpi::EventLoop *loop) |
| Replace the button poll with another one. | |
| wpi::EventLoop * | GetDefaultButtonLoop () const |
| Get the default button poll. | |
| void | Schedule (const CommandPtr &command) |
| Schedules a command for execution. | |
| void | Schedule (CommandPtr &&command) |
| Schedules a command for execution. | |
| void | Schedule (Command *command) |
| Schedules a command for execution. | |
| void | Schedule (std::span< Command *const > commands) |
| Schedules multiple commands for execution. | |
| void | Schedule (std::initializer_list< Command * > commands) |
| Schedules multiple commands for execution. | |
| void | Run () |
| Runs a single iteration of the scheduler. | |
| void | RegisterSubsystem (Subsystem *subsystem) |
| Registers subsystems with the scheduler. | |
| void | UnregisterSubsystem (Subsystem *subsystem) |
| Un-registers subsystems with the scheduler. | |
| 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. | |
| template<std::derived_from< Command > T> | |
| void | SetDefaultCommand (Subsystem *subsystem, T &&defaultCommand) |
| Sets the default command for a subsystem. | |
| void | SetDefaultCommand (Subsystem *subsystem, CommandPtr &&defaultCommand) |
| Sets the default command for a subsystem. | |
| void | RemoveDefaultCommand (Subsystem *subsystem) |
| Removes the default command for a subsystem. | |
| Command * | GetDefaultCommand (const Subsystem *subsystem) const |
| Gets the default command associated with this subsystem. | |
| void | Cancel (Command *command) |
| Cancels commands. | |
| void | Cancel (const CommandPtr &command) |
| Cancels commands. | |
| void | Cancel (std::span< Command *const > commands) |
| Cancels commands. | |
| void | Cancel (std::initializer_list< Command * > commands) |
| Cancels commands. | |
| void | CancelAll () |
| Cancels all commands that are currently scheduled. | |
| bool | IsScheduled (std::span< const Command *const > commands) const |
| Whether the given commands are running. | |
| bool | IsScheduled (std::initializer_list< const Command * > commands) const |
| Whether the given commands are running. | |
| bool | IsScheduled (const Command *command) const |
| Whether a given command is running. | |
| bool | IsScheduled (const CommandPtr &command) const |
| Whether a given command is running. | |
| Command * | Requiring (const Subsystem *subsystem) const |
| Returns the command currently requiring a given subsystem. | |
| void | Disable () |
| Disables the command scheduler. | |
| void | Enable () |
| Enables the command scheduler. | |
| void | PrintWatchdogEpochs () |
| Prints list of epochs added so far and their times. | |
| void | OnCommandInitialize (Action action) |
| Adds an action to perform on the initialization of any command by the scheduler. | |
| void | OnCommandExecute (Action action) |
| Adds an action to perform on the execution of any command by the scheduler. | |
| void | OnCommandInterrupt (Action action) |
| Adds an action to perform on the interruption of any command by the scheduler. | |
| void | OnCommandInterrupt (InterruptAction action) |
| Adds an action to perform on the interruption of any command by the scheduler. | |
| void | OnCommandFinish (Action action) |
| Adds an action to perform on the finishing of any command by the scheduler. | |
| void | RequireUngrouped (const Command *command) |
| Requires that the specified command hasn't already been added to a composition. | |
| void | RequireUngrouped (std::span< const std::unique_ptr< Command > > commands) |
| Requires that the specified commands have not already been added to a composition. | |
| void | RequireUngrouped (std::initializer_list< const Command * > commands) |
| Requires that the specified commands have not already been added to a composition. | |
| void | RequireUngroupedAndUnscheduled (const Command *command) |
| Requires that the specified command has not already been added to a composition and is not currently scheduled. | |
| 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. | |
| 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. | |
| void | InitSendable (wpi::util::SendableBuilder &builder) override |
| Initializes this Sendable object. | |
| Public Member Functions inherited from wpi::util::Sendable | |
| virtual constexpr | ~Sendable ()=default |
| Public Member Functions inherited from wpi::util::SendableHelper< CommandScheduler > | |
| constexpr | SendableHelper (const SendableHelper &rhs)=default |
| constexpr SendableHelper & | operator= (const SendableHelper &rhs)=default |
Static Public Member Functions | |
| static CommandScheduler & | GetInstance () |
| Returns the Scheduler instance. | |
Friends | |
| class | CommandTestBase |
| template<typename T> | |
| class | CommandTestBaseWithParam |
Additional Inherited Members | |
| Protected Member Functions inherited from wpi::util::SendableHelper< CommandScheduler > | |
| constexpr | ~SendableHelper () |
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
| using wpi::cmd::CommandScheduler::Action = std::function<void(const Command&)> |
|
override |
|
delete |
| void wpi::cmd::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.
| command | the command to cancel |
| void wpi::cmd::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.
| command | the command to cancel |
| void wpi::cmd::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.
| commands | the commands to cancel |
| void wpi::cmd::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.
| commands | the commands to cancel |
| void wpi::cmd::CommandScheduler::CancelAll | ( | ) |
Cancels all commands that are currently scheduled.
| void wpi::cmd::CommandScheduler::Disable | ( | ) |
Disables the command scheduler.
| void wpi::cmd::CommandScheduler::Enable | ( | ) |
Enables the command scheduler.
| wpi::EventLoop * wpi::cmd::CommandScheduler::GetActiveButtonLoop | ( | ) | const |
Get the active button poll.
wpi::EventLoop object polling buttons. | wpi::EventLoop * wpi::cmd::CommandScheduler::GetDefaultButtonLoop | ( | ) | const |
Get the default button poll.
wpi::EventLoop object polling buttons. Gets the default command associated with this subsystem.
Null if this subsystem has no default command associated with it.
| subsystem | the subsystem to inquire about |
|
static |
Returns the Scheduler instance.
|
overridevirtual |
Initializes this Sendable object.
| builder | sendable builder |
Implements wpi::util::Sendable.
| bool wpi::cmd::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.
| command | the command to query |
| bool wpi::cmd::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.
| command | the command to query |
| bool wpi::cmd::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.
| commands | the command to query |
| bool wpi::cmd::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.
| commands | the command to query |
| void wpi::cmd::CommandScheduler::OnCommandExecute | ( | Action | action | ) |
Adds an action to perform on the execution of any command by the scheduler.
| action | the action to perform |
| void wpi::cmd::CommandScheduler::OnCommandFinish | ( | Action | action | ) |
Adds an action to perform on the finishing of any command by the scheduler.
| action | the action to perform |
| void wpi::cmd::CommandScheduler::OnCommandInitialize | ( | Action | action | ) |
Adds an action to perform on the initialization of any command by the scheduler.
| action | the action to perform |
| void wpi::cmd::CommandScheduler::OnCommandInterrupt | ( | Action | action | ) |
Adds an action to perform on the interruption of any command by the scheduler.
| action | the action to perform |
| void wpi::cmd::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()).
| action | the action to perform |
|
delete |
| void wpi::cmd::CommandScheduler::PrintWatchdogEpochs | ( | ) |
Prints list of epochs added so far and their times.
| void wpi::cmd::CommandScheduler::RegisterSubsystem | ( | std::initializer_list< Subsystem * > | subsystems | ) |
| void wpi::cmd::CommandScheduler::RegisterSubsystem | ( | std::span< Subsystem *const > | subsystems | ) |
| void wpi::cmd::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.
| subsystem | the subsystem to register |
| void wpi::cmd::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.
| subsystem | the subsystem whose default command will be removed |
| void wpi::cmd::CommandScheduler::RequireUngrouped | ( | const Command * | command | ) |
Requires that the specified command hasn't already been added to a composition.
| command | The command to check |
| void wpi::cmd::CommandScheduler::RequireUngrouped | ( | std::initializer_list< const Command * > | commands | ) |
Requires that the specified commands have not already been added to a composition.
| commands | The commands to check |
| void wpi::cmd::CommandScheduler::RequireUngrouped | ( | std::span< const std::unique_ptr< Command > > | commands | ) |
Requires that the specified commands have not already been added to a composition.
| commands | The commands to check |
| void wpi::cmd::CommandScheduler::RequireUngroupedAndUnscheduled | ( | const Command * | command | ) |
Requires that the specified command has not already been added to a composition and is not currently scheduled.
| command | The command to check |
| void wpi::cmd::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.
| commands | The commands to check |
| void wpi::cmd::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.
| commands | The commands to check |
Returns the command currently requiring a given subsystem.
Null if no command is currently requiring the subsystem
| subsystem | the subsystem to be inquired about |
| void wpi::cmd::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.
| void wpi::cmd::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.
The pointer must remain valid through the entire lifecycle of the command.
| command | the command to schedule |
| void wpi::cmd::CommandScheduler::Schedule | ( | 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.
| command | the command to schedule |
| void wpi::cmd::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.
| command | the command to schedule |
| void wpi::cmd::CommandScheduler::Schedule | ( | std::initializer_list< Command * > | commands | ) |
Schedules multiple commands for execution.
Does nothing for commands already scheduled.
| commands | the commands to schedule |
| void wpi::cmd::CommandScheduler::Schedule | ( | std::span< Command *const > | commands | ) |
Schedules multiple commands for execution.
Does nothing for commands already scheduled.
| commands | the commands to schedule |
| void wpi::cmd::CommandScheduler::SetActiveButtonLoop | ( | wpi::EventLoop * | loop | ) |
Replace the button poll with another one.
| loop | the new button polling loop object. |
| void wpi::cmd::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.
| subsystem | the subsystem whose default command will be set |
| defaultCommand | the default command to associate with the subsystem |
|
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.
| subsystem | the subsystem whose default command will be set |
| defaultCommand | the default command to associate with the subsystem |
| void wpi::cmd::CommandScheduler::SetPeriod | ( | wpi::units::second_t | period | ) |
Changes the period of the loop overrun watchdog.
This should be kept in sync with the TimedRobot period.
| void wpi::cmd::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.
| void wpi::cmd::CommandScheduler::UnregisterSubsystem | ( | std::initializer_list< Subsystem * > | subsystems | ) |
| void wpi::cmd::CommandScheduler::UnregisterSubsystem | ( | std::span< Subsystem *const > | subsystems | ) |
| void wpi::cmd::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.
| subsystem | the subsystem to un-register |
|
friend |
|
friend |