Class CommandScheduler
- All Implemented Interfaces:
NTSendable
,Sendable
,AutoCloseable
public final class CommandScheduler extends Object implements NTSendable, AutoCloseable
Command
s. 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(Subsystem...)
in order for their Subsystem.periodic()
methods to be called and for their default commands to be scheduled.
This class is provided by the NewCommands VendorDep
-
Method Summary
Modifier and Type Method Description void
addButton(Runnable button)
Deprecated.UseTrigger
void
cancel(Command... commands)
Cancels commands.void
cancelAll()
Cancels all commands that are currently scheduled.void
clearButtons()
Deprecated.callEventLoop.clear()
ongetActiveButtonLoop()
directly instead.void
clearComposedCommands()
Clears the list of composed commands, allowing all commands to be freely used again.void
close()
void
disable()
Disables the command scheduler.void
enable()
Enables the command scheduler.EventLoop
getActiveButtonLoop()
Get the active button poll.EventLoop
getDefaultButtonLoop()
Get the default button poll.Command
getDefaultCommand(Subsystem subsystem)
Gets the default command associated with this subsystem.static CommandScheduler
getInstance()
Returns the Scheduler instance.void
initSendable(NTSendableBuilder builder)
Initializes thisSendable
object.boolean
isComposed(Command command)
Check if the given command has been composed.boolean
isScheduled(Command... commands)
Whether the given commands are running.void
onCommandExecute(Consumer<Command> action)
Adds an action to perform on the execution of any command by the scheduler.void
onCommandFinish(Consumer<Command> action)
Adds an action to perform on the finishing of any command by the scheduler.void
onCommandInitialize(Consumer<Command> action)
Adds an action to perform on the initialization of any command by the scheduler.void
onCommandInterrupt(Consumer<Command> action)
Adds an action to perform on the interruption of any command by the scheduler.void
registerComposedCommands(Command... commands)
Register commands as composed.void
registerSubsystem(Subsystem... subsystems)
Registers subsystems with the scheduler.void
removeComposedCommand(Command command)
Removes a single command from the list of composed commands, allowing it to be freely used again.void
removeDefaultCommand(Subsystem subsystem)
Removes the default command for a subsystem.void
requireNotComposed(Command command)
Requires that the specified command hasn't been already added to a composition.void
requireNotComposed(Collection<Command> commands)
Requires that the specified commands not have been already added to a composition.Command
requiring(Subsystem subsystem)
Returns the command currently requiring a given subsystem.void
run()
Runs a single iteration of the scheduler.void
schedule(Command... commands)
Schedules multiple commands for execution.void
setActiveButtonLoop(EventLoop loop)
Replace the button poll with another one.void
setDefaultCommand(Subsystem subsystem, Command defaultCommand)
Sets the default command for a subsystem.void
setPeriod(double period)
Changes the period of the loop overrun watchdog.void
unregisterSubsystem(Subsystem... subsystems)
Un-registers subsystems with the scheduler.
-
Method Details
-
getInstance
Returns the Scheduler instance.- Returns:
- the instance
-
setPeriod
Changes the period of the loop overrun watchdog. This should be kept in sync with the TimedRobot period.- Parameters:
period
- Period in seconds.
-
close
- Specified by:
close
in interfaceAutoCloseable
-
getDefaultButtonLoop
Get the default button poll.- Returns:
- a reference to the default
EventLoop
object polling buttons.
-
getActiveButtonLoop
Get the active button poll.- Returns:
- a reference to the current
EventLoop
object polling buttons.
-
setActiveButtonLoop
Replace the button poll with another one.- Parameters:
loop
- the new button polling loop object.
-
addButton
Deprecated.UseTrigger
Adds a button binding to the scheduler, which will be polled to schedule commands.- Parameters:
button
- The button to add
-
clearButtons
Deprecated.callEventLoop.clear()
ongetActiveButtonLoop()
directly instead.Removes all button bindings from the scheduler. -
schedule
Schedules multiple commands for execution. Does nothing for commands already scheduled.- Parameters:
commands
- the commands to schedule. No-op on null.
-
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.
-
registerSubsystem
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:
subsystems
- the subsystem to register
-
unregisterSubsystem
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:
subsystems
- the subsystem to un-register
-
setDefaultCommand
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. theirCommand.isFinished()
method should return false), as they would simply be re-scheduled if they do. Default commands must also require their subsystem.- Parameters:
subsystem
- the subsystem whose default command will be setdefaultCommand
- the default command to associate with the subsystem
-
removeDefaultCommand
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:
subsystem
- the subsystem whose default command will be removed
-
getDefaultCommand
Gets the default command associated with this subsystem. Null if this subsystem has no default command associated with it.- Parameters:
subsystem
- the subsystem to inquire about- Returns:
- the default command associated with the subsystem
-
cancel
Cancels commands. The scheduler will only callCommand.end(boolean)
method of the canceled command withtrue
, indicating they were canceled (as opposed to finishing normally).Commands will be canceled regardless of
interruption behavior
.- Parameters:
commands
- the commands to cancel
-
cancelAll
Cancels all commands that are currently scheduled. -
isScheduled
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 compositions, as the scheduler does not see them.- Parameters:
commands
- the command to query- Returns:
- whether the command is currently scheduled
-
requiring
Returns the command currently requiring a given subsystem. Null if no command is currently requiring the subsystem- Parameters:
subsystem
- the subsystem to be inquired about- Returns:
- the command currently requiring the subsystem, or null if no command is currently scheduled
-
disable
Disables the command scheduler. -
enable
Enables the command scheduler. -
onCommandInitialize
Adds an action to perform on the initialization of any command by the scheduler.- Parameters:
action
- the action to perform
-
onCommandExecute
Adds an action to perform on the execution of any command by the scheduler.- Parameters:
action
- the action to perform
-
onCommandInterrupt
Adds an action to perform on the interruption of any command by the scheduler.- Parameters:
action
- the action to perform
-
onCommandFinish
Adds an action to perform on the finishing of any command by the scheduler.- Parameters:
action
- the action to perform
-
registerComposedCommands
Register commands as composed. An exception will be thrown if these commands are scheduled directly or added to a composition.- Parameters:
commands
- the commands to register- Throws:
IllegalArgumentException
- if the given commands have already been composed.
-
clearComposedCommands
Clears the list of composed commands, allowing all commands to be freely used again.WARNING: Using this haphazardly can result in unexpected/undesirable behavior. Do not use this unless you fully understand what you are doing.
-
removeComposedCommand
Removes a single command from the list of composed commands, allowing it to be freely used again.WARNING: Using this haphazardly can result in unexpected/undesirable behavior. Do not use this unless you fully understand what you are doing.
- Parameters:
command
- the command to remove from the list of grouped commands
-
requireNotComposed
Requires that the specified command hasn't been already added to a composition.- Parameters:
command
- The command to check- Throws:
IllegalArgumentException
- if the given commands have already been composed.
-
requireNotComposed
Requires that the specified commands not have been already added to a composition.- Parameters:
commands
- The commands to check- Throws:
IllegalArgumentException
- if the given commands have already been composed.
-
isComposed
Check if the given command has been composed.- Parameters:
command
- The command to check- Returns:
- true if composed
-
initSendable
Description copied from interface:NTSendable
Initializes thisSendable
object.- Specified by:
initSendable
in interfaceNTSendable
- Parameters:
builder
- sendable builder
-