[commands] WaitCommand: add Measure<Time> overload (#6386)

Also add waitTime() factory.
This commit is contained in:
Isaac Turner
2024-04-22 13:34:52 +08:00
committed by GitHub
parent e89c8c1008
commit d7dfa63ae9
3 changed files with 46 additions and 0 deletions

View File

@@ -4,8 +4,11 @@
package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.units.Units.Second;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Time;
import edu.wpi.first.util.function.BooleanConsumer;
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableBuilder;
@@ -149,6 +152,23 @@ public abstract class Command implements Sendable {
return raceWith(new WaitCommand(seconds));
}
/**
* Decorates this command with a timeout. If the specified timeout is exceeded before the command
* finishes normally, the command will be interrupted and un-scheduled.
*
* <p>Note: This decorator works by adding this command to a composition. The command the
* decorator was called on cannot be scheduled independently or be added to a different
* composition (namely, decorators), unless it is manually cleared from the list of composed
* commands with {@link CommandScheduler#removeComposedCommand(Command)}. The command composition
* returned from this method can be further decorated without issue.
*
* @param time the timeout duration
* @return the command with the timeout added
*/
public ParallelRaceGroup withTimeout(Measure<Time> time) {
return withTimeout(time.in(Second));
}
/**
* 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.

View File

@@ -6,6 +6,8 @@ package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Time;
import java.util.Map;
import java.util.Set;
import java.util.function.BooleanSupplier;
@@ -115,6 +117,17 @@ public final class Commands {
return new WaitCommand(seconds);
}
/**
* Constructs a command that does nothing, finishing after a specified duration.
*
* @param time after how long the command finishes
* @return the command
* @see WaitCommand
*/
public static Command waitTime(Measure<Time> time) {
return new WaitCommand(time);
}
/**
* Constructs a command that does nothing, finishing once a condition becomes true.
*

View File

@@ -4,6 +4,10 @@
package edu.wpi.first.wpilibj2.command;
import static edu.wpi.first.units.Units.Second;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Time;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.util.sendable.SendableRegistry;
import edu.wpi.first.wpilibj.Timer;
@@ -30,6 +34,15 @@ public class WaitCommand extends Command {
SendableRegistry.setName(this, getName() + ": " + seconds + " seconds");
}
/**
* Creates a new WaitCommand. This command will do nothing, and end after the specified duration.
*
* @param time the time to wait
*/
public WaitCommand(Measure<Time> time) {
this(time.in(Second));
}
@Override
public void initialize() {
m_timer.restart();