LoopTimer
#LoopTimer
A LoopTimer measures how long each loop cycle takes. It reports the last cycle time, the loop frequency and a smoothed average, which helps you see whether the OpMode loop is slow.
#Creating a LoopTimer
LoopTimer timer = new LoopTimer();
LoopTimer smooth = new;The optional parameter is the smoothing factor of the average. The default is 0.1.
The average is an exponential moving average. Each cycle moves the average toward the latest cycle time by the smoothing factor:
avg = avg + smoothing x (dt - avg)A value of 1.0 makes the average equal to the latest cycle time. A smaller value gives a steadier average that reacts more slowly. The value must be above 0.0 and at most 1.0, otherwise the constructor throws an IllegalArgumentException.
#Measuring the Loop
Call tick() exactly once per loop, at the same point in every cycle:
while (opModeIsActive()) {
timer.tick();
telemetry.addData("Loop time (ms)", timer.getDt() * 1000.0);
telemetry.addData("Loop rate (Hz)", timer.getHz());
telemetry.addData("Average (ms)", timer.getAverageMillis());
telemetry.update();
}| Method | Returns |
|---|---|
tick() | Records the current time and updates the measurements. |
getDt() | Time of the last cycle in seconds. Not smoothed. |
getHz() | Loop frequency in hertz, calculated from the smoothed average. Returns 0.0 before it has data. |
getAverageMillis() | Smoothed cycle time in milliseconds. |
The first tick() only records the start time, so all values are 0.0 until the second tick(). There is no reset method.
Última actualización 2026-11-16