2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/commands/Command.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include <typeinfo>
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/RobotState.h"
|
|
|
|
|
#include "frc/Timer.h"
|
|
|
|
|
#include "frc/commands/CommandGroup.h"
|
|
|
|
|
#include "frc/commands/Scheduler.h"
|
|
|
|
|
#include "frc/livewindow/LiveWindow.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
int Command::m_commandCounter = 0;
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Command::Command() : Command("", -1_s) {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Command::Command(std::string_view name) : Command(name, -1_s) {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Command::Command(units::second_t timeout) : Command("", timeout) {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Command::Command(Subsystem& subsystem) : Command("", -1_s) {
|
2018-09-02 14:18:12 -07:00
|
|
|
Requires(&subsystem);
|
2018-08-19 22:43:21 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Command::Command(std::string_view name, units::second_t timeout) {
|
2015-07-24 19:19:40 -04:00
|
|
|
// We use -1.0 to indicate no timeout.
|
2021-05-28 22:06:59 -07:00
|
|
|
if (timeout < 0_s && timeout != -1_s) {
|
|
|
|
|
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0 s",
|
|
|
|
|
timeout.to<double>());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-06-29 02:43:44 -07:00
|
|
|
|
|
|
|
|
m_timeout = timeout;
|
2015-07-09 01:27:56 -07:00
|
|
|
|
|
|
|
|
// If name contains an empty string
|
2021-05-26 17:44:18 -07:00
|
|
|
if (name.empty()) {
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().Add(
|
2021-05-26 17:44:18 -07:00
|
|
|
this, fmt::format("Command_{}", typeid(*this).name()));
|
2016-05-20 17:30:37 -07:00
|
|
|
} else {
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().Add(this, name);
|
2015-07-09 01:27:56 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
Command::Command(std::string_view name, Subsystem& subsystem)
|
2021-05-28 22:06:59 -07:00
|
|
|
: Command(name, -1_s) {
|
2018-09-02 14:18:12 -07:00
|
|
|
Requires(&subsystem);
|
2018-08-19 22:43:21 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Command::Command(units::second_t timeout, Subsystem& subsystem)
|
|
|
|
|
: Command("", timeout) {
|
2018-09-02 14:18:12 -07:00
|
|
|
Requires(&subsystem);
|
2018-08-19 22:43:21 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
Command::Command(std::string_view name, units::second_t timeout,
|
|
|
|
|
Subsystem& subsystem)
|
2018-08-19 22:43:21 -04:00
|
|
|
: Command(name, timeout) {
|
2018-09-02 14:18:12 -07:00
|
|
|
Requires(&subsystem);
|
2018-08-19 22:43:21 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
units::second_t Command::TimeSinceInitialized() const {
|
|
|
|
|
if (m_startTime < 0_s) {
|
|
|
|
|
return 0_s;
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2015-06-25 15:07:55 -04:00
|
|
|
return Timer::GetFPGATimestamp() - m_startTime;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void Command::Requires(Subsystem* subsystem) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!AssertUnlocked("Can not add new requirement to command")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (subsystem != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_requirements.insert(subsystem);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "subsystem");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::Start() {
|
|
|
|
|
LockChanges();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_parent != nullptr) {
|
2021-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(
|
2021-05-23 19:33:33 -07:00
|
|
|
err::CommandIllegalUse, "{}",
|
2015-06-25 15:07:55 -04:00
|
|
|
"Can not start a command that is part of a command group");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-01-18 20:04:33 -08:00
|
|
|
m_completed = false;
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
Scheduler::GetInstance()->AddCommand(this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Command::Run() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_runWhenDisabled && m_parent == nullptr && RobotState::IsDisabled()) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Cancel();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (IsCanceled()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!m_initialized) {
|
|
|
|
|
m_initialized = true;
|
|
|
|
|
StartTiming();
|
|
|
|
|
_Initialize();
|
|
|
|
|
Initialize();
|
|
|
|
|
}
|
|
|
|
|
_Execute();
|
|
|
|
|
Execute();
|
|
|
|
|
return !IsFinished();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Command::Cancel() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_parent != nullptr) {
|
2021-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(
|
2021-05-23 19:33:33 -07:00
|
|
|
err::CommandIllegalUse, "{}",
|
2018-05-31 20:47:15 -07:00
|
|
|
"Can not cancel a command that is part of a command group");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-11-18 17:42:40 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
_Cancel();
|
|
|
|
|
}
|
2016-11-18 17:42:40 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsRunning() const {
|
|
|
|
|
return m_running;
|
|
|
|
|
}
|
2016-11-18 17:42:40 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsInitialized() const {
|
|
|
|
|
return m_initialized;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsCompleted() const {
|
|
|
|
|
return m_completed;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsCanceled() const {
|
|
|
|
|
return m_canceled;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsInterruptible() const {
|
|
|
|
|
return m_interruptible;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Command::SetInterruptible(bool interruptible) {
|
|
|
|
|
m_interruptible = interruptible;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
bool Command::DoesRequire(Subsystem* system) const {
|
|
|
|
|
return m_requirements.count(system) > 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-06-23 19:41:45 -05:00
|
|
|
const Command::SubsystemSet& Command::GetRequirements() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return m_requirements;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
CommandGroup* Command::GetGroup() const {
|
|
|
|
|
return m_parent;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::SetRunWhenDisabled(bool run) {
|
|
|
|
|
m_runWhenDisabled = run;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::WillRunWhenDisabled() const {
|
|
|
|
|
return m_runWhenDisabled;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Command::GetID() const {
|
|
|
|
|
return m_commandID;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
void Command::SetTimeout(units::second_t timeout) {
|
|
|
|
|
if (timeout < 0_s) {
|
|
|
|
|
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0 s",
|
|
|
|
|
timeout.to<double>());
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2018-05-31 20:47:15 -07:00
|
|
|
m_timeout = timeout;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Command::IsTimedOut() const {
|
2021-05-28 22:06:59 -07:00
|
|
|
return m_timeout != -1_s && TimeSinceInitialized() >= m_timeout;
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
bool Command::AssertUnlocked(std::string_view message) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (m_locked) {
|
2021-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(
|
|
|
|
|
err::CommandIllegalUse,
|
2021-05-23 19:33:33 -07:00
|
|
|
"{} after being started or being added to a command group", message);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
return true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void Command::SetParent(CommandGroup* parent) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (parent == nullptr) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "parent");
|
2015-06-23 04:49:51 -07:00
|
|
|
} else if (m_parent != nullptr) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::CommandIllegalUse, "{}",
|
2021-04-18 20:35:29 -07:00
|
|
|
"Can not give command to a command group after "
|
|
|
|
|
"already being put in a command group");
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
|
|
|
|
LockChanges();
|
|
|
|
|
m_parent = parent;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsParented() const {
|
|
|
|
|
return m_parent != nullptr;
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::ClearRequirements() {
|
|
|
|
|
m_requirements.clear();
|
|
|
|
|
}
|
2017-01-04 23:48:13 -08:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Command::Initialize() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Command::Execute() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Command::End() {}
|
2018-01-18 20:04:33 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::Interrupted() {
|
|
|
|
|
End();
|
|
|
|
|
}
|
2018-01-18 20:04:33 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::_Initialize() {
|
|
|
|
|
m_completed = false;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::_Interrupted() {
|
|
|
|
|
m_completed = true;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
void Command::_Execute() {}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::_End() {
|
|
|
|
|
m_completed = true;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Command::_Cancel() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (IsRunning()) {
|
|
|
|
|
m_canceled = true;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::LockChanges() {
|
|
|
|
|
m_locked = true;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Command::Removed() {
|
|
|
|
|
if (m_initialized) {
|
|
|
|
|
if (IsCanceled()) {
|
|
|
|
|
Interrupted();
|
|
|
|
|
_Interrupted();
|
|
|
|
|
} else {
|
|
|
|
|
End();
|
|
|
|
|
_End();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_initialized = false;
|
|
|
|
|
m_canceled = false;
|
|
|
|
|
m_running = false;
|
|
|
|
|
m_completed = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void Command::StartRunning() {
|
|
|
|
|
m_running = true;
|
2021-05-28 22:06:59 -07:00
|
|
|
m_startTime = -1_s;
|
2018-05-31 20:47:15 -07:00
|
|
|
m_completed = false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::StartTiming() {
|
|
|
|
|
m_startTime = Timer::GetFPGATimestamp();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-09-14 15:22:54 -05:00
|
|
|
std::string Command::GetName() const {
|
|
|
|
|
return SendableRegistry::GetInstance().GetName(this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void Command::SetName(std::string_view name) {
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().SetName(this, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Command::GetSubsystem() const {
|
|
|
|
|
return SendableRegistry::GetInstance().GetSubsystem(this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void Command::SetSubsystem(std::string_view name) {
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().SetSubsystem(this, name);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void Command::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Command");
|
2019-09-14 15:22:54 -05:00
|
|
|
builder.AddStringProperty(
|
|
|
|
|
".name", [=]() { return SendableRegistry::GetInstance().GetName(this); },
|
|
|
|
|
nullptr);
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddBooleanProperty(
|
|
|
|
|
"running", [=]() { return IsRunning(); },
|
|
|
|
|
[=](bool value) {
|
|
|
|
|
if (value) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!IsRunning()) {
|
|
|
|
|
Start();
|
|
|
|
|
}
|
2020-06-27 20:39:00 -07:00
|
|
|
} else {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (IsRunning()) {
|
|
|
|
|
Cancel();
|
|
|
|
|
}
|
2020-06-27 20:39:00 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
builder.AddBooleanProperty(
|
|
|
|
|
".isParented", [=]() { return IsParented(); }, nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|