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. Useful for
013 * CommandGroups. Can also be subclassed to make a command with an internal timer.
014 *
015 * <p>This class is provided by the NewCommands VendorDep
016 */
017public class WaitCommand extends CommandBase {
018  protected Timer m_timer = new Timer();
019  private final double m_duration;
020
021  /**
022   * Creates a new WaitCommand. This command will do nothing, and end after the specified duration.
023   *
024   * @param seconds the time to wait, in seconds
025   */
026  public WaitCommand(double seconds) {
027    m_duration = seconds;
028    SendableRegistry.setName(this, getName() + ": " + seconds + " seconds");
029  }
030
031  @Override
032  public void initialize() {
033    m_timer.reset();
034    m_timer.start();
035  }
036
037  @Override
038  public void end(boolean interrupted) {
039    m_timer.stop();
040  }
041
042  @Override
043  public boolean isFinished() {
044    return m_timer.hasElapsed(m_duration);
045  }
046
047  @Override
048  public boolean runsWhenDisabled() {
049    return true;
050  }
051
052  @Override
053  public void initSendable(SendableBuilder builder) {
054    super.initSendable(builder);
055    builder.addDoubleProperty("duration", () -> m_duration, null);
056  }
057}