[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:
Starlight220
2022-12-07 07:13:31 +02:00
committed by GitHub
parent f18fd41ac3
commit 4bbdbdfb48
46 changed files with 450 additions and 472 deletions

View File

@@ -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(