WPILibC++  2020.3.2
Command.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 <functional>
11 #include <initializer_list>
12 #include <memory>
13 #include <string>
14 
15 #include <frc/ErrorBase.h>
16 #include <units/units.h>
17 #include <wpi/ArrayRef.h>
18 #include <wpi/Demangle.h>
19 #include <wpi/SmallSet.h>
20 
21 #include "frc2/command/Subsystem.h"
22 
23 namespace frc2 {
24 
25 template <typename T>
26 std::string GetTypeName(const T& type) {
27  return wpi::Demangle(typeid(type).name());
28 }
29 
30 class ParallelCommandGroup;
31 class ParallelRaceGroup;
32 class ParallelDeadlineGroup;
33 class SequentialCommandGroup;
34 class PerpetualCommand;
35 class ProxyScheduleCommand;
36 
52 class Command : public frc::ErrorBase {
53  public:
54  Command() = default;
55  virtual ~Command();
56 
57  Command(const Command&);
58  Command& operator=(const Command&);
59  Command(Command&&) = default;
60  Command& operator=(Command&&) = default;
61 
66  virtual void Initialize();
67 
72  virtual void Execute();
73 
80  virtual void End(bool interrupted);
81 
88  virtual bool IsFinished() { return false; }
89 
103  virtual wpi::SmallSet<Subsystem*, 4> GetRequirements() const = 0;
104 
114  ParallelRaceGroup WithTimeout(units::second_t duration) &&;
115 
125  ParallelRaceGroup WithInterrupt(std::function<bool()> condition) &&;
126 
135  std::function<void()> toRun,
136  std::initializer_list<Subsystem*> requirements) &&;
137 
146  std::function<void()> toRun,
147  wpi::ArrayRef<Subsystem*> requirements = {}) &&;
148 
156  SequentialCommandGroup AndThen(
157  std::function<void()> toRun,
158  std::initializer_list<Subsystem*> requirements) &&;
159 
167  SequentialCommandGroup AndThen(
168  std::function<void()> toRun,
169  wpi::ArrayRef<Subsystem*> requirements = {}) &&;
170 
177  PerpetualCommand Perpetually() &&;
178 
187  ProxyScheduleCommand AsProxy();
188 
195  void Schedule(bool interruptible);
196 
200  void Schedule() { Schedule(true); }
201 
206  void Cancel();
207 
215  bool IsScheduled() const;
216 
227  bool HasRequirement(Subsystem* requirement) const;
228 
233  bool IsGrouped() const;
234 
240  void SetGrouped(bool grouped);
241 
248  virtual bool RunsWhenDisabled() const { return false; }
249 
250  virtual std::string GetName() const;
251 
252  protected:
257  virtual std::unique_ptr<Command> TransferOwnership() && = 0;
258 
259  bool m_isGrouped = false;
260 };
261 
269 bool RequirementsDisjoint(Command* first, Command* second);
270 } // namespace frc2
frc2::ParallelRaceGroup
A CommandGroup that runs a set of commands in parallel, ending when any one of the commands ends and ...
Definition: ParallelRaceGroup.h:30
frc2::Command::IsFinished
virtual bool IsFinished()
Whether the command has finished.
Definition: Command.h:88
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
frc2::Command::Schedule
void Schedule()
Schedules this command, defaulting to interruptible.
Definition: Command.h:200
frc2::Command::Execute
virtual void Execute()
The main body of a command.
frc2::Command::End
virtual void End(bool interrupted)
The action to take when the command ends.
frc2::Command::WithTimeout
ParallelRaceGroup WithTimeout(units::second_t duration) &&
Decorates this command with a timeout.
wpi::Demangle
std::string Demangle(const Twine &mangledSymbol)
Demangle a C++ symbol.
frc2::Command::TransferOwnership
virtual std::unique_ptr< Command > TransferOwnership() &&=0
Transfers ownership of this command to a unique pointer.
frc2::Command::Cancel
void Cancel()
Cancels this command.
frc2::Command
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:52
frc2::Command::AsProxy
ProxyScheduleCommand AsProxy()
Decorates this command to run "by proxy" by wrapping it in a {}.
frc2::Command::Initialize
virtual void Initialize()
The initial subroutine of a command.
wpi::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc2::Command::IsGrouped
bool IsGrouped() const
Whether the command is currently grouped in a command group.
frc2::Command::WithInterrupt
ParallelRaceGroup WithInterrupt(std::function< bool()> condition) &&
Decorates this command with an interrupt condition.
frc2::Command::RunsWhenDisabled
virtual bool RunsWhenDisabled() const
Whether the given command should run when the robot is disabled.
Definition: Command.h:248
frc2::Command::SetGrouped
void SetGrouped(bool grouped)
Sets whether the command is currently grouped in a command group.
frc2::Command::HasRequirement
bool HasRequirement(Subsystem *requirement) const
Whether the command requires a given subsystem.
frc2::Command::Perpetually
PerpetualCommand Perpetually() &&
Decorates this command to run perpetually, ignoring its ordinary end conditions.
frc2::SequentialCommandGroup
A CommandGroups that runs a list of commands in sequence.
Definition: SequentialCommandGroup.h:38
frc2::Command::BeforeStarting
SequentialCommandGroup BeforeStarting(std::function< void()> toRun, std::initializer_list< Subsystem * > requirements) &&
Decorates this command with a runnable to run before this command starts.
frc2::Command::IsScheduled
bool IsScheduled() const
Whether or not the command is currently scheduled.
frc2::Subsystem
A robot subsystem.
Definition: Subsystem.h:39
frc2::Command::GetRequirements
virtual wpi::SmallSet< Subsystem *, 4 > GetRequirements() const =0
Specifies the set of subsystems used by this command.
frc2::Command::AndThen
SequentialCommandGroup AndThen(std::function< void()> toRun, std::initializer_list< Subsystem * > requirements) &&
Decorates this command with a runnable to run after the command finishes.