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"
|
2022-04-28 19:38:38 +03:00
|
|
|
#include "frc2/command/EndlessCommand.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"
|
|
|
|
|
#include "frc2/command/ProxyScheduleCommand.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) {
|
|
|
|
|
m_isGrouped = false;
|
|
|
|
|
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) && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership()).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) && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership())
|
|
|
|
|
.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) && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership())
|
|
|
|
|
.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-10-06 01:19:28 +03:00
|
|
|
return CommandPtr(std::move(*this).TransferOwnership())
|
|
|
|
|
.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) && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership())
|
|
|
|
|
.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(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::function<void()> toRun, wpi::span<Subsystem* const> requirements) && {
|
2022-10-06 01:19:28 +03:00
|
|
|
return CommandPtr(std::move(*this).TransferOwnership())
|
|
|
|
|
.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,
|
|
|
|
|
wpi::span<Subsystem* const> requirements) && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership())
|
|
|
|
|
.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::Endlessly() && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership()).Endlessly();
|
2022-04-28 19:38:38 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::Repeatedly() && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership()).Repeatedly();
|
2022-04-08 08:02:08 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr Command::AsProxy() && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership()).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) && {
|
|
|
|
|
return CommandPtr(std::move(*this).TransferOwnership())
|
|
|
|
|
.Unless(std::move(condition));
|
2022-05-24 10:22:19 -06: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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
std::string Command::GetName() const {
|
|
|
|
|
return GetTypeName(*this);
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Command::IsGrouped() const {
|
|
|
|
|
return m_isGrouped;
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Command::SetGrouped(bool grouped) {
|
|
|
|
|
m_isGrouped = grouped;
|
|
|
|
|
}
|
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
|