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
|
|
|
|
|
|
|
|
void ParallelRaceGroup::AddCommands(
|
|
|
|
|
std::vector<std::unique_ptr<Command>>&& commands) {
|
|
|
|
|
if (!RequireUngrouped(commands)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isRunning) {
|
2021-05-23 19:33:33 -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())) {
|
|
|
|
|
command->SetGrouped(true);
|
|
|
|
|
AddRequirements(command->GetRequirements());
|
|
|
|
|
m_runWhenDisabled &= command->RunsWhenDisabled();
|
2019-11-05 20:52:49 -08:00
|
|
|
m_commands.emplace_back(std::move(command));
|
2019-08-25 23:55:59 -04:00
|
|
|
} else {
|
2021-05-23 19:33:33 -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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|