Files
allwpilib/commandsv2/src/main/java/org/wpilib/command2/WaitCommand.java
2025-11-07 23:09:21 -08:00

71 lines
1.8 KiB
Java

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.command2;
import static org.wpilib.units.Units.Seconds;
import org.wpilib.units.measure.Time;
import org.wpilib.util.sendable.SendableBuilder;
import org.wpilib.util.sendable.SendableRegistry;
import org.wpilib.system.Timer;
/**
* A command that does nothing but takes a specified amount of time to finish.
*
* <p>This class is provided by the NewCommands VendorDep
*/
public class WaitCommand extends Command {
/** The timer used for waiting. */
protected Timer m_timer = new Timer();
private final double m_duration;
/**
* Creates a new WaitCommand. This command will do nothing, and end after the specified duration.
*
* @param seconds the time to wait, in seconds
*/
@SuppressWarnings("this-escape")
public WaitCommand(double seconds) {
m_duration = 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(Time time) {
this(time.in(Seconds));
}
@Override
public void initialize() {
m_timer.restart();
}
@Override
public void end(boolean interrupted) {
m_timer.stop();
}
@Override
public boolean isFinished() {
return m_timer.hasElapsed(m_duration);
}
@Override
public boolean runsWhenDisabled() {
return true;
}
@Override
public void initSendable(SendableBuilder builder) {
super.initSendable(builder);
builder.addDoubleProperty("duration", () -> m_duration, null);
}
}