mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[commands] Move GroupedCommands to CommandScheduler (#4728)
Move the command group checking functionality from CommandGroupBase into CommandScheduler. Update references to grouping as composition for clarity (because explicitly grouping isn't the only way to do it). Deprecate the static factory methods parallel, race, and deadline in CommandGroupBase in favor of the identical ones in Commands.
This commit is contained in:
@@ -25,7 +25,7 @@ Command::~Command() {
|
||||
}
|
||||
|
||||
Command& Command::operator=(const Command& rhs) {
|
||||
m_isGrouped = false;
|
||||
m_isComposed = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -135,12 +135,20 @@ std::string Command::GetName() const {
|
||||
|
||||
void Command::SetName(std::string_view name) {}
|
||||
|
||||
bool Command::IsComposed() const {
|
||||
return m_isComposed;
|
||||
}
|
||||
|
||||
void Command::SetComposed(bool isComposed) {
|
||||
m_isComposed = isComposed;
|
||||
}
|
||||
|
||||
bool Command::IsGrouped() const {
|
||||
return m_isGrouped;
|
||||
return IsComposed();
|
||||
}
|
||||
|
||||
void Command::SetGrouped(bool grouped) {
|
||||
m_isGrouped = grouped;
|
||||
SetComposed(grouped);
|
||||
}
|
||||
|
||||
namespace frc2 {
|
||||
|
||||
@@ -5,44 +5,3 @@
|
||||
#include "frc2/command/CommandGroupBase.h"
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
bool CommandGroupBase::RequireUngrouped(const Command& command) {
|
||||
if (command.IsGrouped()) {
|
||||
throw FRC_MakeError(
|
||||
frc::err::CommandIllegalUse,
|
||||
"Commands cannot be added to more than one CommandGroup");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CommandGroupBase::RequireUngrouped(const Command* command) {
|
||||
return RequireUngrouped(*command);
|
||||
}
|
||||
|
||||
bool CommandGroupBase::RequireUngrouped(
|
||||
std::span<const std::unique_ptr<Command>> commands) {
|
||||
bool allUngrouped = true;
|
||||
for (auto&& command : commands) {
|
||||
allUngrouped &= !command.get()->IsGrouped();
|
||||
}
|
||||
if (!allUngrouped) {
|
||||
throw FRC_MakeError(
|
||||
frc::err::CommandIllegalUse,
|
||||
"Commands cannot be added to more than one CommandGroup");
|
||||
}
|
||||
return allUngrouped;
|
||||
}
|
||||
|
||||
bool CommandGroupBase::RequireUngrouped(
|
||||
std::initializer_list<const Command*> commands) {
|
||||
bool allUngrouped = true;
|
||||
for (auto&& command : commands) {
|
||||
allUngrouped &= !command->IsGrouped();
|
||||
}
|
||||
if (!allUngrouped) {
|
||||
throw FRC_MakeError(
|
||||
frc::err::CommandIllegalUse,
|
||||
"Commands cannot be added to more than one CommandGroup");
|
||||
}
|
||||
return allUngrouped;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
|
||||
#include "frc2/command/CommandGroupBase.h"
|
||||
#include "frc2/command/CommandPtr.h"
|
||||
#include "frc2/command/Subsystem.h"
|
||||
|
||||
@@ -118,12 +117,8 @@ void CommandScheduler::Schedule(Command* command) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (command->IsGrouped()) {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
"A command that is part of a command group "
|
||||
"cannot be independently scheduled");
|
||||
return;
|
||||
}
|
||||
RequireUngrouped(command);
|
||||
|
||||
if (m_impl->disabled ||
|
||||
(frc::RobotState::IsDisabled() && !command->RunsWhenDisabled()) ||
|
||||
m_impl->scheduledCommands.contains(command)) {
|
||||
@@ -307,6 +302,7 @@ void CommandScheduler::SetDefaultCommand(Subsystem* subsystem,
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse, "{}",
|
||||
"Default commands must require their subsystem!");
|
||||
}
|
||||
RequireUngrouped(defaultCommand.get());
|
||||
|
||||
SetDefaultCommandImpl(subsystem, std::move(defaultCommand).Unwrap());
|
||||
}
|
||||
@@ -436,6 +432,28 @@ void CommandScheduler::OnCommandFinish(Action action) {
|
||||
m_impl->finishActions.emplace_back(std::move(action));
|
||||
}
|
||||
|
||||
void CommandScheduler::RequireUngrouped(const Command* command) {
|
||||
if (command->IsComposed()) {
|
||||
throw FRC_MakeError(
|
||||
frc::err::CommandIllegalUse,
|
||||
"Commands cannot be added to more than one CommandGroup");
|
||||
}
|
||||
}
|
||||
|
||||
void CommandScheduler::RequireUngrouped(
|
||||
std::span<const std::unique_ptr<Command>> commands) {
|
||||
for (auto&& command : commands) {
|
||||
RequireUngrouped(command.get());
|
||||
}
|
||||
}
|
||||
|
||||
void CommandScheduler::RequireUngrouped(
|
||||
std::initializer_list<const Command*> commands) {
|
||||
for (auto&& command : commands) {
|
||||
RequireUngrouped(command);
|
||||
}
|
||||
}
|
||||
|
||||
void CommandScheduler::InitSendable(nt::NTSendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Scheduler");
|
||||
builder.SetUpdateTable(
|
||||
|
||||
@@ -10,15 +10,14 @@ ConditionalCommand::ConditionalCommand(std::unique_ptr<Command>&& onTrue,
|
||||
std::unique_ptr<Command>&& onFalse,
|
||||
std::function<bool()> condition)
|
||||
: m_condition{std::move(condition)} {
|
||||
if (!CommandGroupBase::RequireUngrouped({onTrue.get(), onFalse.get()})) {
|
||||
return;
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(
|
||||
{onTrue.get(), onFalse.get()});
|
||||
|
||||
m_onTrue = std::move(onTrue);
|
||||
m_onFalse = std::move(onFalse);
|
||||
|
||||
m_onTrue->SetGrouped(true);
|
||||
m_onFalse->SetGrouped(true);
|
||||
m_onTrue->SetComposed(true);
|
||||
m_onFalse->SetComposed(true);
|
||||
|
||||
m_runsWhenDisabled &= m_onTrue->RunsWhenDisabled();
|
||||
m_runsWhenDisabled &= m_onFalse->RunsWhenDisabled();
|
||||
|
||||
@@ -63,11 +63,7 @@ Command::InterruptionBehavior ParallelCommandGroup::GetInterruptionBehavior()
|
||||
|
||||
void ParallelCommandGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
for (auto&& command : commands) {
|
||||
if (!RequireUngrouped(*command)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(commands);
|
||||
|
||||
if (isRunning) {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
@@ -77,7 +73,7 @@ void ParallelCommandGroup::AddCommands(
|
||||
|
||||
for (auto&& command : commands) {
|
||||
if (RequirementsDisjoint(this, command.get())) {
|
||||
command->SetGrouped(true);
|
||||
command->SetComposed(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
|
||||
@@ -60,9 +60,7 @@ Command::InterruptionBehavior ParallelDeadlineGroup::GetInterruptionBehavior()
|
||||
|
||||
void ParallelDeadlineGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
if (!RequireUngrouped(commands)) {
|
||||
return;
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(commands);
|
||||
|
||||
if (!m_finished) {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
@@ -72,7 +70,7 @@ void ParallelDeadlineGroup::AddCommands(
|
||||
|
||||
for (auto&& command : commands) {
|
||||
if (RequirementsDisjoint(this, command.get())) {
|
||||
command->SetGrouped(true);
|
||||
command->SetComposed(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
@@ -90,7 +88,7 @@ void ParallelDeadlineGroup::AddCommands(
|
||||
|
||||
void ParallelDeadlineGroup::SetDeadline(std::unique_ptr<Command>&& deadline) {
|
||||
m_deadline = deadline.get();
|
||||
m_deadline->SetGrouped(true);
|
||||
m_deadline->SetComposed(true);
|
||||
m_commands.emplace_back(std::move(deadline), false);
|
||||
AddRequirements(m_deadline->GetRequirements());
|
||||
m_runWhenDisabled &= m_deadline->RunsWhenDisabled();
|
||||
|
||||
@@ -50,9 +50,7 @@ Command::InterruptionBehavior ParallelRaceGroup::GetInterruptionBehavior()
|
||||
|
||||
void ParallelRaceGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
if (!RequireUngrouped(commands)) {
|
||||
return;
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(commands);
|
||||
|
||||
if (isRunning) {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
@@ -62,7 +60,7 @@ void ParallelRaceGroup::AddCommands(
|
||||
|
||||
for (auto&& command : commands) {
|
||||
if (RequirementsDisjoint(this, command.get())) {
|
||||
command->SetGrouped(true);
|
||||
command->SetComposed(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
|
||||
@@ -7,11 +7,9 @@
|
||||
using namespace frc2;
|
||||
|
||||
PerpetualCommand::PerpetualCommand(std::unique_ptr<Command>&& command) {
|
||||
if (!CommandGroupBase::RequireUngrouped(*command)) {
|
||||
return;
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(command.get());
|
||||
m_command = std::move(command);
|
||||
m_command->SetGrouped(true);
|
||||
m_command->SetComposed(true);
|
||||
AddRequirements(m_command->GetRequirements());
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,9 @@
|
||||
using namespace frc2;
|
||||
|
||||
RepeatCommand::RepeatCommand(std::unique_ptr<Command>&& command) {
|
||||
if (!CommandGroupBase::RequireUngrouped(*command)) {
|
||||
return;
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(command.get());
|
||||
m_command = std::move(command);
|
||||
m_command->SetGrouped(true);
|
||||
m_command->SetComposed(true);
|
||||
AddRequirements(m_command->GetRequirements());
|
||||
}
|
||||
|
||||
|
||||
@@ -60,9 +60,7 @@ Command::InterruptionBehavior SequentialCommandGroup::GetInterruptionBehavior()
|
||||
|
||||
void SequentialCommandGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
if (!RequireUngrouped(commands)) {
|
||||
return;
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(commands);
|
||||
|
||||
if (m_currentCommandIndex != invalid_index) {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
@@ -71,7 +69,7 @@ void SequentialCommandGroup::AddCommands(
|
||||
}
|
||||
|
||||
for (auto&& command : commands) {
|
||||
command->SetGrouped(true);
|
||||
command->SetComposed(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
|
||||
@@ -9,11 +9,9 @@
|
||||
using namespace frc2;
|
||||
|
||||
WrapperCommand::WrapperCommand(std::unique_ptr<Command>&& command) {
|
||||
if (!CommandGroupBase::RequireUngrouped(*command)) {
|
||||
return;
|
||||
}
|
||||
CommandScheduler::GetInstance().RequireUngrouped(command.get());
|
||||
m_command = std::move(command);
|
||||
m_command->SetGrouped(true);
|
||||
m_command->SetComposed(true);
|
||||
}
|
||||
|
||||
void WrapperCommand::Initialize() {
|
||||
|
||||
Reference in New Issue
Block a user