Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[command based] add Measure<Time> overload of WaitCommand #6386

Merged
merged 14 commits into from
Apr 22, 2024
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 wait(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
Loading