[commands] Update requirements consistently (#6304)

This commit is contained in:
Jade
2024-07-12 06:01:54 +08:00
committed by GitHub
parent bf75c03218
commit 967105568c
10 changed files with 25 additions and 13 deletions

View File

@@ -13,6 +13,7 @@ import edu.wpi.first.util.function.BooleanConsumer;
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.util.sendable.SendableRegistry;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BooleanSupplier;
@@ -96,6 +97,19 @@ public abstract class Command implements Sendable {
}
}
/**
* Adds the specified subsystems to the requirements of the command. The scheduler will prevent
* two commands that require the same subsystem from being scheduled simultaneously.
*
* <p>Note that the scheduler determines the requirements of a command when it is scheduled, so
* this method should normally be called from the command's constructor.
*
* @param requirements the requirements to add
*/
public final void addRequirements(Collection<Subsystem> requirements) {
m_requirements.addAll(requirements);
}
/**
* Gets the name of this Command.
*

View File

@@ -39,8 +39,8 @@ public class ConditionalCommand extends Command {
CommandScheduler.getInstance().registerComposedCommands(onTrue, onFalse);
m_requirements.addAll(m_onTrue.getRequirements());
m_requirements.addAll(m_onFalse.getRequirements());
addRequirements(m_onTrue.getRequirements());
addRequirements(m_onFalse.getRequirements());
}
@Override

View File

@@ -7,7 +7,6 @@ package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.math.controller.PIDController;
import java.util.Set;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleSupplier;
@@ -55,7 +54,7 @@ public class PIDCommand extends Command {
m_useOutput = useOutput;
m_measurement = measurementSource;
m_setpoint = setpointSource;
m_requirements.addAll(Set.of(requirements));
addRequirements(requirements);
}
/**

View File

@@ -55,7 +55,7 @@ public class ParallelCommandGroup extends Command {
"Multiple commands in a parallel composition cannot require the same subsystems");
}
m_commands.put(command, false);
m_requirements.addAll(command.getRequirements());
addRequirements(command.getRequirements());
m_runWhenDisabled &= command.runsWhenDisabled();
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
m_interruptBehavior = InterruptionBehavior.kCancelSelf;

View File

@@ -85,7 +85,7 @@ public class ParallelDeadlineGroup extends Command {
"Multiple commands in a parallel group cannot require the same subsystems");
}
m_commands.put(command, false);
m_requirements.addAll(command.getRequirements());
addRequirements(command.getRequirements());
m_runWhenDisabled &= command.runsWhenDisabled();
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
m_interruptBehavior = InterruptionBehavior.kCancelSelf;

View File

@@ -56,7 +56,7 @@ public class ParallelRaceGroup extends Command {
"Multiple commands in a parallel composition cannot require the same subsystems");
}
m_commands.add(command);
m_requirements.addAll(command.getRequirements());
addRequirements(command.getRequirements());
m_runWhenDisabled &= command.runsWhenDisabled();
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
m_interruptBehavior = InterruptionBehavior.kCancelSelf;

View File

@@ -8,7 +8,6 @@ import static edu.wpi.first.math.trajectory.TrapezoidProfile.State;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.math.controller.ProfiledPIDController;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;
@@ -58,7 +57,7 @@ public class ProfiledPIDCommand extends Command {
m_useOutput = useOutput;
m_measurement = measurementSource;
m_goal = goalSource;
m_requirements.addAll(Set.of(requirements));
addRequirements(requirements);
}
/**
@@ -86,7 +85,7 @@ public class ProfiledPIDCommand extends Command {
m_useOutput = useOutput;
m_measurement = measurementSource;
m_goal = () -> new State(goalSource.getAsDouble(), 0);
m_requirements.addAll(Set.of(requirements));
addRequirements(requirements);
}
/**

View File

@@ -33,7 +33,7 @@ public class RepeatCommand extends Command {
public RepeatCommand(Command command) {
m_command = requireNonNullParam(command, "command", "RepeatCommand");
CommandScheduler.getInstance().registerComposedCommands(command);
m_requirements.addAll(command.getRequirements());
addRequirements(command.getRequirements());
setName("Repeat(" + command.getName() + ")");
}

View File

@@ -47,7 +47,7 @@ public class SelectCommand<K> extends Command {
.registerComposedCommands(commands.values().toArray(new Command[] {}));
for (Command command : m_commands.values()) {
m_requirements.addAll(command.getRequirements());
addRequirements(command.getRequirements());
m_runsWhenDisabled &= command.runsWhenDisabled();
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
m_interruptBehavior = InterruptionBehavior.kCancelSelf;

View File

@@ -49,7 +49,7 @@ public class SequentialCommandGroup extends Command {
for (Command command : commands) {
m_commands.add(command);
m_requirements.addAll(command.getRequirements());
addRequirements(command.getRequirements());
m_runWhenDisabled &= command.runsWhenDisabled();
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
m_interruptBehavior = InterruptionBehavior.kCancelSelf;