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 CommandBase {
017  protected Timer m_timer = new Timer();
018  private final double m_duration;
019
020  /**
021   * Creates a new WaitCommand. This command will do nothing, and end after the specified duration.
022   *
023   * @param seconds the time to wait, in seconds
024   */
025  public WaitCommand(double seconds) {
026    m_duration = seconds;
027    SendableRegistry.setName(this, getName() + ": " + seconds + " seconds");
028  }
029
030  @Override
031  public void initialize() {
032    m_timer.restart();
033  }
034
035  @Override
036  public void end(boolean interrupted) {
037    m_timer.stop();
038  }
039
040  @Override
041  public boolean isFinished() {
042    return m_timer.hasElapsed(m_duration);
043  }
044
045  @Override
046  public boolean runsWhenDisabled() {
047    return true;
048  }
049
050  @Override
051  public void initSendable(SendableBuilder builder) {
052    super.initSendable(builder);
053    builder.addDoubleProperty("duration", () -> m_duration, null);
054  }
055}