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 is not touched in unit tests, so once-per-loop caches there refresh exactly once.

  • 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
    • 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, which keeps related values on the same records.
      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