001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj2.command;
006
007import edu.wpi.first.util.sendable.SendableBuilder;
008import edu.wpi.first.util.sendable.SendableRegistry;
009import edu.wpi.first.wpilibj.Timer;
010
011/**
012 * A command that does nothing but takes a specified amount of time to finish.
013 *
014 * <p>This class is provided by the NewCommands VendorDep
015 */
016public class WaitCommand extends Command {
017  /** The timer used for waiting. */
018  protected Timer m_timer = new Timer();
019
020  private final double m_duration;
021
022  /**
023   * Creates a new WaitCommand. This command will do nothing, and end after the specified duration.
024   *
025   * @param seconds the time to wait, in seconds
026   */
027  @SuppressWarnings("this-escape")
028  public WaitCommand(double seconds) {
029    m_duration = seconds;
030    SendableRegistry.setName(this, getName() + ": " + seconds + " seconds");
031  }
032
033  @Override
034  public void initialize() {
035    m_timer.restart();
036  }
037
038  @Override
039  public void end(boolean interrupted) {
040    m_timer.stop();
041  }
042
043  @Override
044  public boolean isFinished() {
045    return m_timer.hasElapsed(m_duration);
046  }
047
048  @Override
049  public boolean runsWhenDisabled() {
050    return true;
051  }
052
053  @Override
054  public void initSendable(SendableBuilder builder) {
055    super.initSendable(builder);
056    builder.addDoubleProperty("duration", () -> m_duration, null);
057  }
058}