Skip to content

Commit ebb1e5e

Browse files
author
James Hagborg
committed
Add wait for duration, match time, and condition, as well as ifThenElse
1 parent 0f0d24f commit ebb1e5e

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package org.usfirst.frc.team69.util;
22

3+
import java.util.function.BooleanSupplier;
4+
35
import edu.wpi.first.wpilibj.command.Command;
46
import edu.wpi.first.wpilibj.command.CommandGroup;
7+
import edu.wpi.first.wpilibj.command.ConditionalCommand;
8+
import edu.wpi.first.wpilibj.command.InstantCommand;
9+
import edu.wpi.first.wpilibj.command.WaitCommand;
10+
import edu.wpi.first.wpilibj.command.WaitUntilCommand;
511

612
/**
713
* A builder class to create commands in autonomous. Currently, it wraps a
@@ -10,7 +16,7 @@
1016
* To build a command, add child commands using {@link #sequential(Command)}
1117
* and {@link #parallel(Command)}, then get a command using {@link #build()}.
1218
*
13-
* @author James
19+
* @author James Hagborg
1420
*
1521
*/
1622
public class CommandBuilder {
@@ -86,6 +92,79 @@ public CommandBuilder parallel(Command command, double timeout) {
8692
return this;
8793
}
8894

95+
/**
96+
* Wait for a given duration.
97+
*
98+
* @param seconds The time to wait, in seconds
99+
* @return This CommandBuilder object
100+
*/
101+
public CommandBuilder waitForDuration(double seconds) {
102+
m_cmdGroup.addSequential(new WaitCommand(seconds));
103+
return this;
104+
}
105+
106+
/**
107+
* Wait until a given match time.
108+
*
109+
* @param seconds The match time to wait for, in seconds from the start
110+
* @return This CommandBuilder object
111+
*/
112+
public CommandBuilder waitForMatchTime(double seconds) {
113+
m_cmdGroup.addSequential(new WaitUntilCommand(seconds));
114+
return this;
115+
}
116+
117+
/**
118+
* Wait until an arbitrary condition is met.
119+
*
120+
* @param condition The condition to wait for.
121+
* @return This CommandBuilder object
122+
*/
123+
public CommandBuilder waitForCondition(BooleanSupplier condition) {
124+
m_cmdGroup.addSequential(QuickCommand.waitFor(condition));
125+
return this;
126+
}
127+
128+
/**
129+
* Wait until an arbitrary condition is met, or until a timeout expires.
130+
*
131+
* @param condition The condition to wait for
132+
* @param timeout The timeout in seconds
133+
* @return This CommandBuilder object
134+
*/
135+
public CommandBuilder waitForCondition(BooleanSupplier condition, double timeout) {
136+
m_cmdGroup.addSequential(QuickCommand.waitFor(condition), timeout);
137+
return this;
138+
}
139+
140+
/**
141+
* Run a command if and only if a condition is met at runtime.
142+
*
143+
* @param condition The condition to check
144+
* @param ifTrue The command to run if the condition is true
145+
* @return This CommandBuilder object
146+
*/
147+
public CommandBuilder ifThen(BooleanSupplier condition, Command ifTrue) {
148+
return ifThenElse(condition, ifTrue, new InstantCommand());
149+
}
150+
151+
/**
152+
* Run one of two commands, depending on if a condition is true.
153+
*
154+
* @param condition The condition to check
155+
* @param ifTrue The command to run if the condition is true
156+
* @param ifFalse The command to run if the condition is false
157+
* @return This CommandBuilder object
158+
*/
159+
public CommandBuilder ifThenElse(BooleanSupplier condition, Command ifTrue, Command ifFalse) {
160+
m_cmdGroup.addSequential(new ConditionalCommand(ifTrue, ifFalse) {
161+
@Override protected boolean condition() {
162+
return condition.getAsBoolean();
163+
}
164+
});
165+
return this;
166+
}
167+
89168
/**
90169
* Build a command. Note that for simplicity, calling this method
91170
* multiple times will return the same {@link Command} object. This

0 commit comments

Comments
 (0)