Skip to content

Commit

Permalink
[command based] Update requirements consistently
Browse files Browse the repository at this point in the history
Resolves #6291
  • Loading branch information
spacey-sooty committed Jun 30, 2024
1 parent a2beb75 commit dd05362
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ public final void addRequirements(Subsystem... requirements) {
}
}

/**
* 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(Set<Subsystem> requirements) {
for (Subsystem requirement : requirements) {
m_requirements.add(requireNonNullParam(requirement, "requirement", "addRequirements"));
}
}

/**
* Gets the name of this Command.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public ConditionalCommand(Command onTrue, Command onFalse, BooleanSupplier condi

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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;

Expand Down Expand Up @@ -55,7 +54,7 @@ public PIDCommand(
m_useOutput = useOutput;
m_measurement = measurementSource;
m_setpoint = setpointSource;
m_requirements.addAll(Set.of(requirements));
addRequirements(requirements);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public final void addCommands(Command... commands) {
"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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public final void addCommands(Command... commands) {
"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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final void addCommands(Command... commands) {
"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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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;
Expand Down Expand Up @@ -58,7 +57,7 @@ public ProfiledPIDCommand(
m_useOutput = useOutput;
m_measurement = measurementSource;
m_goal = goalSource;
m_requirements.addAll(Set.of(requirements));
addRequirements(requirements);
}

/**
Expand Down Expand Up @@ -86,7 +85,7 @@ public ProfiledPIDCommand(
m_useOutput = useOutput;
m_measurement = measurementSource;
m_goal = () -> new State(goalSource.getAsDouble(), 0);
m_requirements.addAll(Set.of(requirements));
addRequirements(requirements);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public SelectCommand(Map<K, Command> commands, Supplier<? extends K> selector) {
.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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final void addCommands(Command... commands) {

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;
Expand Down

0 comments on commit dd05362

Please sign in to comment.