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"
|
|
|
|
|
|
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"
|
|
|
|
|
#include "frc2/command/PerpetualCommand.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;
|
|
|
|
|
|
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) {}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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::WithInterrupt(std::function<bool()> condition) && {
|
2022-11-21 19:45:50 +02:00
|
|
|
return std::move(*this).ToPtr().Until(std::move(condition));
|
2019-08-25 23:55:59 -04: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PerpetualCommand Command::Perpetually() && {
|
2022-05-24 13:56:48 -07:00
|
|
|
WPI_IGNORE_DEPRECATED
|
2019-08-25 23:55:59 -04:00
|
|
|
return PerpetualCommand(std::move(*this).TransferOwnership());
|
2022-05-24 13:56:48 -07:00
|
|
|
WPI_UNIGNORE_DEPRECATED
|
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
|
|
|
}
|
|
|
|
|
|
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) && {
|
|
|
|
|
SetName(name);
|
|
|
|
|
return std::move(*this).ToPtr();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
std::string Command::GetName() const {
|
|
|
|
|
return GetTypeName(*this);
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2022-11-28 10:41:25 -05:00
|
|
|
void Command::SetName(std::string_view name) {}
|
|
|
|
|
|
2022-12-07 07:13:31 +02:00
|
|
|
bool Command::IsComposed() const {
|
|
|
|
|
return m_isComposed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Command::SetComposed(bool isComposed) {
|
|
|
|
|
m_isComposed = isComposed;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsGrouped() const {
|
2022-12-07 07:13:31 +02:00
|
|
|
return IsComposed();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::SetGrouped(bool grouped) {
|
2022-12-07 07:13:31 +02:00
|
|
|
SetComposed(grouped);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
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
|