WPILibC++  2020.3.2
CommandScheduler.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <initializer_list>
11 #include <memory>
12 #include <utility>
13 
14 #include <frc/ErrorBase.h>
15 #include <frc/WPIErrors.h>
16 #include <frc/smartdashboard/Sendable.h>
17 #include <frc/smartdashboard/SendableHelper.h>
18 #include <wpi/ArrayRef.h>
19 #include <wpi/FunctionExtras.h>
20 
21 namespace frc2 {
22 class Command;
23 class Subsystem;
24 
32 class CommandScheduler final : public frc::Sendable,
33  public frc::ErrorBase,
34  public frc::SendableHelper<CommandScheduler> {
35  public:
41  static CommandScheduler& GetInstance();
42 
44  CommandScheduler(const CommandScheduler&) = delete;
45  CommandScheduler& operator=(const CommandScheduler&) = delete;
46 
47  using Action = std::function<void(const Command&)>;
48 
55  void AddButton(wpi::unique_function<void()> button);
56 
60  void ClearButtons();
61 
72  void Schedule(bool interruptible, Command* command);
73 
80  void Schedule(Command* command);
81 
92  void Schedule(bool interruptible, wpi::ArrayRef<Command*> commands);
93 
104  void Schedule(bool interruptible, std::initializer_list<Command*> commands);
105 
112  void Schedule(wpi::ArrayRef<Command*> commands);
113 
120  void Schedule(std::initializer_list<Command*> commands);
121 
138  void Run();
139 
148  void RegisterSubsystem(Subsystem* subsystem);
149 
157  void UnregisterSubsystem(Subsystem* subsystem);
158 
159  void RegisterSubsystem(std::initializer_list<Subsystem*> subsystems);
161 
162  void UnregisterSubsystem(std::initializer_list<Subsystem*> subsystems);
164 
176  template <class T, typename = std::enable_if_t<std::is_base_of_v<
177  Command, std::remove_reference_t<T>>>>
178  void SetDefaultCommand(Subsystem* subsystem, T&& defaultCommand) {
179  if (!defaultCommand.HasRequirement(subsystem)) {
180  wpi_setWPIErrorWithContext(
181  CommandIllegalUse, "Default commands must require their subsystem!");
182  return;
183  }
184  if (defaultCommand.IsFinished()) {
185  wpi_setWPIErrorWithContext(CommandIllegalUse,
186  "Default commands should not end!");
187  return;
188  }
189  SetDefaultCommandImpl(subsystem,
190  std::make_unique<std::remove_reference_t<T>>(
191  std::forward<T>(defaultCommand)));
192  }
193 
201  Command* GetDefaultCommand(const Subsystem* subsystem) const;
202 
211  void Cancel(Command* command);
212 
221  void Cancel(wpi::ArrayRef<Command*> commands);
222 
231  void Cancel(std::initializer_list<Command*> commands);
232 
236  void CancelAll();
237 
247  double TimeSinceScheduled(const Command* command) const;
248 
257  bool IsScheduled(wpi::ArrayRef<const Command*> commands) const;
258 
267  bool IsScheduled(std::initializer_list<const Command*> commands) const;
268 
277  bool IsScheduled(const Command* command) const;
278 
286  Command* Requiring(const Subsystem* subsystem) const;
287 
291  void Disable();
292 
296  void Enable();
297 
304  void OnCommandInitialize(Action action);
305 
311  void OnCommandExecute(Action action);
312 
319  void OnCommandInterrupt(Action action);
320 
326  void OnCommandFinish(Action action);
327 
328  void InitSendable(frc::SendableBuilder& builder) override;
329 
330  private:
331  // Constructor; private as this is a singleton
333 
334  void SetDefaultCommandImpl(Subsystem* subsystem,
335  std::unique_ptr<Command> command);
336 
337  class Impl;
338  std::unique_ptr<Impl> m_impl;
339 
340  friend class CommandTestBase;
341 };
342 } // 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:43
frc2::CommandScheduler::UnregisterSubsystem
void UnregisterSubsystem(Subsystem *subsystem)
Un-registers subsystems with the scheduler.
frc2::CommandScheduler::Cancel
void Cancel(Command *command)
Cancels a command.
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::IsScheduled
bool IsScheduled(wpi::ArrayRef< const Command * > commands) const
Whether the given commands are running.
frc2::Command
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:52
frc2::CommandScheduler::ClearButtons
void ClearButtons()
Removes all button bindings from the scheduler.
frc2::CommandScheduler
The scheduler responsible for running Commands.
Definition: CommandScheduler.h:32
frc2::CommandScheduler::OnCommandInitialize
void OnCommandInitialize(Action action)
Adds an action to perform on the initialization of any command by the scheduler.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
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:17
frc2::CommandScheduler::Schedule
void Schedule(bool interruptible, Command *command)
Schedules a command for execution.
frc2::Subsystem
A robot subsystem.
Definition: Subsystem.h:39
frc2::CommandScheduler::SetDefaultCommand
void SetDefaultCommand(Subsystem *subsystem, T &&defaultCommand)
Sets the default command for a subsystem.
Definition: CommandScheduler.h:178
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:28
frc::SendableBuilder
Definition: SendableBuilder.h:23
frc2::CommandScheduler::OnCommandInterrupt
void OnCommandInterrupt(Action action)
Adds an action to perform on the interruption of any command by the scheduler.