WPILibC++  2021.3.1
CommandScheduler.h
1 // Copyright (c) FIRST and other WPILib contributors.
2 // Open Source Software; you can modify and/or share it under the terms of
3 // the WPILib BSD license file in the root directory of this project.
4 
5 #pragma once
6 
7 #include <initializer_list>
8 #include <memory>
9 #include <utility>
10 
11 #include <frc/ErrorBase.h>
12 #include <frc/WPIErrors.h>
13 #include <frc/Watchdog.h>
14 #include <frc/smartdashboard/Sendable.h>
15 #include <frc/smartdashboard/SendableHelper.h>
16 #include <units/time.h>
17 #include <wpi/ArrayRef.h>
18 #include <wpi/FunctionExtras.h>
19 
20 namespace frc2 {
21 class Command;
22 class Subsystem;
23 
31 class CommandScheduler final : public frc::Sendable,
32  public frc::ErrorBase,
33  public frc::SendableHelper<CommandScheduler> {
34  public:
41 
42  ~CommandScheduler() override;
43  CommandScheduler(const CommandScheduler&) = delete;
44  CommandScheduler& operator=(const CommandScheduler&) = delete;
45 
46  using Action = std::function<void(const Command&)>;
47 
52  void SetPeriod(units::second_t period);
53 
60  void AddButton(wpi::unique_function<void()> button);
61 
65  void ClearButtons();
66 
77  void Schedule(bool interruptible, Command* command);
78 
85  void Schedule(Command* command);
86 
97  void Schedule(bool interruptible, wpi::ArrayRef<Command*> commands);
98 
109  void Schedule(bool interruptible, std::initializer_list<Command*> commands);
110 
118 
125  void Schedule(std::initializer_list<Command*> commands);
126 
143  void Run();
144 
153  void RegisterSubsystem(Subsystem* subsystem);
154 
162  void UnregisterSubsystem(Subsystem* subsystem);
163 
164  void RegisterSubsystem(std::initializer_list<Subsystem*> subsystems);
166 
167  void UnregisterSubsystem(std::initializer_list<Subsystem*> subsystems);
169 
181  template <class T, typename = std::enable_if_t<std::is_base_of_v<
182  Command, std::remove_reference_t<T>>>>
183  void SetDefaultCommand(Subsystem* subsystem, T&& defaultCommand) {
184  if (!defaultCommand.HasRequirement(subsystem)) {
185  wpi_setWPIErrorWithContext(
186  CommandIllegalUse, "Default commands must require their subsystem!");
187  return;
188  }
189  if (defaultCommand.IsFinished()) {
190  wpi_setWPIErrorWithContext(CommandIllegalUse,
191  "Default commands should not end!");
192  return;
193  }
194  SetDefaultCommandImpl(subsystem,
195  std::make_unique<std::remove_reference_t<T>>(
196  std::forward<T>(defaultCommand)));
197  }
198 
206  Command* GetDefaultCommand(const Subsystem* subsystem) const;
207 
218  void Cancel(Command* command);
219 
231 
242  void Cancel(std::initializer_list<Command*> commands);
243 
247  void CancelAll();
248 
258  double TimeSinceScheduled(const Command* command) const;
259 
269 
278  bool IsScheduled(std::initializer_list<const Command*> commands) const;
279 
288  bool IsScheduled(const Command* command) const;
289 
297  Command* Requiring(const Subsystem* subsystem) const;
298 
302  void Disable();
303 
307  void Enable();
308 
315  void OnCommandInitialize(Action action);
316 
322  void OnCommandExecute(Action action);
323 
330  void OnCommandInterrupt(Action action);
331 
337  void OnCommandFinish(Action action);
338 
339  void InitSendable(frc::SendableBuilder& builder) override;
340 
341  private:
342  // Constructor; private as this is a singleton
344 
345  void SetDefaultCommandImpl(Subsystem* subsystem,
346  std::unique_ptr<Command> command);
347 
348  class Impl;
349  std::unique_ptr<Impl> m_impl;
350 
351  frc::Watchdog m_watchdog;
352 
353  friend class CommandTestBase;
354 };
355 } // namespace frc2
frc2::CommandScheduler::Run
void Run()
Runs a single iteration of the scheduler.
frc2::CommandScheduler::CancelAll
void CancelAll()
Cancels all commands that are currently scheduled.
FunctionExtras.h
wpi::unique_function
Definition: FunctionExtras.h:44
frc2::CommandScheduler::UnregisterSubsystem
void UnregisterSubsystem(Subsystem *subsystem)
Un-registers subsystems with the scheduler.
frc2::CommandScheduler::IsScheduled
bool IsScheduled(const Command *command) const
Whether a given command is running.
frc2::CommandScheduler::Cancel
void Cancel(Command *command)
Cancels commands.
frc2::CommandScheduler::Requiring
Command * Requiring(const Subsystem *subsystem) const
Returns the command currently requiring a given subsystem.
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
frc2::CommandScheduler::OnCommandFinish
void OnCommandFinish(Action action)
Adds an action to perform on the finishing of any command by the scheduler.
frc2::CommandScheduler::TimeSinceScheduled
double TimeSinceScheduled(const Command *command) const
Returns the time since a given command was scheduled.
frc2::CommandScheduler::Enable
void Enable()
Enables the command scheduler.
frc2::CommandScheduler::GetInstance
static CommandScheduler & GetInstance()
Returns the Scheduler instance.
frc2::CommandScheduler::OnCommandExecute
void OnCommandExecute(Action action)
Adds an action to perform on the execution of any command by the scheduler.
frc2::CommandScheduler::Schedule
void Schedule(bool interruptible, std::initializer_list< Command * > commands)
Schedules multiple commands for execution.
frc2::CommandScheduler::IsScheduled
bool IsScheduled(wpi::ArrayRef< const Command * > commands) const
Whether the given commands are running.
frc2::CommandScheduler::Schedule
void Schedule(bool interruptible, wpi::ArrayRef< Command * > commands)
Schedules multiple commands for execution.
frc2::CommandScheduler::SetPeriod
void SetPeriod(units::second_t period)
Changes the period of the loop overrun watchdog.
frc2::CommandScheduler::Schedule
void Schedule(Command *command)
Schedules a command for execution, with interruptible defaulted to true.
frc2::Command
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:49
frc2::CommandScheduler::Schedule
void Schedule(wpi::ArrayRef< Command * > commands)
Schedules multiple commands for execution, with interruptible defaulted to true.
frc2::CommandScheduler::IsScheduled
bool IsScheduled(std::initializer_list< const Command * > commands) const
Whether the given commands are running.
frc2::CommandScheduler::ClearButtons
void ClearButtons()
Removes all button bindings from the scheduler.
frc2::CommandScheduler
The scheduler responsible for running Commands.
Definition: CommandScheduler.h:33
frc2::CommandScheduler::Cancel
void Cancel(wpi::ArrayRef< Command * > commands)
Cancels commands.
frc2::CommandScheduler::OnCommandInitialize
void OnCommandInitialize(Action action)
Adds an action to perform on the initialization of any command by the scheduler.
frc::Watchdog
A class that's a wrapper around a watchdog timer.
Definition: Watchdog.h:27
frc2::CommandScheduler::Schedule
void Schedule(std::initializer_list< Command * > commands)
Schedules multiple commands for execution, with interruptible defaulted to true.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:99
frc2::CommandScheduler::Disable
void Disable()
Disables the command scheduler.
frc2::CommandScheduler::InitSendable
void InitSendable(frc::SendableBuilder &builder) override
Initializes this Sendable object.
frc2::CommandScheduler::AddButton
void AddButton(wpi::unique_function< void()> button)
Adds a button binding to the scheduler, which will be polled to schedule commands.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:14
frc2::CommandScheduler::Schedule
void Schedule(bool interruptible, Command *command)
Schedules a command for execution.
frc2::Subsystem
A robot subsystem.
Definition: Subsystem.h:36
frc2::CommandScheduler::SetDefaultCommand
void SetDefaultCommand(Subsystem *subsystem, T &&defaultCommand)
Sets the default command for a subsystem.
Definition: CommandScheduler.h:183
frc2::CommandScheduler::RegisterSubsystem
void RegisterSubsystem(Subsystem *subsystem)
Registers subsystems with the scheduler.
frc2::CommandScheduler::GetDefaultCommand
Command * GetDefaultCommand(const Subsystem *subsystem) const
Gets the default command associated with this subsystem.
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:25
frc::SendableBuilder
Definition: SendableBuilder.h:21
frc2::CommandScheduler::OnCommandInterrupt
void OnCommandInterrupt(Action action)
Adds an action to perform on the interruption of any command by the scheduler.
frc2::CommandScheduler::Cancel
void Cancel(std::initializer_list< Command * > commands)
Cancels commands.