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.
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
#include "frc2/command/Command.h"
|
|
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
|
|
|
|
|
2022-06-24 20:52:53 +03:00
|
|
|
#include "frc2/command/CommandHelper.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
#include "frc2/command/CommandScheduler.h"
|
2022-05-24 10:22:19 -06:00
|
|
|
#include "frc2/command/ConditionalCommand.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
#include "frc2/command/InstantCommand.h"
|
|
|
|
|
#include "frc2/command/ParallelCommandGroup.h"
|
|
|
|
|
#include "frc2/command/ParallelDeadlineGroup.h"
|
|
|
|
|
#include "frc2/command/ParallelRaceGroup.h"
|
2022-04-08 08:02:08 +03:00
|
|
|
#include "frc2/command/RepeatCommand.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
#include "frc2/command/SequentialCommandGroup.h"
|
|
|
|
|
#include "frc2/command/WaitCommand.h"
|
|
|
|
|
#include "frc2/command/WaitUntilCommand.h"
|
2022-06-24 20:52:53 +03:00
|
|
|
#include "frc2/command/WrapperCommand.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
using namespace frc2;
|
|
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
Command::Command() {
|
|
|
|
|
wpi::SendableRegistry::Add(this, GetTypeName(*this));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
Command::~Command() {
|
|
|
|
|
CommandScheduler::GetInstance().Cancel(this);
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2019-09-14 15:22:54 -05:00
|
|
|
Command& Command::operator=(const Command& rhs) {
|
2022-12-07 07:13:31 +02:00
|
|
|
m_isComposed = false;
|
2019-09-14 15:22:54 -05:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
void Command::Initialize() {}
|
|
|
|
|
void Command::Execute() {}
|
|
|
|
|
void Command::End(bool interrupted) {}
|
|
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
wpi::SmallSet<Subsystem*, 4> Command::GetRequirements() const {
|
|
|
|
|
return m_requirements;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::AddRequirements(std::initializer_list<Subsystem*> requirements) {
|
|
|
|
|
m_requirements.insert(requirements.begin(), requirements.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::AddRequirements(std::span<Subsystem* const> requirements) {
|
|
|
|
|
m_requirements.insert(requirements.begin(), requirements.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::AddRequirements(wpi::SmallSet<Subsystem*, 4> requirements) {
|
|
|
|
|
m_requirements.insert(requirements.begin(), requirements.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::AddRequirements(Subsystem* requirement) {
|
|
|
|
|
m_requirements.insert(requirement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::SetName(std::string_view name) {
|
|
|
|
|
wpi::SendableRegistry::SetName(this, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Command::GetName() const {
|
|
|
|
|
return wpi::SendableRegistry::GetName(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Command::GetSubsystem() const {
|
|
|
|
|
return wpi::SendableRegistry::GetSubsystem(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::SetSubsystem(std::string_view subsystem) {
|
|
|
|
|
wpi::SendableRegistry::SetSubsystem(this, subsystem);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::WithTimeout(units::second_t duration) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().WithTimeout(duration);
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::Until(std::function<bool()> condition) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().Until(std::move(condition));
|
2022-02-04 01:14:52 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-30 14:09:02 -07:00
|
|
|
CommandPtr Command::OnlyWhile(std::function<bool()> condition) && {
|
|
|
|
|
return std::move(*this).ToPtr().OnlyWhile(std::move(condition));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::IgnoringDisable(bool doesRunWhenDisabled) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().IgnoringDisable(doesRunWhenDisabled);
|
2022-06-24 20:52:53 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::WithInterruptBehavior(
|
2022-08-30 07:53:47 +03:00
|
|
|
InterruptionBehavior interruptBehavior) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().WithInterruptBehavior(interruptBehavior);
|
2022-08-30 07:53:47 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::BeforeStarting(
|
2019-11-20 22:44:18 -08:00
|
|
|
std::function<void()> toRun,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements) && {
|
2020-01-01 20:09:17 -08:00
|
|
|
return std::move(*this).BeforeStarting(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::move(toRun), {requirements.begin(), requirements.end()});
|
2020-01-01 20:09:17 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::BeforeStarting(
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void()> toRun, std::span<Subsystem* const> requirements) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().BeforeStarting(std::move(toRun),
|
|
|
|
|
requirements);
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::AndThen(std::function<void()> toRun,
|
|
|
|
|
std::initializer_list<Subsystem*> requirements) && {
|
2021-06-06 19:51:14 -07:00
|
|
|
return std::move(*this).AndThen(std::move(toRun),
|
|
|
|
|
{requirements.begin(), requirements.end()});
|
2020-01-01 20:09:17 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::AndThen(std::function<void()> toRun,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::span<Subsystem* const> requirements) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().AndThen(std::move(toRun), requirements);
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::Repeatedly() && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().Repeatedly();
|
2022-04-08 08:02:08 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::AsProxy() && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().AsProxy();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::Unless(std::function<bool()> condition) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().Unless(std::move(condition));
|
2022-05-24 10:22:19 -06:00
|
|
|
}
|
|
|
|
|
|
2023-04-30 14:09:02 -07:00
|
|
|
CommandPtr Command::OnlyIf(std::function<bool()> condition) && {
|
|
|
|
|
return std::move(*this).ToPtr().OnlyIf(std::move(condition));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 19:53:27 +03:00
|
|
|
CommandPtr Command::FinallyDo(std::function<void(bool)> end) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().FinallyDo(std::move(end));
|
2022-10-11 19:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr Command::HandleInterrupt(std::function<void(void)> handler) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().HandleInterrupt(std::move(handler));
|
2022-10-11 19:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 10:41:25 -05:00
|
|
|
CommandPtr Command::WithName(std::string_view name) && {
|
2022-12-16 04:28:52 +02:00
|
|
|
return std::move(*this).ToPtr().WithName(name);
|
2022-11-28 10:41:25 -05:00
|
|
|
}
|
|
|
|
|
|
2022-08-30 07:53:47 +03:00
|
|
|
void Command::Schedule() {
|
|
|
|
|
CommandScheduler::GetInstance().Schedule(this);
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::Cancel() {
|
|
|
|
|
CommandScheduler::GetInstance().Cancel(this);
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
bool Command::IsScheduled() const {
|
|
|
|
|
return CommandScheduler::GetInstance().IsScheduled(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Command::HasRequirement(Subsystem* requirement) const {
|
|
|
|
|
bool hasRequirement = false;
|
|
|
|
|
for (auto&& subsystem : GetRequirements()) {
|
|
|
|
|
hasRequirement |= requirement == subsystem;
|
|
|
|
|
}
|
|
|
|
|
return hasRequirement;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 07:13:31 +02:00
|
|
|
bool Command::IsComposed() const {
|
|
|
|
|
return m_isComposed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::SetComposed(bool isComposed) {
|
|
|
|
|
m_isComposed = isComposed;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
void Command::InitSendable(wpi::SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Command");
|
|
|
|
|
builder.AddStringProperty(
|
|
|
|
|
".name", [this] { return GetName(); }, nullptr);
|
|
|
|
|
builder.AddBooleanProperty(
|
|
|
|
|
"running", [this] { return IsScheduled(); },
|
|
|
|
|
[this](bool value) {
|
|
|
|
|
bool isScheduled = IsScheduled();
|
|
|
|
|
if (value && !isScheduled) {
|
|
|
|
|
Schedule();
|
|
|
|
|
} else if (!value && isScheduled) {
|
|
|
|
|
Cancel();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
builder.AddBooleanProperty(
|
|
|
|
|
".isParented", [this] { return IsComposed(); }, nullptr);
|
|
|
|
|
builder.AddStringProperty(
|
|
|
|
|
"interruptBehavior",
|
|
|
|
|
[this] {
|
|
|
|
|
switch (GetInterruptionBehavior()) {
|
|
|
|
|
case Command::InterruptionBehavior::kCancelIncoming:
|
|
|
|
|
return "kCancelIncoming";
|
|
|
|
|
case Command::InterruptionBehavior::kCancelSelf:
|
|
|
|
|
return "kCancelSelf";
|
|
|
|
|
default:
|
|
|
|
|
return "Invalid";
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
builder.AddBooleanProperty(
|
|
|
|
|
"runsWhenDisabled", [this] { return RunsWhenDisabled(); }, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
namespace frc2 {
|
|
|
|
|
bool RequirementsDisjoint(Command* first, Command* second) {
|
|
|
|
|
bool disjoint = true;
|
|
|
|
|
auto&& requirements = second->GetRequirements();
|
|
|
|
|
for (auto&& requirement : first->GetRequirements()) {
|
|
|
|
|
disjoint &= requirements.find(requirement) == requirements.end();
|
|
|
|
|
}
|
|
|
|
|
return disjoint;
|
|
|
|
|
}
|
|
|
|
|
} // namespace frc2
|