mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
This deals with the majority of the user-facing code in wpilibC++Devices and a substantial portion of it in wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests are untouched except where it is necessary to make them work with the rest of the libraries. There is still a lot to do in the following areas: -The HAL (which we may not want to touch at all). -The I2C, Serial, and SPI interfaces in wpilibC++Devices, which I haven't gotten around to doing yet. -Most wpilibC++Devices classes have void* pointers for interacting with the HAL. -InterruptableSensorBase passes a void *params for the interrupt handler. -I haven't converted all the const char* to std::strings. -There are plenty of other cases of raw pointers still existing. -This doesn't fall directly under raw pointer stuff, but move syntax and rvalue references could be introduced in many places. -I haven't touched vision code. -The Resource classes conflict (one is in the hal, the other in wpilibC++). Someone should figure out a more permanent fix (eg, just renaming them), then doing what I did (making a new namespace for one of them, essentially the same as renaming it). A few other things: -I created a NullDeleter class which is marked as deprecated. What this does is it can be passed as the deleter to a std::shared_ptr so that when you are converting raw pointers to shared_ptrs the shared_ptr doesn't do any deletion if someone else owns the raw pointer. This should only be used in making old raw pointer UIs. -I had to alter the build.gradle so that it did not emit errors when deprecated functions called deprecated functions. Unfortunately, gradle doesn't appear to be actually printing out gcc warnigns for some reason. The best way I have found to fix this is to patch the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff) so that a deprecated function calling a deprecated function is fine but a non-deprecated function calling a deprecated function will throw a warning (which we then elevate with -Werror). I believe that clang deals with this properly, although I have not tried it myself. Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
190 lines
5.9 KiB
C++
190 lines
5.9 KiB
C++
/*----------------------------------------------------------------------------*/
|
|
/* Copyright (c) FIRST 2011. All Rights Reserved.
|
|
*/
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
#ifndef __COMMAND_H__
|
|
#define __COMMAND_H__
|
|
|
|
#include "ErrorBase.h"
|
|
#include "SmartDashboard/NamedSendable.h"
|
|
#include <set>
|
|
#include <string>
|
|
#include <memory>
|
|
|
|
class CommandGroup;
|
|
class Subsystem;
|
|
|
|
/**
|
|
* The Command class is at the very core of the entire command framework.
|
|
* Every command can be started with a call to {@link Command#Start() Start()}.
|
|
* Once a command is started it will call {@link Command#Initialize()
|
|
* Initialize()}, and then
|
|
* will repeatedly call {@link Command#Execute() Execute()} until the {@link
|
|
*Command#IsFinished() IsFinished()}
|
|
* returns true. Once it does, {@link Command#End() End()} will be called.
|
|
*
|
|
* <p>However, if at any point while it is running {@link Command#Cancel()
|
|
* Cancel()} is called, then
|
|
* the command will be stopped and {@link Command#Interrupted() Interrupted()}
|
|
* will be called.</p>
|
|
*
|
|
* <p>If a command uses a {@link Subsystem}, then it should specify that it does
|
|
* so by
|
|
* calling the {@link Command#Requires(Subsystem) Requires(...)} method
|
|
* in its constructor. Note that a Command may have multiple requirements, and
|
|
* {@link Command#Requires(Subsystem) Requires(...)} should be
|
|
* called for each one.</p>
|
|
*
|
|
* <p>If a command is running and a new command with shared requirements is
|
|
* started,
|
|
* then one of two things will happen. If the active command is interruptible,
|
|
* then {@link Command#Cancel() Cancel()} will be called and the command will be
|
|
* removed
|
|
* to make way for the new one. If the active command is not interruptible, the
|
|
* other one will not even be started, and the active one will continue
|
|
* functioning.</p>
|
|
*
|
|
* @see CommandGroup
|
|
* @see Subsystem
|
|
*/
|
|
class Command : public ErrorBase, public NamedSendable, public ITableListener {
|
|
friend class CommandGroup;
|
|
friend class Scheduler;
|
|
|
|
public:
|
|
Command();
|
|
Command(const char *name);
|
|
Command(double timeout);
|
|
Command(const char *name, double timeout);
|
|
virtual ~Command();
|
|
double TimeSinceInitialized() const;
|
|
void Requires(Subsystem *s);
|
|
bool IsCanceled() const;
|
|
void Start();
|
|
bool Run();
|
|
void Cancel();
|
|
bool IsRunning() const;
|
|
bool IsInterruptible() const;
|
|
void SetInterruptible(bool interruptible);
|
|
bool DoesRequire(Subsystem *subsystem) const;
|
|
typedef std::set<Subsystem *> SubsystemSet;
|
|
SubsystemSet GetRequirements() const;
|
|
CommandGroup *GetGroup() const;
|
|
void SetRunWhenDisabled(bool run);
|
|
bool WillRunWhenDisabled() const;
|
|
int GetID() const;
|
|
|
|
protected:
|
|
void SetTimeout(double timeout);
|
|
bool IsTimedOut() const;
|
|
bool AssertUnlocked(const char *message);
|
|
void SetParent(CommandGroup *parent);
|
|
/**
|
|
* The initialize method is called the first time this Command is run after
|
|
* being started.
|
|
*/
|
|
virtual void Initialize() = 0;
|
|
/**
|
|
* The execute method is called repeatedly until this Command either finishes
|
|
* or is canceled.
|
|
*/
|
|
virtual void Execute() = 0;
|
|
/**
|
|
* Returns whether this command is finished.
|
|
* If it is, then the command will be removed
|
|
* and {@link Command#end() end()} will be called.
|
|
*
|
|
* <p>It may be useful for a team to reference the {@link Command#isTimedOut()
|
|
* isTimedOut()} method
|
|
* for time-sensitive commands.</p>
|
|
* @return whether this command is finished.
|
|
* @see Command#isTimedOut() isTimedOut()
|
|
*/
|
|
virtual bool IsFinished() = 0;
|
|
/**
|
|
* Called when the command ended peacefully. This is where you may want
|
|
* to wrap up loose ends, like shutting off a motor that was being used
|
|
* in the command.
|
|
*/
|
|
virtual void End() = 0;
|
|
/**
|
|
* Called when the command ends because somebody called {@link
|
|
*Command#cancel() cancel()}
|
|
* or another command shared the same requirements as this one, and booted
|
|
* it out.
|
|
*
|
|
* <p>This is where you may want
|
|
* to wrap up loose ends, like shutting off a motor that was being used
|
|
* in the command.</p>
|
|
*
|
|
* <p>Generally, it is useful to simply call the {@link Command#end() end()}
|
|
* method
|
|
* within this method</p>
|
|
*/
|
|
virtual void Interrupted() = 0;
|
|
virtual void _Initialize();
|
|
virtual void _Interrupted();
|
|
virtual void _Execute();
|
|
virtual void _End();
|
|
virtual void _Cancel();
|
|
|
|
private:
|
|
void LockChanges();
|
|
/*synchronized*/ void Removed();
|
|
void StartRunning();
|
|
void StartTiming();
|
|
|
|
/** The name of this command */
|
|
std::string m_name;
|
|
|
|
/** The time since this command was initialized */
|
|
double m_startTime = -1;
|
|
|
|
/** The time (in seconds) before this command "times out" (or -1 if no
|
|
* timeout) */
|
|
double m_timeout;
|
|
|
|
/** Whether or not this command has been initialized */
|
|
bool m_initialized = false;
|
|
|
|
/** The requirements (or null if no requirements) */
|
|
SubsystemSet m_requirements;
|
|
|
|
/** Whether or not it is running */
|
|
bool m_running = false;
|
|
|
|
/** Whether or not it is interruptible*/
|
|
bool m_interruptible = true;
|
|
|
|
/** Whether or not it has been canceled */
|
|
bool m_canceled = false;
|
|
|
|
/** Whether or not it has been locked */
|
|
bool m_locked = false;
|
|
|
|
/** Whether this command should run when the robot is disabled */
|
|
bool m_runWhenDisabled = false;
|
|
|
|
/** The {@link CommandGroup} this is in */
|
|
CommandGroup *m_parent = nullptr;
|
|
|
|
int m_commandID = m_commandCounter++;
|
|
static int m_commandCounter;
|
|
|
|
public:
|
|
virtual std::string GetName();
|
|
virtual void InitTable(::std::shared_ptr<ITable> table);
|
|
virtual ::std::shared_ptr<ITable> GetTable() const;
|
|
virtual std::string GetSmartDashboardType() const;
|
|
virtual void ValueChanged(::std::shared_ptr<ITable> source, const std::string &key,
|
|
EntryValue value, bool isNew);
|
|
|
|
protected:
|
|
::std::shared_ptr<ITable> m_table = nullptr;
|
|
};
|
|
|
|
#endif
|