-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathSequentialCommandGroup.java
120 lines (102 loc) · 3.49 KB
/
SequentialCommandGroup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 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.
package edu.wpi.first.wpilibj2.command;
import edu.wpi.first.util.sendable.SendableBuilder;
import java.util.ArrayList;
import java.util.List;
/**
* A command composition that runs a list of commands in sequence.
*
* <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.
*
* <p>This class is provided by the NewCommands VendorDep
*/
public class SequentialCommandGroup extends Command {
private final List<Command> m_commands = new ArrayList<>();
private int m_currentCommandIndex = -1;
private boolean m_runWhenDisabled = true;
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
/**
* Creates a new SequentialCommandGroup. The given commands will be run sequentially, with the
* composition finishing when the last command finishes.
*
* @param commands the commands to include in this composition.
*/
@SuppressWarnings("this-escape")
public SequentialCommandGroup(Command... commands) {
addCommands(commands);
}
/**
* Adds the given commands to the group.
*
* @param commands Commands to add, in order of execution.
*/
@SuppressWarnings("PMD.UseArraysAsList")
public final void addCommands(Command... commands) {
if (m_currentCommandIndex != -1) {
throw new IllegalStateException(
"Commands cannot be added to a composition while it's running");
}
CommandScheduler.getInstance().registerComposedCommands(commands);
for (Command command : commands) {
m_commands.add(command);
addRequirements(command.getRequirements());
m_runWhenDisabled &= command.runsWhenDisabled();
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
}
}
}
@Override
public final void initialize() {
m_currentCommandIndex = 0;
if (!m_commands.isEmpty()) {
m_commands.get(0).initialize();
}
}
@Override
public final void execute() {
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
public final void end(boolean interrupted) {
if (interrupted
&& !m_commands.isEmpty()
&& m_currentCommandIndex > -1
&& m_currentCommandIndex < m_commands.size()) {
m_commands.get(m_currentCommandIndex).end(true);
}
m_currentCommandIndex = -1;
}
@Override
public final boolean isFinished() {
return m_currentCommandIndex == m_commands.size() || m_currentCommandIndex == -1;
}
@Override
public boolean runsWhenDisabled() {
return m_runWhenDisabled;
}
@Override
public InterruptionBehavior getInterruptionBehavior() {
return m_interruptBehavior;
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addIntegerProperty("index", () -> m_currentCommandIndex, null);
}
}