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) {}
|
|
|
|
|
|
2019-09-02 23:39:51 -07:00
|
|
|
ParallelRaceGroup Command::WithTimeout(units::second_t duration) && {
|
2019-08-25 23:55:59 -04:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
2019-09-02 23:39:51 -07:00
|
|
|
temp.emplace_back(std::make_unique<WaitCommand>(duration));
|
2019-08-25 23:55:59 -04:00
|
|
|
temp.emplace_back(std::move(*this).TransferOwnership());
|
|
|
|
|
return ParallelRaceGroup(std::move(temp));
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 01:14:52 -05:00
|
|
|
ParallelRaceGroup Command::Until(std::function<bool()> condition) && {
|
|
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
|
|
|
|
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
|
|
|
|
|
temp.emplace_back(std::move(*this).TransferOwnership());
|
|
|
|
|
return ParallelRaceGroup(std::move(temp));
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 20:52:53 +03:00
|
|
|
std::unique_ptr<Command> Command::IgnoringDisable(bool doesRunWhenDisabled) && {
|
|
|
|
|
class RunsWhenDisabledCommand
|
|
|
|
|
: public CommandHelper<WrapperCommand, RunsWhenDisabledCommand> {
|
|
|
|
|
public:
|
|
|
|
|
RunsWhenDisabledCommand(std::unique_ptr<Command>&& command,
|
|
|
|
|
bool doesRunWhenDisabled)
|
|
|
|
|
: CommandHelper(std::move(command)),
|
|
|
|
|
m_runsWhenDisabled(doesRunWhenDisabled) {}
|
|
|
|
|
bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_runsWhenDisabled;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return std::make_unique<RunsWhenDisabledCommand>(
|
|
|
|
|
std::move(*this).TransferOwnership(), doesRunWhenDisabled);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 07:53:47 +03:00
|
|
|
std::unique_ptr<Command> Command::WithInterruptBehavior(
|
|
|
|
|
InterruptionBehavior interruptBehavior) && {
|
|
|
|
|
class InterruptBehaviorCommand
|
|
|
|
|
: public CommandHelper<WrapperCommand, InterruptBehaviorCommand> {
|
|
|
|
|
public:
|
|
|
|
|
InterruptBehaviorCommand(std::unique_ptr<Command>&& command,
|
|
|
|
|
InterruptionBehavior interruptBehavior)
|
|
|
|
|
: CommandHelper(std::move(command)),
|
|
|
|
|
m_interruptBehavior(interruptBehavior) {}
|
|
|
|
|
InterruptionBehavior GetInterruptionBehavior() const override {
|
|
|
|
|
return m_interruptBehavior;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
InterruptionBehavior m_interruptBehavior;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return std::make_unique<InterruptBehaviorCommand>(
|
|
|
|
|
std::move(*this).TransferOwnership(), interruptBehavior);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 11:13:33 -04:00
|
|
|
ParallelRaceGroup Command::WithInterrupt(std::function<bool()> condition) && {
|
2019-08-25 23:55:59 -04:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
|
|
|
|
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
|
|
|
|
|
temp.emplace_back(std::move(*this).TransferOwnership());
|
|
|
|
|
return ParallelRaceGroup(std::move(temp));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
SequentialCommandGroup Command::BeforeStarting(
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SequentialCommandGroup Command::BeforeStarting(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::function<void()> toRun, wpi::span<Subsystem* const> requirements) && {
|
2019-08-25 23:55:59 -04:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
2019-11-20 22:44:18 -08:00
|
|
|
temp.emplace_back(
|
|
|
|
|
std::make_unique<InstantCommand>(std::move(toRun), requirements));
|
2019-08-25 23:55:59 -04:00
|
|
|
temp.emplace_back(std::move(*this).TransferOwnership());
|
|
|
|
|
return SequentialCommandGroup(std::move(temp));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
SequentialCommandGroup 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SequentialCommandGroup Command::AndThen(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::function<void()> toRun, wpi::span<Subsystem* const> requirements) && {
|
2019-08-25 23:55:59 -04:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
|
|
|
|
temp.emplace_back(std::move(*this).TransferOwnership());
|
2019-11-20 22:44:18 -08:00
|
|
|
temp.emplace_back(
|
|
|
|
|
std::make_unique<InstantCommand>(std::move(toRun), requirements));
|
2019-08-25 23:55:59 -04:00
|
|
|
return SequentialCommandGroup(std::move(temp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-04-28 19:38:38 +03:00
|
|
|
EndlessCommand Command::Endlessly() && {
|
|
|
|
|
return EndlessCommand(std::move(*this).TransferOwnership());
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 17:20:17 -05:00
|
|
|
RepeatCommand Command::Repeatedly() && {
|
2022-04-08 08:02:08 +03:00
|
|
|
return RepeatCommand(std::move(*this).TransferOwnership());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
ProxyScheduleCommand Command::AsProxy() {
|
|
|
|
|
return ProxyScheduleCommand(this);
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2022-05-24 10:22:19 -06:00
|
|
|
ConditionalCommand Command::Unless(std::function<bool()> condition) && {
|
|
|
|
|
return ConditionalCommand(std::make_unique<InstantCommand>(),
|
|
|
|
|
std::move(*this).TransferOwnership(),
|
|
|
|
|
std::move(condition));
|
|
|
|
|
}
|
|
|
|
|
|
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
|