Skip to content

Commit 24e9655

Browse files
author
James Hagborg
committed
Add release and releaseAll() methods for CommandBuilder
1 parent ebb1e5e commit 24e9655

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package edu.wpi.first.wpilibj.command;
2+
3+
import java.util.Enumeration;
4+
5+
/**
6+
* Helper class to get the requirements of a {@link Command}. This is necessary
7+
* because the getRequirements method is package-scope, but we want to use it
8+
* anyway.
9+
*
10+
* @author James Hagborg
11+
*
12+
*/
13+
public class GetRequirements {
14+
private GetRequirements() {}
15+
16+
/**
17+
* Get the requirements of a command
18+
* @param cmd The command to use
19+
* @return An enumeration of the requirements
20+
*/
21+
@SuppressWarnings("rawtypes")
22+
public static Enumeration getRequirements(Command cmd) {
23+
return cmd.getRequirements();
24+
}
25+
26+
}

src/main/java/org/usfirst/frc/team69/util/CommandBuilder.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.usfirst.frc.team69.util;
22

3+
import java.util.Collections;
34
import java.util.function.BooleanSupplier;
45

56
import edu.wpi.first.wpilibj.command.Command;
67
import edu.wpi.first.wpilibj.command.CommandGroup;
78
import edu.wpi.first.wpilibj.command.ConditionalCommand;
9+
import edu.wpi.first.wpilibj.command.GetRequirements;
810
import edu.wpi.first.wpilibj.command.InstantCommand;
11+
import edu.wpi.first.wpilibj.command.Subsystem;
912
import edu.wpi.first.wpilibj.command.WaitCommand;
1013
import edu.wpi.first.wpilibj.command.WaitUntilCommand;
1114

@@ -165,6 +168,39 @@ public CommandBuilder ifThenElse(BooleanSupplier condition, Command ifTrue, Comm
165168
return this;
166169
}
167170

171+
/**
172+
* End any command currently running on the subsystem. This is accomplished
173+
* by running a command which ends instantly, which requires the given
174+
* subsystem. Afterwards, the default command will run, if there is one.
175+
*
176+
* @param subsystem The subsystem to release
177+
* @return This CommandBuilder object
178+
*/
179+
public CommandBuilder release(Subsystem subsystem) {
180+
m_cmdGroup.addSequential(QuickCommand.release(subsystem));
181+
return this;
182+
}
183+
184+
/**
185+
* End all commands currently running in parallel. This is accomplished by
186+
* starting an command which requires all the subsystems used so far,
187+
* which ends instantly. This will have the effect of starting the default
188+
* commands for all these subsystems, if they have any.
189+
*
190+
* @return This CommandBuilder object
191+
*/
192+
@SuppressWarnings("unchecked")
193+
public CommandBuilder releaseAll() {
194+
m_cmdGroup.addSequential(new InstantCommand() {
195+
{
196+
for (Object req : Collections.list(GetRequirements.getRequirements(m_cmdGroup))) {
197+
requires((Subsystem) req);
198+
}
199+
}
200+
});
201+
return this;
202+
}
203+
168204
/**
169205
* Build a command. Note that for simplicity, calling this method
170206
* multiple times will return the same {@link Command} object. This

0 commit comments

Comments
 (0)