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
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.command2;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2025-11-07 19:57:21 -05:00
|
|
|
import org.wpilib.util.sendable.SendableBuilder;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
|
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* A command composition that runs a list of commands in sequence.
|
2019-08-25 17:47:07 -04:00
|
|
|
*
|
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 SequentialCommandGroup extends Command {
|
2019-08-25 17:47:07 -04:00
|
|
|
private final List<Command> m_commands = new ArrayList<>();
|
|
|
|
|
private int m_currentCommandIndex = -1;
|
|
|
|
|
private boolean m_runWhenDisabled = 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 SequentialCommandGroup. The given commands will be run sequentially, with the
|
2022-12-07 07:13:31 +02:00
|
|
|
* composition finishing when the last command finishes.
|
2019-08-25 17:47:07 -04:00
|
|
|
*
|
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 SequentialCommandGroup(Command... commands) {
|
|
|
|
|
addCommands(commands);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 12:56:18 -04:00
|
|
|
/**
|
|
|
|
|
* Adds the given commands to the group.
|
|
|
|
|
*
|
|
|
|
|
* @param commands Commands to add, in order of execution.
|
|
|
|
|
*/
|
2024-06-09 01:08:23 -04:00
|
|
|
@SuppressWarnings("PMD.UseArraysAsList")
|
2019-08-25 17:47:07 -04:00
|
|
|
public final void addCommands(Command... commands) {
|
|
|
|
|
if (m_currentCommandIndex != -1) {
|
|
|
|
|
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) {
|
|
|
|
|
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_currentCommandIndex = 0;
|
|
|
|
|
|
|
|
|
|
if (!m_commands.isEmpty()) {
|
|
|
|
|
m_commands.get(0).initialize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 19:15:28 +03:00
|
|
|
public final void execute() {
|
2019-08-25 17:47:07 -04:00
|
|
|
if (m_commands.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command currentCommand = m_commands.get(m_currentCommandIndex);
|
|
|
|
|
|
|
|
|
|
currentCommand.execute();
|
|
|
|
|
if (currentCommand.isFinished()) {
|
|
|
|
|
currentCommand.end(false);
|
|
|
|
|
m_currentCommandIndex++;
|
|
|
|
|
if (m_currentCommandIndex < m_commands.size()) {
|
|
|
|
|
m_commands.get(m_currentCommandIndex).initialize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 19:15:28 +03:00
|
|
|
public final void end(boolean interrupted) {
|
2020-12-29 22:45:16 -08:00
|
|
|
if (interrupted
|
|
|
|
|
&& !m_commands.isEmpty()
|
|
|
|
|
&& m_currentCommandIndex > -1
|
|
|
|
|
&& m_currentCommandIndex < m_commands.size()) {
|
2019-08-25 17:47:07 -04:00
|
|
|
m_commands.get(m_currentCommandIndex).end(true);
|
|
|
|
|
}
|
|
|
|
|
m_currentCommandIndex = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 19:15:28 +03:00
|
|
|
public final boolean isFinished() {
|
2019-08-25 17:47:07 -04:00
|
|
|
return m_currentCommandIndex == m_commands.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean runsWhenDisabled() {
|
|
|
|
|
return m_runWhenDisabled;
|
|
|
|
|
}
|
2022-11-28 02:23:56 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public InterruptionBehavior getInterruptionBehavior() {
|
|
|
|
|
return m_interruptBehavior;
|
|
|
|
|
}
|
2022-12-16 04:28:52 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initSendable(SendableBuilder builder) {
|
|
|
|
|
super.initSendable(builder);
|
|
|
|
|
|
|
|
|
|
builder.addIntegerProperty("index", () -> m_currentCommandIndex, null);
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|