WPILibC++  2020.3.2
Command.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2011-2019 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/Sendable.h"
19 #include "frc/smartdashboard/SendableHelper.h"
20 
21 namespace frc {
22 
23 class CommandGroup;
24 
49 class Command : public ErrorBase,
50  public Sendable,
51  public SendableHelper<Command> {
52  friend class CommandGroup;
53  friend class Scheduler;
54 
55  public:
61  Command();
62 
68  explicit Command(const wpi::Twine& name);
69 
76  explicit Command(double timeout);
77 
83  explicit Command(Subsystem& subsystem);
84 
92  Command(const wpi::Twine& name, double timeout);
93 
100  Command(const wpi::Twine& name, Subsystem& subsystem);
101 
108  Command(double timeout, Subsystem& subsystem);
109 
117  Command(const wpi::Twine& name, double timeout, Subsystem& subsystem);
118 
119  ~Command() override = default;
120 
121  Command(Command&&) = default;
122  Command& operator=(Command&&) = default;
123 
131  double TimeSinceInitialized() const;
132 
143  void Requires(Subsystem* s);
144 
152  void Start();
153 
159  bool Run();
160 
172  void Cancel();
173 
182  bool IsRunning() const;
183 
189  bool IsInitialized() const;
190 
196  bool IsCompleted() const;
197 
203  bool IsCanceled() const;
204 
210  bool IsInterruptible() const;
211 
217  void SetInterruptible(bool interruptible);
218 
225  bool DoesRequire(Subsystem* subsystem) const;
226 
228 
236  const SubsystemSet& GetRequirements() const;
237 
246  CommandGroup* GetGroup() const;
247 
256  void SetRunWhenDisabled(bool run);
257 
265  bool WillRunWhenDisabled() const;
266 
274  int GetID() const;
275 
276  protected:
283  void SetTimeout(double timeout);
284 
293  bool IsTimedOut() const;
294 
302  bool AssertUnlocked(const std::string& message);
303 
309  void SetParent(CommandGroup* parent);
310 
316  bool IsParented() const;
317 
324  void ClearRequirements();
325 
330  virtual void Initialize();
331 
336  virtual void Execute();
337 
354  virtual bool IsFinished() = 0;
355 
362  virtual void End();
363 
374  virtual void Interrupted();
375 
376  virtual void _Initialize();
377  virtual void _Interrupted();
378  virtual void _Execute();
379  virtual void _End();
380 
387  virtual void _Cancel();
388 
389  friend class ConditionalCommand;
390 
391  public:
397  std::string GetName() const;
398 
404  void SetName(const wpi::Twine& name);
405 
411  std::string GetSubsystem() const;
412 
418  void SetSubsystem(const wpi::Twine& subsystem);
419 
420  private:
424  void LockChanges();
425 
431  void Removed();
432 
444  void StartRunning();
445 
451  void StartTiming();
452 
453  // The time since this command was initialized
454  double m_startTime = -1;
455 
456  // The time (in seconds) before this command "times out" (-1 if no timeout)
457  double m_timeout;
458 
459  // Whether or not this command has been initialized
460  bool m_initialized = false;
461 
462  // The requirements (or null if no requirements)
463  wpi::SmallPtrSet<Subsystem*, 4> m_requirements;
464 
465  // Whether or not it is running
466  bool m_running = false;
467 
468  // Whether or not it is interruptible
469  bool m_interruptible = true;
470 
471  // Whether or not it has been canceled
472  bool m_canceled = false;
473 
474  // Whether or not it has been locked
475  bool m_locked = false;
476 
477  // Whether this command should run when the robot is disabled
478  bool m_runWhenDisabled = false;
479 
480  // The CommandGroup this is in
481  CommandGroup* m_parent = nullptr;
482 
483  // Whether or not this command has completed running
484  bool m_completed = false;
485 
486  int m_commandID = m_commandCounter++;
487  static int m_commandCounter;
488 
489  public:
490  void InitSendable(SendableBuilder& builder) override;
491 };
492 
493 } // namespace frc
frc::Command::End
virtual void End()
Called when the command ended peacefully.
frc::Command::Run
bool Run()
The run method is used internally to actually run the commands.
frc::Command::GetRequirements
const SubsystemSet & GetRequirements() const
Returns the requirements (as an std::set of Subsystem pointers) of this command.
frc::Command::Cancel
void Cancel()
This will cancel the current command.
frc::Command::SetSubsystem
void SetSubsystem(const wpi::Twine &subsystem)
Sets the subsystem name of this Command.
frc::Command::Interrupted
virtual void Interrupted()
Called when the command ends because somebody called Cancel() or another command shared the same requ...
wpi::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:395
frc::Command::IsFinished
virtual bool IsFinished()=0
Returns whether this command is finished.
frc::CommandGroup
A CommandGroup is a list of commands which are executed in sequence.
Definition: CommandGroup.h:36
frc::Command::Requires
void Requires(Subsystem *s)
This method specifies that the given Subsystem is used by this command.
frc::Scheduler
Definition: Scheduler.h:22
frc::ConditionalCommand
A ConditionalCommand is a Command that starts one of two commands.
Definition: ConditionalCommand.h:35
frc::Command::SetParent
void SetParent(CommandGroup *parent)
Sets the parent of this command.
frc::Command::Command
Command()
Creates a new command.
frc::Command::IsRunning
bool IsRunning() const
Returns whether or not the command is running.
frc::Command::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::Command::TimeSinceInitialized
double TimeSinceInitialized() const
Returns the time since this command was initialized (in seconds).
frc::Command
The Command class is at the very core of the entire command framework.
Definition: Command.h:49
frc::Command::ClearRequirements
void ClearRequirements()
Clears list of subsystem requirements.
frc::Command::IsParented
bool IsParented() const
Returns whether the command has a parent.
frc::Subsystem
Definition: Subsystem.h:24
frc::Command::GetName
std::string GetName() const
Gets the name of this Command.
frc::Command::IsInitialized
bool IsInitialized() const
Returns whether or not the command has been initialized.
frc::Command::GetID
int GetID() const
Get the ID (sequence number) for this command.
frc::Command::GetSubsystem
std::string GetSubsystem() const
Gets the subsystem name of this Command.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::Command::IsCanceled
bool IsCanceled() const
Returns whether or not this has been canceled.
wpi::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:324
frc::Command::Start
void Start()
Starts up the command.
frc::Command::_Cancel
virtual void _Cancel()
This works like Cancel(), except that it doesn't throw an exception if it is a part of a command grou...
frc::Command::DoesRequire
bool DoesRequire(Subsystem *subsystem) const
Checks if the command requires the given Subsystem.
frc::Command::SetName
void SetName(const wpi::Twine &name)
Sets the name of this Command.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc::Command::SetTimeout
void SetTimeout(double timeout)
Sets the timeout of this command.
frc::Command::SetInterruptible
void SetInterruptible(bool interruptible)
Sets whether or not this command can be interrupted.
frc::Command::SetRunWhenDisabled
void SetRunWhenDisabled(bool run)
Sets whether or not this Command should run when the robot is disabled.
frc::Command::IsCompleted
bool IsCompleted() const
Returns whether or not the command has completed running.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::Command::IsTimedOut
bool IsTimedOut() const
Returns whether or not the TimeSinceInitialized() method returns a number which is greater than or eq...
frc::Command::Initialize
virtual void Initialize()
The initialize method is called the first time this Command is run after being started.
frc::Command::Execute
virtual void Execute()
The execute method is called repeatedly until this Command either finishes or is canceled.
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::Command::WillRunWhenDisabled
bool WillRunWhenDisabled() const
Returns whether or not this Command will run when the robot is disabled, or if it will cancel itself.
frc::SendableBuilder
Definition: SendableBuilder.h:23
frc::Command::GetGroup
CommandGroup * GetGroup() const
Returns the CommandGroup that this command is a part of.
frc::Command::AssertUnlocked
bool AssertUnlocked(const std::string &message)
If changes are locked, then this will generate a CommandIllegalUse error.
frc::Command::IsInterruptible
bool IsInterruptible() const
Returns whether or not this command can be interrupted.