WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Command.h
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 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <memory>
11 #include <string>
12 
13 #include <wpi/SmallPtrSet.h>
14 #include <wpi/Twine.h>
15 
16 #include "frc/ErrorBase.h"
17 #include "frc/commands/Subsystem.h"
18 #include "frc/smartdashboard/SendableBase.h"
19 
20 namespace frc {
21 
22 class CommandGroup;
23 
48 class Command : public ErrorBase, public SendableBase {
49  friend class CommandGroup;
50  friend class Scheduler;
51 
52  public:
58  Command();
59 
65  explicit Command(const wpi::Twine& name);
66 
73  explicit Command(double timeout);
74 
80  explicit Command(Subsystem& subsystem);
81 
89  Command(const wpi::Twine& name, double timeout);
90 
97  Command(const wpi::Twine& name, Subsystem& subsystem);
98 
105  Command(double timeout, Subsystem& subsystem);
106 
114  Command(const wpi::Twine& name, double timeout, Subsystem& subsystem);
115 
116  ~Command() override = default;
117 
118  Command(Command&&) = default;
119  Command& operator=(Command&&) = default;
120 
128  double TimeSinceInitialized() const;
129 
140  void Requires(Subsystem* s);
141 
149  void Start();
150 
156  bool Run();
157 
169  void Cancel();
170 
179  bool IsRunning() const;
180 
186  bool IsInitialized() const;
187 
193  bool IsCompleted() const;
194 
200  bool IsCanceled() const;
201 
207  bool IsInterruptible() const;
208 
214  void SetInterruptible(bool interruptible);
215 
222  bool DoesRequire(Subsystem* subsystem) const;
223 
225 
233  const SubsystemSet& GetRequirements() const;
234 
243  CommandGroup* GetGroup() const;
244 
253  void SetRunWhenDisabled(bool run);
254 
262  bool WillRunWhenDisabled() const;
263 
271  int GetID() const;
272 
273  protected:
280  void SetTimeout(double timeout);
281 
290  bool IsTimedOut() const;
291 
299  bool AssertUnlocked(const std::string& message);
300 
306  void SetParent(CommandGroup* parent);
307 
313  bool IsParented() const;
314 
321  void ClearRequirements();
322 
327  virtual void Initialize();
328 
333  virtual void Execute();
334 
351  virtual bool IsFinished() = 0;
352 
359  virtual void End();
360 
371  virtual void Interrupted();
372 
373  virtual void _Initialize();
374  virtual void _Interrupted();
375  virtual void _Execute();
376  virtual void _End();
377 
384  virtual void _Cancel();
385 
386  friend class ConditionalCommand;
387 
388  private:
392  void LockChanges();
393 
399  void Removed();
400 
412  void StartRunning();
413 
419  void StartTiming();
420 
421  // The time since this command was initialized
422  double m_startTime = -1;
423 
424  // The time (in seconds) before this command "times out" (-1 if no timeout)
425  double m_timeout;
426 
427  // Whether or not this command has been initialized
428  bool m_initialized = false;
429 
430  // The requirements (or null if no requirements)
431  wpi::SmallPtrSet<Subsystem*, 4> m_requirements;
432 
433  // Whether or not it is running
434  bool m_running = false;
435 
436  // Whether or not it is interruptible
437  bool m_interruptible = true;
438 
439  // Whether or not it has been canceled
440  bool m_canceled = false;
441 
442  // Whether or not it has been locked
443  bool m_locked = false;
444 
445  // Whether this command should run when the robot is disabled
446  bool m_runWhenDisabled = false;
447 
448  // The CommandGroup this is in
449  CommandGroup* m_parent = nullptr;
450 
451  // Whether or not this command has completed running
452  bool m_completed = false;
453 
454  int m_commandID = m_commandCounter++;
455  static int m_commandCounter;
456 
457  public:
458  void InitSendable(SendableBuilder& builder) override;
459 };
460 
461 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
bool WillRunWhenDisabled() const
Returns whether or not this Command will run when the robot is disabled, or if it will cancel itself...
const SubsystemSet & GetRequirements() const
Returns the requirements (as an std::set of Subsystem pointers) of this command.
bool IsTimedOut() const
Returns whether or not the TimeSinceInitialized() method returns a number which is greater than or eq...
int GetID() const
Get the ID (sequence number) for this command.
void Requires(Subsystem *s)
This method specifies that the given Subsystem is used by this command.
void Cancel()
This will cancel the current command.
void ClearRequirements()
Clears list of subsystem requirements.
Definition: Subsystem.h:23
void Start()
Starts up the command.
virtual bool IsFinished()=0
Returns whether this command is finished.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:324
Command()
Creates a new command.
bool IsCanceled() const
Returns whether or not this has been canceled.
void SetParent(CommandGroup *parent)
Sets the parent of this command.
bool IsInitialized() const
Returns whether or not the command has been initialized.
virtual void End()
Called when the command ended peacefully.
virtual void Initialize()
The initialize method is called the first time this Command is run after being started.
bool IsInterruptible() const
Returns whether or not this command can be interrupted.
virtual void Execute()
The execute method is called repeatedly until this Command either finishes or is canceled.
double TimeSinceInitialized() const
Returns the time since this command was initialized (in seconds).
bool IsRunning() const
Returns whether or not the command is running.
bool DoesRequire(Subsystem *subsystem) const
Checks if the command requires the given Subsystem.
A CommandGroup is a list of commands which are executed in sequence.
Definition: CommandGroup.h:36
bool IsParented() const
Returns whether the command has a parent.
void SetInterruptible(bool interruptible)
Sets whether or not this command can be interrupted.
Base class for most objects.
Definition: ErrorBase.h:74
bool AssertUnlocked(const std::string &message)
If changes are locked, then this will generate a CommandIllegalUse error.
Definition: SendableBase.h:19
bool IsCompleted() const
Returns whether or not the command has completed running.
Definition: SendableBuilder.h:23
CommandGroup * GetGroup() const
Returns the CommandGroup that this command is a part of.
bool Run()
The run method is used internally to actually run the commands.
The Command class is at the very core of the entire command framework.
Definition: Command.h:48
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:395
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
A ConditionalCommand is a Command that starts one of two commands.
Definition: ConditionalCommand.h:35
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void SetRunWhenDisabled(bool run)
Sets whether or not this Command should run when the robot is disabled.
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: Scheduler.h:21
void SetTimeout(double timeout)
Sets the timeout of this command.
virtual void Interrupted()
Called when the command ends because somebody called Cancel() or another command shared the same requ...