Package trclib.timer

Class TrcTimer

java.lang.Object
trclib.timer.TrcTimer

public class TrcTimer extends Object
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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final TrcDbgTrace
     
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    TrcTimer(String instanceName)
    Constructor: Creates an instance of the timer with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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
    This method returns the current time stamp with the specified format.
    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
    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.
    void
    set(double time, TrcEvent event)
    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.
    static void
    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.
    This method returns the instance name and the timer state.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • TrcTimer

      public TrcTimer(String instanceName)
      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

      public String toString()
      This method returns the instance name and the timer state.
      Overrides:
      toString in class Object
      Returns:
      instance name and its state.
    • set

      public void set(double time, TrcEvent event, TrcEvent.Callback callback, Object callbackContext)
      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

      public void set(double time, TrcEvent event)
      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

      public void set(double time, TrcEvent.Callback callback, Object callbackContext)
      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

      public void set(double time, TrcEvent.Callback callback)
      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

      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.
    • 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.
    • 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

      public static String getCurrentTimeString(String format)
      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

      public static String 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.
    • shutdown

      public static void shutdown()
      This method is called by the TrcTaskMgr to shut down the timer thread when it is exiting.