Class RobotLoop

java.lang.Object
frc.spectrumLib.framework.RobotLoop

public final class RobotLoop extends Object
Counts robot loops so work can be done "once per loop" or "every Nth loop" without a timer.

Robot.robotPeriodic() calls next() first thing. Anything that wants to do something once per loop remembers the count it last ran at and compares; anything that wants a slower cadence uses every(int). Both are a long compare, so they are safe to call from tight per-loop paths such as status-signal reads.

Main robot thread only. The count never advances in unit tests, so once-per-loop caches there refresh exactly once.

  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    Returns the current loop number.
    static boolean
    every(int n)
    Returns true on every nth loop, so a 20 ms loop with n = 5 is 10 Hz.
    static boolean
    everySeconds(double seconds)
    Returns true about once every seconds, whatever the loop rate.
    static void
    Advances to the next loop.
    static double
    The robot loop period in seconds (0.02 at 50 Hz, 0.01 at 100 Hz).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • next

      public static void next()
      Advances to the next loop. Called once, at the top of robotPeriodic().
    • count

      public static long count()
      Returns the current loop number. Starts at 0 and increases by one per next().
      Returns:
      the current loop count
    • periodSeconds

      public static double periodSeconds()
      The robot loop period in seconds (0.02 at 50 Hz, 0.01 at 100 Hz).
    • everySeconds

      public static boolean everySeconds(double seconds)
      Returns true about once every seconds, whatever the loop rate. Use it instead of counting loops, so a slow-tier rate such as 10 Hz holds when the loop period changes.
      Parameters:
      seconds - interval between true results
      Returns:
      whether this loop is one to run the periodic work on
    • every

      public static boolean every(int n)
      Returns true on every nth loop, so a 20 ms loop with n = 5 is 10 Hz. All callers in the same loop agree, so related values are logged in the same cycle.
      Parameters:
      n - loops between true results; values below 1 mean every loop
      Returns:
      whether this is a loop on which the periodic work should run