Class Timer
java.lang.Object
org.wpilib.system.Timer
A timer class.
Note that if the user calls SimHooks.restartTiming(), they should also reset the timer so get() won't return a negative duration.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanadvanceIfElapsed(double seconds) Check if the period specified has passed and if it has, advance the start time by that period.static TimerCreates a new timer that begins started.static voiddelay(double seconds) Pause the execution of the thread for a specified period of time given in seconds.static voidPause the execution of the thread for a specified period of time.doubleget()Get the current time from the timer.static doubleReturn the approximate match time.static doubleReturn the monotonic clock time in seconds.static doubleReturn the clock time in seconds.booleanhasElapsed(double seconds) Check if the period specified has passed.booleanhasElapsed(Time period) Check if the period specified has passed.booleanWhether the timer is currently running.final voidreset()Reset the timer by setting the time to 0.voidrestart()Restart the timer by stopping the timer, if it is not already stopped, resetting the accumulated time, then starting the timer again.voidstart()Start the timer running.voidstop()Stop the timer.
-
Constructor Details
-
Timer
public Timer()Creates a new timer. The timer is initially not running and must be started withstart()to start measuring time. Consider usingcreateStarted()instead if the timer will be used immediately after creation.
-
-
Method Details
-
getTimestamp
public static double getTimestamp()Return the clock time in seconds. By default, the time is the time returned by getMonotonicTimestamp(). However, the return value of this method may be modified to use any time base, including non-monotonic time bases.- Returns:
- Robot running time in seconds.
-
getMonotonicTimestamp
public static double getMonotonicTimestamp()Return the monotonic clock time in seconds.- Returns:
- Monotonic time in seconds.
-
getMatchTime
public static double getMatchTime()Return the approximate match time. The FMS does not send an official match time to the robots, but does send an approximate match time. The value will count down the time remaining in the current period (auto or teleop). Warning: This is not an official time (so it cannot be used to dispute ref calls or guarantee that a function will trigger before the match ends).When connected to the real field, this number only changes in full integer increments, and always counts down.
When the DS is in practice mode, this number is a floating point number, and counts down.
When the DS is in teleop or autonomous mode, this number returns -1.0.
Simulation matches DS behavior without an FMS connected.
- Returns:
- Time remaining in current match period (auto or teleop) in seconds
-
delay
Pause the execution of the thread for a specified period of time. Motors will continue to run at their last assigned values, and sensors will continue to update. Only the task containing the wait will pause until the wait time is expired.- Parameters:
period- Length of time to pause
-
delay
public static void delay(double seconds) Pause the execution of the thread for a specified period of time given in seconds. Motors will continue to run at their last assigned values, and sensors will continue to update. Only the task containing the wait will pause until the wait time is expired.- Parameters:
seconds- Length of time to pause
-
createStarted
Creates a new timer that begins started.This is equivalent to
Timer timer = new Timer(); timer.start();- Returns:
- A new started timer.
-
get
public double get()Get the current time from the timer. If the clock is running it is derived from the current system clock the start time stored in the timer class. If the clock is not running, then return the time when it was last stopped.- Returns:
- Current time value for this timer in seconds
-
reset
public final void reset()Reset the timer by setting the time to 0.Make the timer startTime the current time so new requests will be relative now.
-
start
public void start()Start the timer running. Just set the running flag to true indicating that all time requests should be relative to the system clock. Note that this method is a no-op if the timer is already running. -
restart
public void restart()Restart the timer by stopping the timer, if it is not already stopped, resetting the accumulated time, then starting the timer again. If you want an event to periodically reoccur at some time interval from the start time, consider using advanceIfElapsed() instead. -
stop
public void stop()Stop the timer. This computes the time as of now and clears the running flag, causing all subsequent time requests to be read from the accumulated time rather than looking at the system clock. -
hasElapsed
Check if the period specified has passed.- Parameters:
period- The period to check.- Returns:
- Whether the period has passed.
-
hasElapsed
public boolean hasElapsed(double seconds) Check if the period specified has passed.- Parameters:
seconds- The period to check in seconds.- Returns:
- Whether the period has passed.
-
advanceIfElapsed
public boolean advanceIfElapsed(double seconds) Check if the period specified has passed and if it has, advance the start time by that period. This is useful to decide if it's time to do periodic work without drifting later by the time it took to get around to checking.- Parameters:
seconds- The period to check.- Returns:
- Whether the period has passed.
-
isRunning
public boolean isRunning()Whether the timer is currently running.- Returns:
- true if running.
-