Class RobotLoop
java.lang.Object
frc.spectrumLib.framework.RobotLoop
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 TypeMethodDescriptionstatic longcount()Returns the current loop number.static booleanevery(int n) Returnstrueon everynth loop, so a 20 ms loop withn = 5is 10 Hz.static booleaneverySeconds(double seconds) Returnstrueabout once everyseconds, whatever the loop rate.static voidnext()Advances to the next loop.static doubleThe robot loop period in seconds (0.02 at 50 Hz, 0.01 at 100 Hz).
-
Method Details
-
next
public static void next()Advances to the next loop. Called once, at the top ofrobotPeriodic(). -
count
public static long count()Returns the current loop number. Starts at 0 and increases by one pernext().- 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) Returnstrueabout once everyseconds, 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 betweentrueresults- Returns:
- whether this loop is one to run the periodic work on
-
every
public static boolean every(int n) Returnstrueon everynth loop, so a 20 ms loop withn = 5is 10 Hz. All callers in the same loop agree, so related values are logged in the same cycle.- Parameters:
n- loops betweentrueresults; values below 1 mean every loop- Returns:
- whether this is a loop on which the periodic work should run
-