|
2 | 2 |
|
3 | 3 | import java.util.Collections; |
4 | 4 | import java.util.function.BooleanSupplier; |
| 5 | +import java.util.function.Consumer; |
5 | 6 |
|
6 | 7 | import edu.wpi.first.wpilibj.command.Command; |
7 | 8 | import edu.wpi.first.wpilibj.command.CommandGroup; |
@@ -64,11 +65,16 @@ public CommandBuilder sequential(Command command) { |
64 | 65 | * @param command |
65 | 66 | * The command to add in sequence |
66 | 67 | * @param timeout |
67 | | - * The timeout, in seconds |
| 68 | + * The timeout, in seconds. if negative, no timeout is used. |
68 | 69 | * @return This CommandBuilder object |
69 | 70 | */ |
70 | 71 | public CommandBuilder sequential(Command command, double timeout) { |
71 | | - m_cmdGroup.addSequential(command, timeout); |
| 72 | + if(timeout >= 0.0) { |
| 73 | + m_cmdGroup.addSequential(command, timeout); |
| 74 | + } else { |
| 75 | + m_cmdGroup.addSequential(command); |
| 76 | + } |
| 77 | + |
72 | 78 | return this; |
73 | 79 | } |
74 | 80 |
|
@@ -99,11 +105,32 @@ public CommandBuilder parallel(Command command) { |
99 | 105 | * @param command |
100 | 106 | * The command to add in parallel |
101 | 107 | * @param timeout |
102 | | - * The timeout, in seconds |
| 108 | + * The timeout, in seconds. if negative, no timeout is used. |
103 | 109 | * @return This CommandBuilder object |
104 | 110 | */ |
105 | 111 | public CommandBuilder parallel(Command command, double timeout) { |
106 | | - m_cmdGroup.addParallel(command, timeout); |
| 112 | + if(timeout >= 0.0) { |
| 113 | + m_cmdGroup.addParallel(command, timeout); |
| 114 | + } else { |
| 115 | + m_cmdGroup.addParallel(command); |
| 116 | + } |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Add a sequence of commands in parallel. Execution of the next command |
| 122 | + * will begin immediately, while this one continues to execute in parallel. |
| 123 | + * The command will be killed if the timeout expires. |
| 124 | + * |
| 125 | + * @param command |
| 126 | + * A function taking a CommandBuilder specifying the sequence of |
| 127 | + * commands |
| 128 | + * @return {CommandBuilder} |
| 129 | + */ |
| 130 | + public CommandBuilder parallel(Consumer<CommandBuilder> command) { |
| 131 | + CommandBuilder child = new CommandBuilder(); |
| 132 | + command.accept(child); |
| 133 | + m_cmdGroup.addParallel(child.build()); |
107 | 134 | return this; |
108 | 135 | } |
109 | 136 |
|
@@ -152,8 +179,7 @@ public CommandBuilder waitForCondition(BooleanSupplier condition) { |
152 | 179 | * The timeout in seconds |
153 | 180 | * @return This CommandBuilder object |
154 | 181 | */ |
155 | | - public CommandBuilder waitForCondition(BooleanSupplier condition, |
156 | | - double timeout) { |
| 182 | + public CommandBuilder waitForCondition(BooleanSupplier condition, double timeout) { |
157 | 183 | m_cmdGroup.addSequential(QuickCommand.waitFor(condition), timeout); |
158 | 184 | return this; |
159 | 185 | } |
@@ -182,8 +208,7 @@ public CommandBuilder ifThen(BooleanSupplier condition, Command ifTrue) { |
182 | 208 | * The command to run if the condition is false |
183 | 209 | * @return This CommandBuilder object |
184 | 210 | */ |
185 | | - public CommandBuilder ifThenElse(BooleanSupplier condition, Command ifTrue, |
186 | | - Command ifFalse) { |
| 211 | + public CommandBuilder ifThenElse(BooleanSupplier condition, Command ifTrue, Command ifFalse) { |
187 | 212 | m_cmdGroup.addSequential(new ConditionalCommand(ifTrue, ifFalse) { |
188 | 213 | @Override |
189 | 214 | protected boolean condition() { |
@@ -238,10 +263,8 @@ private static class Counter { |
238 | 263 | */ |
239 | 264 | public CommandBuilder forLoop(int count, Command body) { |
240 | 265 | Counter counter = new Counter(); |
241 | | - m_cmdGroup.addSequential( |
242 | | - QuickCommand.oneShot(null, () -> counter.value = 0)); |
243 | | - m_cmdGroup.addSequential( |
244 | | - new WhileCommand(() -> counter.value++ < count, body)); |
| 266 | + m_cmdGroup.addSequential(QuickCommand.oneShot(null, () -> counter.value = 0)); |
| 267 | + m_cmdGroup.addSequential(new WhileCommand(() -> counter.value++ < count, body)); |
245 | 268 | return this; |
246 | 269 | } |
247 | 270 |
|
@@ -271,8 +294,7 @@ public CommandBuilder release(Subsystem subsystem) { |
271 | 294 | public CommandBuilder releaseAll() { |
272 | 295 | m_cmdGroup.addSequential(new InstantCommand() { |
273 | 296 | { |
274 | | - for (Object req : Collections |
275 | | - .list(GetRequirements.getRequirements(m_cmdGroup))) { |
| 297 | + for (Object req : Collections.list(GetRequirements.getRequirements(m_cmdGroup))) { |
276 | 298 | requires((Subsystem) req); |
277 | 299 | } |
278 | 300 | } |
|
0 commit comments