WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Command.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2011-2017. 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 <memory>
11 #include <set>
12 #include <string>
13 
14 #include "ErrorBase.h"
15 #include "SmartDashboard/NamedSendable.h"
16 #include "tables/ITableListener.h"
17 
18 namespace frc {
19 
20 class CommandGroup;
21 class Subsystem;
22 
52 class Command : public ErrorBase, public NamedSendable, public ITableListener {
53  friend class CommandGroup;
54  friend class Scheduler;
55 
56  public:
57  Command();
58  explicit Command(const std::string& name);
59  explicit Command(double timeout);
60  Command(const std::string& name, double timeout);
61  virtual ~Command();
62  double TimeSinceInitialized() const;
63  void Requires(Subsystem* s);
64  bool IsCanceled() const;
65  void Start();
66  bool Run();
67  void Cancel();
68  bool IsRunning() const;
69  bool IsInterruptible() const;
70  void SetInterruptible(bool interruptible);
71  bool DoesRequire(Subsystem* subsystem) const;
72  typedef std::set<Subsystem*> SubsystemSet;
73  SubsystemSet GetRequirements() const;
74  CommandGroup* GetGroup() const;
75  void SetRunWhenDisabled(bool run);
76  bool WillRunWhenDisabled() const;
77  int GetID() const;
78 
79  protected:
80  void SetTimeout(double timeout);
81  bool IsTimedOut() const;
82  bool AssertUnlocked(const std::string& message);
83  void SetParent(CommandGroup* parent);
84  void ClearRequirements();
85 
86  virtual void Initialize();
87  virtual void Execute();
88 
105  virtual bool IsFinished() = 0;
106 
107  virtual void End();
108  virtual void Interrupted();
109 
110  virtual void _Initialize();
111  virtual void _Interrupted();
112  virtual void _Execute();
113  virtual void _End();
114  virtual void _Cancel();
115 
116  friend class ConditionalCommand;
117 
118  private:
119  void LockChanges();
120  /*synchronized*/ void Removed();
121  void StartRunning();
122  void StartTiming();
123 
125  std::string m_name;
126 
128  double m_startTime = -1;
129 
132  double m_timeout;
133 
135  bool m_initialized = false;
136 
138  SubsystemSet m_requirements;
139 
141  bool m_running = false;
142 
144  bool m_interruptible = true;
145 
147  bool m_canceled = false;
148 
150  bool m_locked = false;
151 
153  bool m_runWhenDisabled = false;
154 
156  CommandGroup* m_parent = nullptr;
157 
158  int m_commandID = m_commandCounter++;
159  static int m_commandCounter;
160 
161  public:
162  std::string GetName() const override;
163  void InitTable(std::shared_ptr<ITable> subtable) override;
164  std::shared_ptr<ITable> GetTable() const override;
165  std::string GetSmartDashboardType() const override;
166  void ValueChanged(ITable* source, llvm::StringRef key,
167  std::shared_ptr<nt::Value> value, bool isNew) override;
168 
169  protected:
170  std::shared_ptr<ITable> m_table;
171 };
172 
173 } // namespace frc
void InitTable(std::shared_ptr< ITable > subtable) override
Initializes a table for this sendable object.
Definition: Command.cpp:438
bool IsCanceled() const
Returns whether or not this has been canceled.
Definition: Command.cpp:376
Command()
Creates a new command.
Definition: Command.cpp:30
A table whose values can be read and written to.
Definition: ITable.h:22
double TimeSinceInitialized() const
Returns the time since this command was initialized (in seconds).
Definition: Command.cpp:102
int GetID() const
Get the ID (sequence number) for this command.
Definition: Command.cpp:80
std::shared_ptr< ITable > GetTable() const override
Definition: Command.cpp:449
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:432
void ClearRequirements()
Clears list of subsystem requirements.
Definition: Command.cpp:311
std::string GetName() const override
Definition: Command.cpp:434
Definition: Subsystem.h:20
void Start()
Starts up the command.
Definition: Command.cpp:159
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:400
bool Run()
The run method is used internally to actually run the commands.
Definition: Command.cpp:174
void SetTimeout(double timeout)
Sets the timeout of this command.
Definition: Command.cpp:88
CommandGroup * GetGroup() const
Returns the CommandGroup that this command is a part of.
Definition: Command.cpp:412
void SetRunWhenDisabled(bool run)
Sets whether or not this Command should run when the robot is disabled.
Definition: Command.cpp:423
bool IsRunning() const
Returns whether or not the command is running.
Definition: Command.cpp:339
bool IsInterruptible() const
Returns whether or not this command can be interrupted.
Definition: Command.cpp:383
A listener that listens to changes in values in a ITable.
Definition: ITableListener.h:18
A CommandGroup is a list of commands which are executed in sequence.
Definition: CommandGroup.h:39
void ValueChanged(ITable *source, llvm::StringRef key, std::shared_ptr< nt::Value > value, bool isNew) override
Called when a key-value pair is changed in a ITable.
Definition: Command.cpp:451
bool AssertUnlocked(const std::string &message)
If changes are locked, then this will generate a CommandIllegalUse error.
Definition: Command.cpp:275
Base class for most objects.
Definition: ErrorBase.h:72
void SetInterruptible(bool interruptible)
Sets whether or not this command can be interrupted.
Definition: Command.cpp:390
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:367
virtual void Initialize()
The initialize method is called the first time this Command is run after being started.
Definition: Command.cpp:195
virtual void Execute()
The execute method is called repeatedly until this Command either finishes or is canceled.
Definition: Command.cpp:201
void SetParent(CommandGroup *parent)
Sets the parent of this command.
Definition: Command.cpp:290
The Command class is at the very core of the entire command framework.
Definition: Command.h:52
std::string GetSmartDashboardType() const override
Definition: Command.cpp:436
void Cancel()
This will cancel the current command.
Definition: Command.cpp:352
A ConditionalCommand is a Command that starts one of two commands.
Definition: ConditionalCommand.h:41
virtual void End()
Called when the command ended peacefully.
Definition: Command.cpp:208
void Requires(Subsystem *s)
This method specifies that the given Subsystem is used by this command.
Definition: Command.cpp:121
SubsystemSet GetRequirements() const
Returns the requirements (as an std::set of Subsystems pointers) of this command. ...
Definition: Command.cpp:259
virtual void Interrupted()
Called when the command ends because somebody called cancel() or another command shared the same requ...
Definition: Command.cpp:221
The interface for sendable objects that gives the sendable a default name in the Smart Dashboard...
Definition: NamedSendable.h:21
bool IsTimedOut() const
Returns whether or not the timeSinceInitialized() method returns a number which is greater than or eq...
Definition: Command.cpp:248
Definition: Scheduler.h:29