Skip to content

Commit

Permalink
Add simple wait to coroutines
Browse files Browse the repository at this point in the history
Helpful for timed sequences without needing to wrap async methods in a command
  • Loading branch information
SamCarlberg committed May 19, 2024
1 parent 3ab52bd commit 1321fd4
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.wpi.first.wpilibj.commandsv3;

import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Time;
import java.util.Collection;

/**
Expand Down Expand Up @@ -68,6 +70,27 @@ default void awaitAny(Collection<Command> commands) {
scheduler().awaitAny(commands);
}

/**
* Waits for some duration of time to elapse. Returns immediately if the given duration is zero
* or negative. Call this within a command or command composition to introduce a simple delay.
*
* <p>For example, a basic autonomous routine that drives straight for 5 seconds:
* {@snippet lang = java :
* AsyncCommand timedDrive() {
* return drivebase.run((coroutine) -> {
* drivebase.tankDrive(1, 1);
* coroutine.wait(Seconds.of(5));
* drivebase.stop();
* }).named("Timed Drive");
* }
* }
*
* @param duration the duration of time to wait
*/
default void wait(Measure<Time> duration) {
await(new WaitCommand(duration));
}

/**
* Advanced users only: this permits access to the backing command scheduler to run custom logic
* not provided by the standard coroutine methods.
Expand Down

0 comments on commit 1321fd4

Please sign in to comment.