WPILibC++  2020.3.2-60-g3011ebe
frc2::CommandScheduler Class Referencefinal

The scheduler responsible for running Commands. More...

#include <CommandScheduler.h>

Inheritance diagram for frc2::CommandScheduler:
frc::Sendable frc::ErrorBase frc::SendableHelper< CommandScheduler >

Public Types

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

Public Member Functions

 CommandScheduler (const CommandScheduler &)=delete
 
CommandScheduleroperator= (const CommandScheduler &)=delete
 
void SetPeriod (units::second_t period)
 Changes the period of the loop overrun watchdog. More...
 
void AddButton (wpi::unique_function< void()> button)
 Adds a button binding to the scheduler, which will be polled to schedule commands. More...
 
void ClearButtons ()
 Removes all button bindings from the scheduler.
 
void Schedule (bool interruptible, Command *command)
 Schedules a command for execution. More...
 
void Schedule (Command *command)
 Schedules a command for execution, with interruptible defaulted to true. More...
 
void Schedule (bool interruptible, wpi::ArrayRef< Command * > commands)
 Schedules multiple commands for execution. More...
 
void Schedule (bool interruptible, std::initializer_list< Command * > commands)
 Schedules multiple commands for execution. More...
 
void Schedule (wpi::ArrayRef< Command * > commands)
 Schedules multiple commands for execution, with interruptible defaulted to true. More...
 
void Schedule (std::initializer_list< Command * > commands)
 Schedules multiple commands for execution, with interruptible defaulted to true. 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 (wpi::ArrayRef< Subsystem * > subsystems)
 
void UnregisterSubsystem (std::initializer_list< Subsystem * > subsystems)
 
void UnregisterSubsystem (wpi::ArrayRef< Subsystem * > subsystems)
 
template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
void SetDefaultCommand (Subsystem *subsystem, T &&defaultCommand)
 Sets 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 a command. More...
 
void Cancel (wpi::ArrayRef< Command * > commands)
 Cancels commands. More...
 
void Cancel (std::initializer_list< Command * > commands)
 Cancels commands. More...
 
void CancelAll ()
 Cancels all commands that are currently scheduled.
 
double TimeSinceScheduled (const Command *command) const
 Returns the time since a given command was scheduled. More...
 
bool IsScheduled (wpi::ArrayRef< const Command * > 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...
 
CommandRequiring (const Subsystem *subsystem) const
 Returns the command currently requiring a given subsystem. More...
 
void Disable ()
 Disables the command scheduler.
 
void Enable ()
 Enables the command scheduler.
 
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 OnCommandFinish (Action action)
 Adds an action to perform on the finishing of any command by the scheduler. More...
 
void InitSendable (frc::SendableBuilder &builder) override
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from frc::ErrorBase
 ErrorBase (const ErrorBase &)=default
 
ErrorBaseoperator= (const ErrorBase &)=default
 
 ErrorBase (ErrorBase &&)=default
 
ErrorBaseoperator= (ErrorBase &&)=default
 
virtual ErrorGetError ()
 Retrieve the current error. More...
 
virtual const ErrorGetError () const
 Retrieve the current error. More...
 
virtual void ClearError () const
 Clear the current error information associated with this sensor.
 
virtual void SetErrnoError (const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
 Set error information associated with a C library call that set an error to the "errno" global variable. More...
 
virtual void SetImaqError (int success, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
 Set the current error information associated from the nivision Imaq API. More...
 
virtual void SetError (Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void SetErrorRange (Error::Code code, int32_t minRange, int32_t maxRange, int32_t requestedValue, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void SetWPIError (const wpi::Twine &errorMessage, Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void CloneError (const ErrorBase &rhs) const
 
virtual bool StatusIsFatal () const
 Check if the current error code represents a fatal error. More...
 
void ClearGlobalErrors ()
 Clear global errors.
 
- Public Member Functions inherited from frc::SendableHelper< CommandScheduler >
 SendableHelper (const SendableHelper &rhs)=default
 
 SendableHelper (SendableHelper &&rhs)
 
SendableHelperoperator= (const SendableHelper &rhs)=default
 
SendableHelperoperator= (SendableHelper &&rhs)
 
std::string GetName () const
 Gets the name of this Sendable object. More...
 
void SetName (const wpi::Twine &name)
 Sets the name of this Sendable object. More...
 
void SetName (const wpi::Twine &subsystem, const wpi::Twine &name)
 Sets both the subsystem name and device name of this Sendable object. More...
 
std::string GetSubsystem () const
 Gets the subsystem name of this Sendable object. More...
 
void SetSubsystem (const wpi::Twine &subsystem)
 Sets the subsystem name of this Sendable object. More...
 

Static Public Member Functions

static CommandSchedulerGetInstance ()
 Returns the Scheduler instance. More...
 
- Static Public Member Functions inherited from frc::ErrorBase
static void SetGlobalError (Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber)
 
static void SetGlobalWPIError (const wpi::Twine &errorMessage, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber)
 
static Error GetGlobalError ()
 Retrieve the last global error.
 
static std::vector< ErrorGetGlobalErrors ()
 Retrieve all global errors.
 

Friends

class CommandTestBase
 

Additional Inherited Members

- Protected Member Functions inherited from frc::SendableHelper< CommandScheduler >
void SetName (const wpi::Twine &moduleType, int channel)
 Sets the name of the sensor with a channel number. More...
 
void SetName (const wpi::Twine &moduleType, int moduleNumber, int channel)
 Sets the name of the sensor with a module and channel number. More...
 
void AddChild (std::shared_ptr< Sendable > child)
 Add a child component. More...
 
void AddChild (void *child)
 Add a child component. More...
 
- Protected Attributes inherited from frc::ErrorBase
Error m_error
 

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.

Member Function Documentation

◆ AddButton()

void frc2::CommandScheduler::AddButton ( wpi::unique_function< void()>  button)

Adds a button binding to the scheduler, which will be polled to schedule commands.

Parameters
buttonThe button to add

◆ Cancel() [1/3]

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

Cancels a command.

The scheduler will only call the interrupted method of a canceled command, not the end method (though the interrupted method may itself call the end method). Commands will be canceled even if they are not scheduled as interruptible.

Parameters
commandthe command to cancel

◆ Cancel() [2/3]

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

Cancels commands.

The scheduler will only call the interrupted method of a canceled command, not the end method (though the interrupted method may itself call the end method). Commands will be canceled even if they are not scheduled as interruptible.

Parameters
commandsthe commands to cancel

◆ Cancel() [3/3]

void frc2::CommandScheduler::Cancel ( wpi::ArrayRef< Command * >  commands)

Cancels commands.

The scheduler will only call the interrupted method of a canceled command, not the end method (though the interrupted method may itself call the end method). Commands will be canceled even if they are not scheduled as interruptible.

Parameters
commandsthe commands to cancel

◆ 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 ( frc::SendableBuilder builder)
overridevirtual

Initializes this Sendable object.

Parameters
buildersendable builder

Implements frc::Sendable.

◆ IsScheduled() [1/3]

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
commandsthe command to query
Returns
whether the command is currently scheduled

◆ IsScheduled() [2/3]

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() [3/3]

bool frc2::CommandScheduler::IsScheduled ( wpi::ArrayRef< 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

◆ 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()

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

◆ RegisterSubsystem()

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

◆ 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/6]

void frc2::CommandScheduler::Schedule ( bool  interruptible,
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
interruptiblewhether this command can be interrupted
commandthe command to schedule

◆ Schedule() [2/6]

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

Schedules multiple commands 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
interruptiblewhether the commands should be interruptible
commandsthe commands to schedule

◆ Schedule() [3/6]

void frc2::CommandScheduler::Schedule ( bool  interruptible,
wpi::ArrayRef< Command * >  commands 
)

Schedules multiple commands 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
interruptiblewhether the commands should be interruptible
commandsthe commands to schedule

◆ Schedule() [4/6]

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

Schedules a command for execution, with interruptible defaulted to true.

Does nothing if the command is already scheduled.

Parameters
commandthe command to schedule

◆ Schedule() [5/6]

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

Schedules multiple commands for execution, with interruptible defaulted to true.

Does nothing if the command is already scheduled.

Parameters
commandsthe commands to schedule

◆ Schedule() [6/6]

void frc2::CommandScheduler::Schedule ( wpi::ArrayRef< Command * >  commands)

Schedules multiple commands for execution, with interruptible defaulted to true.

Does nothing if the command is already scheduled.

Parameters
commandsthe commands to schedule

◆ SetDefaultCommand()

template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<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.

◆ TimeSinceScheduled()

double frc2::CommandScheduler::TimeSinceScheduled ( const Command command) const

Returns the time since a given command was scheduled.

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
the time since the command was scheduled, in seconds

◆ UnregisterSubsystem()

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

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