Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commands] Update requirements consistently #6304

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -96,6 +97,19 @@ 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(Collection<Subsystem> requirements) {
m_requirements.addAll(requirements);
}

/**
* 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
Loading