2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2019-08-25 17:47:07 -04:00
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj2.command;
|
|
|
|
|
|
2024-09-07 13:59:29 -04:00
|
|
|
import static edu.wpi.first.units.Units.Seconds;
|
2024-04-22 13:34:52 +08:00
|
|
|
|
2024-09-07 13:59:29 -04:00
|
|
|
import edu.wpi.first.units.measure.Time;
|
2022-12-16 04:28:52 +02:00
|
|
|
import edu.wpi.first.util.sendable.SendableBuilder;
|
2021-06-13 16:38:05 -07:00
|
|
|
import edu.wpi.first.util.sendable.SendableRegistry;
|
2019-08-25 17:47:07 -04:00
|
|
|
import edu.wpi.first.wpilibj.Timer;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-20 22:46:26 +02:00
|
|
|
* A command that does nothing but takes a specified amount of time to finish.
|
2022-01-08 11:11:34 -08:00
|
|
|
*
|
|
|
|
|
* <p>This class is provided by the NewCommands VendorDep
|
2019-08-25 17:47:07 -04:00
|
|
|
*/
|
2023-07-14 01:12:01 -04:00
|
|
|
public class WaitCommand extends Command {
|
2024-01-04 08:38:06 -08:00
|
|
|
/** The timer used for waiting. */
|
2019-08-25 17:47:07 -04:00
|
|
|
protected Timer m_timer = new Timer();
|
2024-01-04 08:38:06 -08:00
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
private final double m_duration;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Creates a new WaitCommand. This command will do nothing, and end after the specified duration.
|
2019-08-25 17:47:07 -04:00
|
|
|
*
|
|
|
|
|
* @param seconds the time to wait, in seconds
|
|
|
|
|
*/
|
2023-12-09 21:45:02 -08:00
|
|
|
@SuppressWarnings("this-escape")
|
2019-08-25 17:47:07 -04:00
|
|
|
public WaitCommand(double seconds) {
|
|
|
|
|
m_duration = seconds;
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry.setName(this, getName() + ": " + seconds + " seconds");
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-22 13:34:52 +08:00
|
|
|
/**
|
|
|
|
|
* Creates a new WaitCommand. This command will do nothing, and end after the specified duration.
|
|
|
|
|
*
|
|
|
|
|
* @param time the time to wait
|
|
|
|
|
*/
|
2024-09-07 13:59:29 -04:00
|
|
|
public WaitCommand(Time time) {
|
|
|
|
|
this(time.in(Seconds));
|
2024-04-22 13:34:52 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
2023-01-29 10:21:07 -05:00
|
|
|
m_timer.restart();
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void end(boolean interrupted) {
|
|
|
|
|
m_timer.stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
2020-02-08 13:23:29 -05:00
|
|
|
return m_timer.hasElapsed(m_duration);
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean runsWhenDisabled() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-12-16 04:28:52 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initSendable(SendableBuilder builder) {
|
|
|
|
|
super.initSendable(builder);
|
|
|
|
|
builder.addDoubleProperty("duration", () -> m_duration, null);
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|