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 static edu.wpi.first.units.Units.Seconds; 008 009import edu.wpi.first.units.measure.Time; 010import edu.wpi.first.util.sendable.SendableBuilder; 011import edu.wpi.first.util.sendable.SendableRegistry; 012import edu.wpi.first.wpilibj.Timer; 013 014/** 015 * A command that does nothing but takes a specified amount of time to finish. 016 * 017 * <p>This class is provided by the NewCommands VendorDep 018 */ 019public class WaitCommand extends Command { 020 /** The timer used for waiting. */ 021 protected Timer m_timer = new Timer(); 022 023 private final double m_duration; 024 025 /** 026 * Creates a new WaitCommand. This command will do nothing, and end after the specified duration. 027 * 028 * @param seconds the time to wait, in seconds 029 */ 030 @SuppressWarnings("this-escape") 031 public WaitCommand(double seconds) { 032 m_duration = seconds; 033 SendableRegistry.setName(this, getName() + ": " + seconds + " seconds"); 034 } 035 036 /** 037 * Creates a new WaitCommand. This command will do nothing, and end after the specified duration. 038 * 039 * @param time the time to wait 040 */ 041 public WaitCommand(Time time) { 042 this(time.in(Seconds)); 043 } 044 045 @Override 046 public void initialize() { 047 m_timer.restart(); 048 } 049 050 @Override 051 public void end(boolean interrupted) { 052 m_timer.stop(); 053 } 054 055 @Override 056 public boolean isFinished() { 057 return m_timer.hasElapsed(m_duration); 058 } 059 060 @Override 061 public boolean runsWhenDisabled() { 062 return true; 063 } 064 065 @Override 066 public void initSendable(SendableBuilder builder) { 067 super.initSendable(builder); 068 builder.addDoubleProperty("duration", () -> m_duration, null); 069 } 070}