WPILibC++  unspecified
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2011-2018 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 /*----------------------------------------------------------------------------*/
8 #pragma once
10 #include <memory>
11 #include <set>
12 #include <string>
14 #include <llvm/Twine.h>
16 #include "ErrorBase.h"
17 #include "SmartDashboard/SendableBase.h"
19 namespace frc {
21 class CommandGroup;
22 class Subsystem;
48 class Command : public ErrorBase, public SendableBase {
49  friend class CommandGroup;
50  friend class Scheduler;
52  public:
53  Command();
54  explicit Command(const llvm::Twine& name);
55  explicit Command(double timeout);
56  Command(const llvm::Twine& name, double timeout);
57  ~Command() override = default;
58  double TimeSinceInitialized() const;
59  void Requires(Subsystem* s);
60  bool IsCanceled() const;
61  void Start();
62  bool Run();
63  void Cancel();
64  bool IsRunning() const;
65  bool IsInitialized() const;
66  bool IsCompleted() const;
67  bool IsInterruptible() const;
68  void SetInterruptible(bool interruptible);
69  bool DoesRequire(Subsystem* subsystem) const;
70  typedef std::set<Subsystem*> SubsystemSet;
71  SubsystemSet GetRequirements() const;
72  CommandGroup* GetGroup() const;
73  void SetRunWhenDisabled(bool run);
74  bool WillRunWhenDisabled() const;
75  int GetID() const;
77  protected:
78  void SetTimeout(double timeout);
79  bool IsTimedOut() const;
80  bool AssertUnlocked(const std::string& message);
81  void SetParent(CommandGroup* parent);
82  bool IsParented() const;
83  void ClearRequirements();
85  virtual void Initialize();
86  virtual void Execute();
104  virtual bool IsFinished() = 0;
106  virtual void End();
107  virtual void Interrupted();
109  virtual void _Initialize();
110  virtual void _Interrupted();
111  virtual void _Execute();
112  virtual void _End();
113  virtual void _Cancel();
115  friend class ConditionalCommand;
117  private:
118  void LockChanges();
119  void Removed();
120  void StartRunning();
121  void StartTiming();
123  // The time since this command was initialized
124  double m_startTime = -1;
126  // The time (in seconds) before this command "times out" (-1 if no timeout)
127  double m_timeout;
129  // Whether or not this command has been initialized
130  bool m_initialized = false;
132  // The requirements (or null if no requirements)
133  SubsystemSet m_requirements;
135  // Whether or not it is running
136  bool m_running = false;
138  // Whether or not it is interruptible
139  bool m_interruptible = true;
141  // Whether or not it has been canceled
142  bool m_canceled = false;
144  // Whether or not it has been locked
145  bool m_locked = false;
147  // Whether this command should run when the robot is disabled
148  bool m_runWhenDisabled = false;
150  // The CommandGroup this is in
151  CommandGroup* m_parent = nullptr;
153  // Whether or not this command has completed running
154  bool m_completed = false;
156  int m_commandID = m_commandCounter++;
157  static int m_commandCounter;
159  public:
160  void InitSendable(SendableBuilder& builder) override;
161 };
163 } // namespace frc
bool IsCanceled() const
Returns whether or not this has been canceled.
Definition: Command.cpp:385
Creates a new command.
Definition: Command.cpp:29
Definition: RobotController.cpp:14
double TimeSinceInitialized() const
Returns the time since this command was initialized (in seconds).
Definition: Command.cpp:99
int GetID() const
Get the ID (sequence number) for this command.
Definition: Command.cpp:77
bool IsInitialized() const
Returns whether or not the command has been initialized.
Definition: Command.cpp:341
bool WillRunWhenDisabled() const
Returns whether or not this Command will run when the robot is disabled, or if it will cancel itself...
Definition: Command.cpp:440
void ClearRequirements()
Clears list of subsystem requirements.
Definition: Command.cpp:307
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Definition: Subsystem.h:23
void Start()
Starts up the command.
Definition: Command.cpp:153
virtual bool IsFinished()=0
Returns whether this command is finished.
bool DoesRequire(Subsystem *subsystem) const
Checks if the command requires the given Subsystem.
Definition: Command.cpp:409
bool Run()
The run method is used internally to actually run the commands.
Definition: Command.cpp:169
void SetTimeout(double timeout)
Sets the timeout of this command.
Definition: Command.cpp:85
CommandGroup * GetGroup() const
Returns the CommandGroup that this command is a part of.
Definition: Command.cpp:421
void SetRunWhenDisabled(bool run)
Sets whether or not this Command should run when the robot is disabled.
Definition: Command.cpp:431
bool IsRunning() const
Returns whether or not the command is running.
Definition: Command.cpp:334
bool IsInterruptible() const
Returns whether or not this command can be interrupted.
Definition: Command.cpp:392
A CommandGroup is a list of commands which are executed in sequence.
Definition: CommandGroup.h:37
bool AssertUnlocked(const std::string &message)
If changes are locked, then this will generate a CommandIllegalUse error.
Definition: Command.cpp:266
Base class for most objects.
Definition: ErrorBase.h:74
void SetInterruptible(bool interruptible)
Sets whether or not this command can be interrupted.
Definition: Command.cpp:399
Definition: SendableBase.h:19
virtual void _Cancel()
This works like Cancel(), except that it doesn&#39;t throw an exception if it is a part of a command grou...
Definition: Command.cpp:376
virtual void Initialize()
The initialize method is called the first time this Command is run after being started.
Definition: Command.cpp:190
bool IsCompleted() const
Returns whether or not the command has completed running.
Definition: Command.cpp:348
Definition: SendableBuilder.h:23
virtual void Execute()
The execute method is called repeatedly until this Command either finishes or is canceled.
Definition: Command.cpp:196
void SetParent(CommandGroup *parent)
Sets the parent of this command.
Definition: Command.cpp:281
The Command class is at the very core of the entire command framework.
Definition: Command.h:48
void Cancel()
This will cancel the current command.
Definition: Command.cpp:361
A ConditionalCommand is a Command that starts one of two commands.
Definition: ConditionalCommand.h:35
bool IsParented() const
Returns whether the command has a parent.
Definition: Command.cpp:299
virtual void End()
Called when the command ended peacefully.
Definition: Command.cpp:202
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
Definition: Command.cpp:442
void Requires(Subsystem *s)
This method specifies that the given Subsystem is used by this command.
Definition: Command.cpp:116
SubsystemSet GetRequirements() const
Returns the requirements (as an std::set of Subsystem pointers) of this command.
Definition: Command.cpp:250
virtual void Interrupted()
Called when the command ends because somebody called Cancel() or another command shared the same requ...
Definition: Command.cpp:214
bool IsTimedOut() const
Returns whether or not the TimeSinceInitialized() method returns a number which is greater than or eq...
Definition: Command.cpp:239
Definition: Scheduler.h:27