2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-08-25 17:47:07 -04:00
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj2.command;
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
2024-06-01 12:01:15 -07:00
|
|
|
import java.util.LinkedHashSet;
|
2019-08-25 17:47:07 -04:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* A composition that runs a set of commands in parallel, ending when any one of the commands ends
|
2019-08-25 17:47:07 -04:00
|
|
|
* and interrupting all the others.
|
|
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* <p>The rules for command compositions apply: command instances that are passed to it cannot be
|
|
|
|
|
* added to any other composition or scheduled individually, and the composition requires all
|
|
|
|
|
* subsystems its components require.
|
2022-01-08 11:11:34 -08:00
|
|
|
*
|
|
|
|
|
* <p>This class is provided by the NewCommands VendorDep
|
2019-08-25 17:47:07 -04:00
|
|
|
*/
|
2023-07-14 01:12:01 -04:00
|
|
|
public class ParallelRaceGroup extends Command {
|
2024-06-01 12:01:15 -07:00
|
|
|
// LinkedHashSet guarantees we iterate over commands in the order they were added
|
|
|
|
|
private final Set<Command> m_commands = new LinkedHashSet<>();
|
2019-08-25 17:47:07 -04:00
|
|
|
private boolean m_runWhenDisabled = true;
|
|
|
|
|
private boolean m_finished = true;
|
2022-11-28 02:23:56 +02:00
|
|
|
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Creates a new ParallelCommandRace. The given commands will be executed simultaneously, and will
|
|
|
|
|
* "race to the finish" - the first command to finish ends the entire command, with all other
|
2019-08-25 17:47:07 -04:00
|
|
|
* commands being interrupted.
|
|
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* @param commands the commands to include in this composition.
|
2019-08-25 17:47:07 -04:00
|
|
|
*/
|
2024-07-13 06:52:30 -07:00
|
|
|
@SuppressWarnings("this-escape")
|
2019-08-25 17:47:07 -04:00
|
|
|
public ParallelRaceGroup(Command... commands) {
|
|
|
|
|
addCommands(commands);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 12:56:18 -04:00
|
|
|
/**
|
|
|
|
|
* Adds the given commands to the group.
|
|
|
|
|
*
|
|
|
|
|
* @param commands Commands to add to the group.
|
|
|
|
|
*/
|
2019-08-25 17:47:07 -04:00
|
|
|
public final void addCommands(Command... commands) {
|
|
|
|
|
if (!m_finished) {
|
|
|
|
|
throw new IllegalStateException(
|
2022-12-07 07:13:31 +02:00
|
|
|
"Commands cannot be added to a composition while it's running!");
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-07 07:13:31 +02:00
|
|
|
CommandScheduler.getInstance().registerComposedCommands(commands);
|
2019-08-25 17:47:07 -04:00
|
|
|
|
|
|
|
|
for (Command command : commands) {
|
2024-07-12 21:51:21 +08:00
|
|
|
if (!Collections.disjoint(command.getRequirements(), getRequirements())) {
|
2020-12-29 22:45:16 -08:00
|
|
|
throw new IllegalArgumentException(
|
2022-12-07 07:13:31 +02:00
|
|
|
"Multiple commands in a parallel composition cannot require the same subsystems");
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
m_commands.add(command);
|
2024-07-12 06:01:54 +08:00
|
|
|
addRequirements(command.getRequirements());
|
2019-08-25 17:47:07 -04:00
|
|
|
m_runWhenDisabled &= command.runsWhenDisabled();
|
2022-11-28 02:23:56 +02:00
|
|
|
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
|
|
|
|
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 19:15:28 +03:00
|
|
|
public final void initialize() {
|
2019-08-25 17:47:07 -04:00
|
|
|
m_finished = false;
|
|
|
|
|
for (Command command : m_commands) {
|
|
|
|
|
command.initialize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 19:15:28 +03:00
|
|
|
public final void execute() {
|
2019-08-25 17:47:07 -04:00
|
|
|
for (Command command : m_commands) {
|
|
|
|
|
command.execute();
|
|
|
|
|
if (command.isFinished()) {
|
|
|
|
|
m_finished = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 19:15:28 +03:00
|
|
|
public final void end(boolean interrupted) {
|
2019-08-25 17:47:07 -04:00
|
|
|
for (Command command : m_commands) {
|
2019-10-18 07:55:14 -07:00
|
|
|
command.end(!command.isFinished());
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 19:15:28 +03:00
|
|
|
public final boolean isFinished() {
|
2019-08-25 17:47:07 -04:00
|
|
|
return m_finished;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean runsWhenDisabled() {
|
|
|
|
|
return m_runWhenDisabled;
|
|
|
|
|
}
|
2022-11-28 02:23:56 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public InterruptionBehavior getInterruptionBehavior() {
|
|
|
|
|
return m_interruptBehavior;
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|