|
1 | 1 | package org.usfirst.frc.team69.util; |
2 | 2 |
|
| 3 | +import java.util.function.BooleanSupplier; |
| 4 | + |
3 | 5 | import edu.wpi.first.wpilibj.command.Command; |
4 | 6 | 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; |
5 | 11 |
|
6 | 12 | /** |
7 | 13 | * A builder class to create commands in autonomous. Currently, it wraps a |
|
10 | 16 | * To build a command, add child commands using {@link #sequential(Command)} |
11 | 17 | * and {@link #parallel(Command)}, then get a command using {@link #build()}. |
12 | 18 | * |
13 | | - * @author James |
| 19 | + * @author James Hagborg |
14 | 20 | * |
15 | 21 | */ |
16 | 22 | public class CommandBuilder { |
@@ -86,6 +92,79 @@ public CommandBuilder parallel(Command command, double timeout) { |
86 | 92 | return this; |
87 | 93 | } |
88 | 94 |
|
| 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 | + |
89 | 168 | /** |
90 | 169 | * Build a command. Note that for simplicity, calling this method |
91 | 170 | * multiple times will return the same {@link Command} object. This |
|
0 commit comments