WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Command.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2011-2016. 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 #ifndef __COMMAND_H__
9 #define __COMMAND_H__
10 
11 #include "ErrorBase.h"
12 #include "SmartDashboard/NamedSendable.h"
13 #include "tables/ITableListener.h"
14 #include <set>
15 #include <string>
16 #include <memory>
17 
18 class CommandGroup;
19 class Subsystem;
20 
54 class Command : public ErrorBase, public NamedSendable, public ITableListener {
55  friend class CommandGroup;
56  friend class Scheduler;
57 
58  public:
59  Command();
60  Command(const std::string &name);
61  Command(double timeout);
62  Command(const std::string &name, double timeout);
63  virtual ~Command();
64  double TimeSinceInitialized() const;
65  void Requires(Subsystem *s);
66  bool IsCanceled() const;
67  void Start();
68  bool Run();
69  void Cancel();
70  bool IsRunning() const;
71  bool IsInterruptible() const;
72  void SetInterruptible(bool interruptible);
73  bool DoesRequire(Subsystem *subsystem) const;
74  typedef std::set<Subsystem *> SubsystemSet;
75  SubsystemSet GetRequirements() const;
76  CommandGroup *GetGroup() const;
77  void SetRunWhenDisabled(bool run);
78  bool WillRunWhenDisabled() const;
79  int GetID() const;
80 
81  protected:
82  void SetTimeout(double timeout);
83  bool IsTimedOut() const;
84  bool AssertUnlocked(const std::string &message);
85  void SetParent(CommandGroup *parent);
90  virtual void Initialize() = 0;
95  virtual void Execute() = 0;
107  virtual bool IsFinished() = 0;
113  virtual void End() = 0;
128  virtual void Interrupted() = 0;
129  virtual void _Initialize();
130  virtual void _Interrupted();
131  virtual void _Execute();
132  virtual void _End();
133  virtual void _Cancel();
134 
135  private:
136  void LockChanges();
137  /*synchronized*/ void Removed();
138  void StartRunning();
139  void StartTiming();
140 
142  std::string m_name;
143 
145  double m_startTime = -1;
146 
149  double m_timeout;
150 
152  bool m_initialized = false;
153 
155  SubsystemSet m_requirements;
156 
158  bool m_running = false;
159 
161  bool m_interruptible = true;
162 
164  bool m_canceled = false;
165 
167  bool m_locked = false;
168 
170  bool m_runWhenDisabled = false;
171 
173  CommandGroup *m_parent = nullptr;
174 
175  int m_commandID = m_commandCounter++;
176  static int m_commandCounter;
177 
178  public:
179  virtual std::string GetName() const;
180  virtual void InitTable(std::shared_ptr<ITable> table);
181  virtual std::shared_ptr<ITable> GetTable() const;
182  virtual std::string GetSmartDashboardType() const;
183  virtual void ValueChanged(ITable* source, llvm::StringRef key,
184  std::shared_ptr<nt::Value> value, bool isNew);
185 
186  protected:
187  std::shared_ptr<ITable> m_table;
188 };
189 
190 #endif
virtual void InitTable(std::shared_ptr< ITable > table)
Initializes a table for this sendable object.
Definition: Command.cpp:371
A CommandGroup is a list of commands which are executed in sequence.
Definition: CommandGroup.h:37
bool IsInterruptible() const
Returns whether or not this command can be interrupted.
Definition: Command.cpp:320
A table whose values can be read and written to.
Definition: ITable.h:43
Definition: Scheduler.h:27
bool IsRunning() const
Returns whether or not the command is running.
Definition: Command.cpp:279
double TimeSinceInitialized() const
Returns the time since this command was initialized (in seconds).
Definition: Command.cpp:91
virtual void Interrupted()=0
Called when the command ends because somebody called cancel() or another command shared the same requ...
void SetRunWhenDisabled(bool run)
Sets whether or not this Command should run when the robot is disabled.
Definition: Command.cpp:355
virtual void Execute()=0
The execute method is called repeatedly until this Command either finishes or is canceled.
bool IsCanceled() const
Returns whether or not this has been canceled.
Definition: Command.cpp:314
Command()
Creates a new command.
Definition: Command.cpp:26
void SetInterruptible(bool interruptible)
Sets whether or not this command can be interrupted.
Definition: Command.cpp:326
int GetID() const
Get the ID (sequence number) for this command The ID is a unique sequence number that is incremented ...
Definition: Command.cpp:72
virtual std::string GetName() const
Definition: Command.cpp:365
void Start()
Starts up the command.
Definition: Command.cpp:146
virtual void Initialize()=0
The initialize method is called the first time this Command is run after being started.
bool Run()
The run method is used internally to actually run the commands.
Definition: Command.cpp:160
CommandGroup * GetGroup() const
Returns the CommandGroup that this command is a part of.
Definition: Command.cpp:345
Definition: Subsystem.h:18
virtual void _Cancel()
This works like cancel(), except that it doesn't throw an exception if it is a part of a command grou...
Definition: Command.cpp:306
Base class for most objects.
Definition: ErrorBase.h:66
bool DoesRequire(Subsystem *subsystem) const
Checks if the command requires the given Subsystem.
Definition: Command.cpp:335
virtual void End()=0
Called when the command ended peacefully.
virtual std::shared_ptr< ITable > GetTable() const
Definition: Command.cpp:382
void Cancel()
This will cancel the current command.
Definition: Command.cpp:292
A listener that listens to changes in values in a ITable.
Definition: ITableListener.h:18
virtual std::string GetSmartDashboardType() const
Definition: Command.cpp:369
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:363
The interface for sendable objects that gives the sendable a default name in the Smart Dashboard...
Definition: NamedSendable.h:19
virtual bool IsFinished()=0
Returns whether this command is finished.
virtual void ValueChanged(ITable *source, llvm::StringRef key, std::shared_ptr< nt::Value > value, bool isNew)
Called when a key-value pair is changed in a ITable.
Definition: Command.cpp:384
The Command class is at the very core of the entire command framework.
Definition: Command.h:54
SubsystemSet GetRequirements() const
Returns the requirements (as an std::set of Subsystems pointers) of this command. ...
Definition: Command.cpp:211
void Requires(Subsystem *s)
This method specifies that the given Subsystem is used by this command.
Definition: Command.cpp:109
bool IsTimedOut() const
Returns whether or not the timeSinceInitialized() method returns a number which is greater than or eq...
Definition: Command.cpp:201
bool AssertUnlocked(const std::string &message)
If changes are locked, then this will generate a CommandIllegalUse error.
Definition: Command.cpp:226
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39
void SetTimeout(double timeout)
Sets the timeout of this command.
Definition: Command.cpp:79
void SetParent(CommandGroup *parent)
Sets the parent of this command.
Definition: Command.cpp:239