[commands] Add until() as alias for withInterrupt() (#3981)

This is a clearer description for the functionality.
Will deprecate withInterrupt next year.
This commit is contained in:
Oblarg
2022-02-04 01:14:52 -05:00
committed by GitHub
parent 16bf2c70c5
commit 6a6366b0d6
12 changed files with 50 additions and 14 deletions

View File

@@ -91,10 +91,29 @@ public interface Command {
* @param condition the interrupt condition
* @return the command with the interrupt condition added
*/
default ParallelRaceGroup withInterrupt(BooleanSupplier condition) {
default ParallelRaceGroup until(BooleanSupplier condition) {
return raceWith(new WaitUntilCommand(condition));
}
/**
* Decorates this command with an interrupt condition. If the specified condition becomes true
* before the command finishes normally, the command will be interrupted and un-scheduled. Note
* that this only applies to the command returned by this method; the calling command is not
* itself changed.
*
* <p>Note: This decorator works by composing this command within a CommandGroup. The command
* cannot be used independently after being decorated, or be re-decorated with a different
* decorator, unless it is manually cleared from the list of grouped commands with {@link
* CommandGroupBase#clearGroupedCommand(Command)}. The decorated command can, however, be further
* decorated without issue.
*
* @param condition the interrupt condition
* @return the command with the interrupt condition added
*/
default ParallelRaceGroup withInterrupt(BooleanSupplier condition) {
return until(condition);
}
/**
* Decorates this command with a runnable to run before this command starts.
*

View File

@@ -9,7 +9,7 @@ import edu.wpi.first.wpilibj.Notifier;
/**
* A command that starts a notifier to run the given runnable periodically in a separate thread. Has
* no end condition as-is; either subclass it or use {@link Command#withTimeout(double)} or {@link
* Command#withInterrupt(java.util.function.BooleanSupplier)} to give it one.
* Command#until(java.util.function.BooleanSupplier)} to give it one.
*
* <p>WARNING: Do not use this class unless you are confident in your ability to make the executed
* code thread-safe. If you do not know what "thread-safe" means, that is a good sign that you

View File

@@ -10,8 +10,8 @@ import java.util.function.BooleanSupplier;
/**
* A command that runs a Runnable continuously. Has no end condition as-is; either subclass it or
* use {@link Command#withTimeout(double)} or {@link Command#withInterrupt(BooleanSupplier)} to give
* it one. If you only wish to execute a Runnable once, use {@link InstantCommand}.
* use {@link Command#withTimeout(double)} or {@link Command#until(BooleanSupplier)} to give it one.
* If you only wish to execute a Runnable once, use {@link InstantCommand}.
*
* <p>This class is provided by the NewCommands VendorDep
*/

View File

@@ -10,7 +10,7 @@ import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam;
* A command that runs a given runnable when it is initialized, and another runnable when it ends.
* Useful for running and then stopping a motor, or extending and then retracting a solenoid. Has no
* end condition as-is; either subclass it or use {@link Command#withTimeout(double)} or {@link
* Command#withInterrupt(java.util.function.BooleanSupplier)} to give it one.
* Command#until(java.util.function.BooleanSupplier)} to give it one.
*
* <p>This class is provided by the NewCommands VendorDep
*/

View File

@@ -37,6 +37,13 @@ ParallelRaceGroup Command::WithTimeout(units::second_t duration) && {
return ParallelRaceGroup(std::move(temp));
}
ParallelRaceGroup Command::Until(std::function<bool()> condition) && {
std::vector<std::unique_ptr<Command>> temp;
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
temp.emplace_back(std::move(*this).TransferOwnership());
return ParallelRaceGroup(std::move(temp));
}
ParallelRaceGroup Command::WithInterrupt(std::function<bool()> condition) && {
std::vector<std::unique_ptr<Command>> temp;
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));

View File

@@ -111,6 +111,17 @@ class Command {
*/
virtual ParallelRaceGroup WithTimeout(units::second_t duration) &&;
/**
* Decorates this command with an interrupt condition. If the specified
* condition becomes true before the command finishes normally, the command
* will be interrupted and un-scheduled. Note that this only applies to the
* command returned by this method; the calling command is not itself changed.
*
* @param condition the interrupt condition
* @return the command with the interrupt condition added
*/
virtual ParallelRaceGroup Until(std::function<bool()> condition) &&;
/**
* Decorates this command with an interrupt condition. If the specified
* condition becomes true before the command finishes normally, the command

View File

@@ -18,7 +18,7 @@ namespace frc2 {
/**
* A command that starts a notifier to run the given runnable periodically in a
* separate thread. Has no end condition as-is; either subclass it or use
* Command::WithTimeout(double) or Command::WithInterrupt(BooleanSupplier) to
* Command::WithTimeout(double) or Command::Until(BooleanSupplier) to
* give it one.
*
* <p>WARNING: Do not use this class unless you are confident in your ability to

View File

@@ -16,7 +16,7 @@ namespace frc2 {
/**
* A command that runs a Runnable continuously. Has no end condition as-is;
* either subclass it or use Command.WithTimeout() or
* Command.WithInterrupt() to give it one. If you only wish
* Command.Until() to give it one. If you only wish
* to execute a Runnable once, use InstantCommand.
*
* This class is provided by the NewCommands VendorDep

View File

@@ -17,7 +17,7 @@ namespace frc2 {
* A command that runs a given runnable when it is initialized, and another
* runnable when it ends. Useful for running and then stopping a motor, or
* extending and then retracting a solenoid. Has no end condition as-is; either
* subclass it or use Command.WithTimeout() or Command.WithInterrupt() to give
* subclass it or use Command.WithTimeout() or Command.Until() to give
* it one.
*
* This class is provided by the NewCommands VendorDep

View File

@@ -39,11 +39,11 @@ class CommandDecoratorTest extends CommandTestBase {
}
@Test
void withInterruptTest() {
void untilTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
ConditionHolder condition = new ConditionHolder();
Command command = new WaitCommand(10).withInterrupt(condition::getCondition);
Command command = new WaitCommand(10).until(condition::getCondition);
scheduler.schedule(command);
scheduler.run();

View File

@@ -40,7 +40,7 @@ class CommandGroupErrorTest extends CommandTestBase {
void redecoratedCommandErrorTest() {
Command command = new InstantCommand();
assertDoesNotThrow(() -> command.withTimeout(10).withInterrupt(() -> false));
assertDoesNotThrow(() -> command.withTimeout(10).until(() -> false));
assertThrows(IllegalArgumentException.class, () -> command.withTimeout(10));
CommandGroupBase.clearGroupedCommand(command);
assertDoesNotThrow(() -> command.withTimeout(10));

View File

@@ -34,13 +34,12 @@ TEST_F(CommandDecoratorTest, WithTimeout) {
frc::sim::ResumeTiming();
}
TEST_F(CommandDecoratorTest, WithInterrupt) {
TEST_F(CommandDecoratorTest, Until) {
CommandScheduler scheduler = GetScheduler();
bool finished = false;
auto command =
RunCommand([] {}, {}).WithInterrupt([&finished] { return finished; });
auto command = RunCommand([] {}, {}).Until([&finished] { return finished; });
scheduler.Schedule(&command);