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 SummaryNested ClassesModifier and TypeClassDescriptionprivate static classThis class encapsulates the state of a timer that must be updated atomically.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprivate final Stringprivate static final TrcHighPrecisionTimeprivate static final Stringprivate static TrcTimerprivate static TrcTimerprivate static booleanprivate final TrcTimer.Statestatic final TrcDbgTraceprivate static Threadfinal TrcDbgTrace
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprivate static voidThis method adds the timer to the timer list in the order of expiration.voidcancel()This method cancels the timer if it's set but has not expired.private voidcancel(boolean doNotify) This method cancels the timer if it's set but has not expired.static doubleReturns the current time in seconds.static longThis method returns the current epoch time in msec.static StringThis method returns the current time stamp with the default format.static StringgetCurrentTimeString(String format) This method returns the current time stamp with the specified format.private longThis method returns the expired timestamp in milliseconds.static doubleThis method returns the competition mode elapsed time by subtracting mode start time from the current time.static doublegetModeElapsedTime(double epochTime) This method returns the elapsed time in seconds of the specified epoch time from the competition mode start epoch time.static longThis method returns the nano second timestamp since a fixed arbitrary time referenced by the Java VM.booleanThis method checks if the timer has expired.booleanisActive()This method checks if the timer is active (i.e.booleanThis method checks if the timer was canceled.static voidThis 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 voidremoveTimer(TrcTimer timer) This method removes a timer from the list.voidThis methods sets the expire time relative to the current time.voidset(double time, TrcEvent.Callback callback) This methods sets the expire time relative to the current time.voidset(double time, TrcEvent.Callback callback, Object callbackContext) This methods sets the expire time relative to the current time.voidset(double time, TrcEvent event, TrcEvent.Callback callback, Object callbackContext) This methods sets the expire time relative to the current time.private voidThis method is called when the timer has expired.static voidshutdown()This method is called by the TrcTaskMgr to shut down the timer thread when it is exiting.static voidsleep(long milliTime) This method puts the current thread to sleep for the given time in msec.private static voidThis 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
- 
shuttingDownprivate static volatile boolean shuttingDown
- 
nextTimerToExpire
- 
preemptingTimer
 
- 
- 
Constructor Details- 
TrcTimerConstructor: Creates an instance of the timer with the given name.- Parameters:
- instanceName- specifies the name to identify this instance of the timer.
 
 
- 
- 
Method Details- 
toStringThis method returns the instance name and the timer state.
- 
setThis 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.
 
- 
setThis 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.
 
- 
setThis 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.
 
- 
setThis 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.
 
- 
cancelprivate 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.
 
- 
cancelpublic void cancel()This method cancels the timer if it's set but has not expired. If the timer is canceled, the event is signaled.
- 
getExpiredTimeInMsecprivate long getExpiredTimeInMsec()This method returns the expired timestamp in milliseconds. If the timer is not set, it returns 0.- Returns:
- expired timestamp in msec.
 
- 
hasExpiredpublic boolean hasExpired()This method checks if the timer has expired.- Returns:
- true if the timer has expired, false otherwise.
 
- 
isCanceledpublic boolean isCanceled()This method checks if the timer was canceled.- Returns:
- true if the timer was canceled, false otherwise.
 
- 
isActivepublic 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.
 
- 
setExpiredprivate void setExpired()This method is called when the timer has expired.
- 
recordModeStartTimepublic 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.
- 
getModeElapsedTimepublic 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.
 
- 
getModeElapsedTimepublic 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.
 
- 
getCurrentTimepublic 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.
 
- 
getNanoTimepublic 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.
 
- 
getCurrentTimeMillispublic static long getCurrentTimeMillis()This method returns the current epoch time in msec.- Returns:
- current time in msec.
 
- 
getCurrentTimeStringThis 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.
 
- 
getCurrentTimeStringThis method returns the current time stamp with the default format.- Returns:
- current time stamp string with the default format.
 
- 
sleeppublic 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.
 
- 
addTimerThis method adds the timer to the timer list in the order of expiration.- Parameters:
- timer- specifies the timer to be added to the list.
 
- 
removeTimerThis method removes a timer from the list.- Parameters:
- timer- specifies the timer to be removed.
 
- 
shutdownpublic static void shutdown()This method is called by the TrcTaskMgr to shut down the timer thread when it is exiting.
- 
timerTaskprivate 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.
 
-