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/ParallelRaceGroup.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc2;
|
|
|
|
|
|
|
|
|
|
ParallelRaceGroup::ParallelRaceGroup(
|
|
|
|
|
std::vector<std::unique_ptr<Command>>&& commands) {
|
|
|
|
|
AddCommands(std::move(commands));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ParallelRaceGroup::Initialize() {
|
2020-02-08 10:26:06 -08:00
|
|
|
m_finished = false;
|
2019-08-25 23:55:59 -04:00
|
|
|
for (auto& commandRunning : m_commands) {
|
|
|
|
|
commandRunning->Initialize();
|
|
|
|
|
}
|
|
|
|
|
isRunning = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ParallelRaceGroup::Execute() {
|
|
|
|
|
for (auto& commandRunning : m_commands) {
|
|
|
|
|
commandRunning->Execute();
|
|
|
|
|
if (commandRunning->IsFinished()) {
|
|
|
|
|
m_finished = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ParallelRaceGroup::End(bool interrupted) {
|
|
|
|
|
for (auto& commandRunning : m_commands) {
|
2019-10-18 07:55:14 -07:00
|
|
|
commandRunning->End(!commandRunning->IsFinished());
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
isRunning = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool ParallelRaceGroup::IsFinished() {
|
|
|
|
|
return m_finished;
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool ParallelRaceGroup::RunsWhenDisabled() const {
|
|
|
|
|
return m_runWhenDisabled;
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2022-11-28 02:23:56 +02:00
|
|
|
Command::InterruptionBehavior ParallelRaceGroup::GetInterruptionBehavior()
|
|
|
|
|
const {
|
|
|
|
|
return m_interruptBehavior;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
void ParallelRaceGroup::AddCommands(
|
|
|
|
|
std::vector<std::unique_ptr<Command>>&& commands) {
|
2022-12-07 07:13:31 +02:00
|
|
|
CommandScheduler::GetInstance().RequireUngrouped(commands);
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
if (isRunning) {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
2021-04-18 20:35:29 -07:00
|
|
|
"Commands cannot be added to a CommandGroup "
|
|
|
|
|
"while the group is running");
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto&& command : commands) {
|
|
|
|
|
if (RequirementsDisjoint(this, command.get())) {
|
2022-12-07 07:13:31 +02:00
|
|
|
command->SetComposed(true);
|
2019-08-25 23:55:59 -04:00
|
|
|
AddRequirements(command->GetRequirements());
|
|
|
|
|
m_runWhenDisabled &= command->RunsWhenDisabled();
|
2022-11-28 02:23:56 +02:00
|
|
|
if (command->GetInterruptionBehavior() ==
|
|
|
|
|
Command::InterruptionBehavior::kCancelSelf) {
|
|
|
|
|
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
|
|
|
|
}
|
2019-11-05 20:52:49 -08:00
|
|
|
m_commands.emplace_back(std::move(command));
|
2019-08-25 23:55:59 -04:00
|
|
|
} else {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
2021-04-18 20:35:29 -07:00
|
|
|
"Multiple commands in a parallel group cannot "
|
|
|
|
|
"require the same subsystems");
|
2019-08-25 23:55:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|