Skip to content

Commit

Permalink
[commands] WaitCommand: add Measure<Time> overload (wpilibsuite#6386)
Browse files Browse the repository at this point in the history
Also add waitTime() factory.
  • Loading branch information
spacey-sooty authored and chauser committed May 30, 2024
1 parent 4cd0235 commit 0038caf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,6 +152,23 @@ public ParallelRaceGroup withTimeout(double seconds) {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

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;
Expand Down Expand Up @@ -115,6 +117,17 @@ public static Command waitSeconds(double seconds) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +34,15 @@ public WaitCommand(double seconds) {
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();
Expand Down

0 comments on commit 0038caf

Please sign in to comment.