Package trclib.timer
Class TrcTimer
java.lang.Object
trclib.timer.TrcTimer
This class implements a timer that will signal an event or make a notification callback when the time has expired.
This is useful for doing delays in autonomous, for example.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
This class encapsulates the state of a timer that must be updated atomically. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String
private static final TrcHighPrecisionTime
private static final String
private static TrcTimer
private static TrcTimer
private static boolean
private final TrcTimer.State
static final TrcDbgTrace
private static Thread
final TrcDbgTrace
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
This method adds the timer to the timer list in the order of expiration.void
cancel()
This method cancels the timer if it's set but has not expired.private void
cancel
(boolean doNotify) This method cancels the timer if it's set but has not expired.static double
Returns the current time in seconds.static long
This method returns the current epoch time in msec.static String
This method returns the current time stamp with the default format.static String
getCurrentTimeString
(String format) This method returns the current time stamp with the specified format.private long
This method returns the expired timestamp in milliseconds.static double
This method returns the competition mode elapsed time by subtracting mode start time from the current time.static double
getModeElapsedTime
(double epochTime) This method returns the elapsed time in seconds of the specified epoch time from the competition mode start epoch time.static long
This method returns the nano second timestamp since a fixed arbitrary time referenced by the Java VM.boolean
This method checks if the timer has expired.boolean
isActive()
This method checks if the timer is active (i.e.boolean
This method checks if the timer was canceled.static void
This method is called at the start of a competition mode to set the mode start timestamp so that getModeElapsedTime can calculate the mode elapsed time.private static void
removeTimer
(TrcTimer timer) This method removes a timer from the list.void
This methods sets the expire time relative to the current time.void
set
(double time, TrcEvent.Callback callback) This methods sets the expire time relative to the current time.void
set
(double time, TrcEvent.Callback callback, Object callbackContext) This methods sets the expire time relative to the current time.void
set
(double time, TrcEvent event, TrcEvent.Callback callback, Object callbackContext) This methods sets the expire time relative to the current time.private void
This method is called when the timer has expired.static void
shutdown()
This method is called by the TrcTaskMgr to shut down the timer thread when it is exiting.static void
sleep
(long milliTime) This method puts the current thread to sleep for the given time in msec.private static void
This method runs by the timer thread to wait for the next timer in the list and signal the timer object when it expires.toString()
This method returns the instance name and the timer state.
-
Field Details
-
moduleName
-
staticTracer
-
state
-
tracer
-
instanceName
-
modeStartTime
-
timerList
-
timerThread
-
shuttingDown
private static volatile boolean shuttingDown -
nextTimerToExpire
-
preemptingTimer
-
-
Constructor Details
-
TrcTimer
Constructor: Creates an instance of the timer with the given name.- Parameters:
instanceName
- specifies the name to identify this instance of the timer.
-
-
Method Details
-
toString
This method returns the instance name and the timer state. -
set
This methods sets the expire time relative to the current time. When the time expires, it will signal the event if provided. It will also do the notification callback if provided.- Parameters:
time
- specifies the expire time in seconds relative to the current time.event
- specifies the event to signal when time has expired.callback
- specifies the callback to call when time has expired.callbackContext
- specifies the object to pass back for the callback, can be null if not provided.- Throws:
IllegalArgumentException
- if both event and callback are null.
-
set
This methods sets the expire time relative to the current time. When the time expires, it will signal the given event.- Parameters:
time
- specifies the expire time in seconds relative to the current time.event
- specifies the event to signal when time has expired.
-
set
This methods sets the expire time relative to the current time. When the time expires, it will do the notificaton callback.- Parameters:
time
- specifies the expire time in seconds relative to the current time.callback
- specifies the callback when time has expired.callbackContext
- specifies the object to pass back for the callback, can be null if not provided.
-
set
This methods sets the expire time relative to the current time. When the time expires, it will do the notificaton callback.- Parameters:
time
- specifies the expire time in seconds relative to the current time.callback
- specifies the callback when time has expired.
-
cancel
private void cancel(boolean doNotify) This method cancels the timer if it's set but has not expired. If the timer is canceled, the event is signaled.- Parameters:
doNotify
- specifies true to notify the timer owner of the cancellation, false if called internally to to re-arm the previous non-expired timer.
-
cancel
public void cancel()This method cancels the timer if it's set but has not expired. If the timer is canceled, the event is signaled. -
getExpiredTimeInMsec
private long getExpiredTimeInMsec()This method returns the expired timestamp in milliseconds. If the timer is not set, it returns 0.- Returns:
- expired timestamp in msec.
-
hasExpired
public boolean hasExpired()This method checks if the timer has expired.- Returns:
- true if the timer has expired, false otherwise.
-
isCanceled
public boolean isCanceled()This method checks if the timer was canceled.- Returns:
- true if the timer was canceled, false otherwise.
-
isActive
public boolean isActive()This method checks if the timer is active (i.e. running, not expired and not canceled).- Returns:
- true if the timer is active, false otherwise.
-
setExpired
private void setExpired()This method is called when the timer has expired. -
recordModeStartTime
public static void recordModeStartTime()This method is called at the start of a competition mode to set the mode start timestamp so that getModeElapsedTime can calculate the mode elapsed time. -
getModeElapsedTime
public static double getModeElapsedTime()This method returns the competition mode elapsed time by subtracting mode start time from the current time. If this method is called before the competition mode is started, the system elapsed time is returned instead.- Returns:
- mode elapsed time in seconds.
-
getModeElapsedTime
public static double getModeElapsedTime(double epochTime) This method returns the elapsed time in seconds of the specified epoch time from the competition mode start epoch time.- Parameters:
epochTime
- specifies the epoch time to compute the elapsed time from the mode start time.- Returns:
- elapsed time in seconds from last mode start time.
-
getCurrentTime
public static double getCurrentTime()Returns the current time in seconds. Note that while the unit of time of the return value is in seconds, the precision is in nanoseconds but not necessarily nanosecond resolution (that is, how frequently the value changes). There is no guarantee except that the resolution is at least as good as that of System.currentTimeMillis().- Returns:
- the time difference in seconds, between the current time and midnight, January 1, 1970 UTC with nanosecond precision.
-
getNanoTime
public static long getNanoTime()This method returns the nano second timestamp since a fixed arbitrary time referenced by the Java VM. Note: this is not epoch time so it is not interchangeable with the time returned from getCurrentTime().- Returns:
- current time in nano second.
-
getCurrentTimeMillis
public static long getCurrentTimeMillis()This method returns the current epoch time in msec.- Returns:
- current time in msec.
-
getCurrentTimeString
This method returns the current time stamp with the specified format.- Parameters:
format
- specifies the time stamp format.- Returns:
- current time stamp string with the specified format.
-
getCurrentTimeString
This method returns the current time stamp with the default format.- Returns:
- current time stamp string with the default format.
-
sleep
public static void sleep(long milliTime) This method puts the current thread to sleep for the given time in msec. It handles InterruptException where it recalculates the remaining time and calls sleep again repeatedly until the specified sleep time has past.- Parameters:
milliTime
- specifies sleep time in msec.
-
addTimer
This method adds the timer to the timer list in the order of expiration.- Parameters:
timer
- specifies the timer to be added to the list.
-
removeTimer
This method removes a timer from the list.- Parameters:
timer
- specifies the timer to be removed.
-
shutdown
public static void shutdown()This method is called by the TrcTaskMgr to shut down the timer thread when it is exiting. -
timerTask
private static void timerTask()This method runs by the timer thread to wait for the next timer in the list and signal the timer object when it expires.
-