|
| 1 | +// Copyright (c) FIRST and other WPILib contributors. |
| 2 | +// Open Source Software; you can modify and/or share it under the terms of |
| 3 | +// the WPILib BSD license file in the root directory of this project. |
| 4 | + |
| 5 | +#include "frc2/command/Command.h" |
| 6 | + |
| 7 | +#include "frc2/command/CommandScheduler.h" |
| 8 | +#include "frc2/command/InstantCommand.h" |
| 9 | +#include "frc2/command/ParallelCommandGroup.h" |
| 10 | +#include "frc2/command/ParallelDeadlineGroup.h" |
| 11 | +#include "frc2/command/ParallelRaceGroup.h" |
| 12 | +#include "frc2/command/PerpetualCommand.h" |
| 13 | +#include "frc2/command/ProxyScheduleCommand.h" |
| 14 | +#include "frc2/command/SequentialCommandGroup.h" |
| 15 | +#include "frc2/command/WaitCommand.h" |
| 16 | +#include "frc2/command/WaitUntilCommand.h" |
| 17 | + |
| 18 | +using namespace frc2; |
| 19 | + |
| 20 | +Command::~Command() { |
| 21 | + CommandScheduler::GetInstance().Cancel(this); |
| 22 | +} |
| 23 | + |
| 24 | +Command::Command(const Command& rhs) : ErrorBase(rhs) {} |
| 25 | + |
| 26 | +Command& Command::operator=(const Command& rhs) { |
| 27 | + ErrorBase::operator=(rhs); |
| 28 | + m_isGrouped = false; |
| 29 | + return *this; |
| 30 | +} |
| 31 | + |
| 32 | +void Command::Initialize() {} |
| 33 | +void Command::Execute() {} |
| 34 | +void Command::End(bool interrupted) {} |
| 35 | + |
| 36 | +ParallelRaceGroup Command::WithTimeout(units::second_t duration) && { |
| 37 | + std::vector<std::unique_ptr<Command>> temp; |
| 38 | + temp.emplace_back(std::make_unique<WaitCommand>(duration)); |
| 39 | + temp.emplace_back(std::move(*this).TransferOwnership()); |
| 40 | + return ParallelRaceGroup(std::move(temp)); |
| 41 | +} |
| 42 | + |
| 43 | +ParallelRaceGroup Command::WithInterrupt(std::function<bool()> condition) && { |
| 44 | + std::vector<std::unique_ptr<Command>> temp; |
| 45 | + temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition))); |
| 46 | + temp.emplace_back(std::move(*this).TransferOwnership()); |
| 47 | + return ParallelRaceGroup(std::move(temp)); |
| 48 | +} |
| 49 | + |
| 50 | +SequentialCommandGroup Command::BeforeStarting( |
| 51 | + std::function<void()> toRun, |
| 52 | + std::initializer_list<Subsystem*> requirements) && { |
| 53 | + return std::move(*this).BeforeStarting( |
| 54 | + std::move(toRun), |
| 55 | + wpi::makeArrayRef(requirements.begin(), requirements.end())); |
| 56 | +} |
| 57 | + |
| 58 | +SequentialCommandGroup Command::BeforeStarting( |
| 59 | + std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && { |
| 60 | + std::vector<std::unique_ptr<Command>> temp; |
| 61 | + temp.emplace_back( |
| 62 | + std::make_unique<InstantCommand>(std::move(toRun), requirements)); |
| 63 | + temp.emplace_back(std::move(*this).TransferOwnership()); |
| 64 | + return SequentialCommandGroup(std::move(temp)); |
| 65 | +} |
| 66 | + |
| 67 | +SequentialCommandGroup Command::AndThen( |
| 68 | + std::function<void()> toRun, |
| 69 | + std::initializer_list<Subsystem*> requirements) && { |
| 70 | + return std::move(*this).AndThen( |
| 71 | + std::move(toRun), |
| 72 | + wpi::makeArrayRef(requirements.begin(), requirements.end())); |
| 73 | +} |
| 74 | + |
| 75 | +SequentialCommandGroup Command::AndThen( |
| 76 | + std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && { |
| 77 | + std::vector<std::unique_ptr<Command>> temp; |
| 78 | + temp.emplace_back(std::move(*this).TransferOwnership()); |
| 79 | + temp.emplace_back( |
| 80 | + std::make_unique<InstantCommand>(std::move(toRun), requirements)); |
| 81 | + return SequentialCommandGroup(std::move(temp)); |
| 82 | +} |
| 83 | + |
| 84 | +PerpetualCommand Command::Perpetually() && { |
| 85 | + return PerpetualCommand(std::move(*this).TransferOwnership()); |
| 86 | +} |
| 87 | + |
| 88 | +ProxyScheduleCommand Command::AsProxy() { |
| 89 | + return ProxyScheduleCommand(this); |
| 90 | +} |
| 91 | + |
| 92 | +void Command::Schedule(bool interruptible) { |
| 93 | + CommandScheduler::GetInstance().Schedule(interruptible, this); |
| 94 | +} |
| 95 | + |
| 96 | +void Command::Cancel() { |
| 97 | + CommandScheduler::GetInstance().Cancel(this); |
| 98 | +} |
| 99 | + |
| 100 | +bool Command::IsScheduled() const { |
| 101 | + return CommandScheduler::GetInstance().IsScheduled(this); |
| 102 | +} |
| 103 | + |
| 104 | +bool Command::HasRequirement(Subsystem* requirement) const { |
| 105 | + bool hasRequirement = false; |
| 106 | + for (auto&& subsystem : GetRequirements()) { |
| 107 | + hasRequirement |= requirement == subsystem; |
| 108 | + } |
| 109 | + return hasRequirement; |
| 110 | +} |
| 111 | + |
| 112 | +std::string Command::GetName() const { |
| 113 | + return GetTypeName(*this); |
| 114 | +} |
| 115 | + |
| 116 | +bool Command::IsGrouped() const { |
| 117 | + return m_isGrouped; |
| 118 | +} |
| 119 | + |
| 120 | +void Command::SetGrouped(bool grouped) { |
| 121 | + m_isGrouped = grouped; |
| 122 | +} |
| 123 | + |
| 124 | +namespace frc2 { |
| 125 | +bool RequirementsDisjoint(Command* first, Command* second) { |
| 126 | + bool disjoint = true; |
| 127 | + auto&& requirements = second->GetRequirements(); |
| 128 | + for (auto&& requirement : first->GetRequirements()) { |
| 129 | + disjoint &= requirements.find(requirement) == requirements.end(); |
| 130 | + } |
| 131 | + return disjoint; |
| 132 | +} |
| 133 | +} // namespace frc2 |
0 commit comments