Class BackgroundSampler

java.lang.Object
frc.spectrumLib.util.BackgroundSampler

public final class BackgroundSampler extends Object
Runs slow reads, such as files under /proc and CAN bus status, on one low-priority daemon thread. The robot loop only ever picks up the latest result.

On SystemCore the main thread runs at real-time priority. A read that blocks for a millisecond there (CANBus.getStatus() can) or walks sixty /proc files makes the loop late. The results are telemetry and alerts, which are outputs, so where they are computed does not matter to AdvantageKit replay.

Start samplers during robot init: threads inherit the scheduling policy of the thread that creates them, and the main thread becomes real-time at the end of init.

  • Method Details

    • every

      public static <T> BackgroundSampler.Latest<T> every(double periodSeconds, Supplier<T> read)
      Runs read every periodSeconds on the sampler thread.
      Parameters:
      periodSeconds - time between reads
      read - the read; exceptions are swallowed so one bad read cannot stop the thread
      Returns:
      where each result lands