gpio-motors: deliver sub-tick delays on HZ=100 kernels - #2247
Conversation
The delay argument has never actually worked below 10ms. These cameras run HZ=100 kernels without high-resolution timers, so every usleep() rounds up to a 10ms tick: usleep(1500) waits ~10ms, and 8 micro-steps x 10ms puts a hard ~80ms floor under every step regardless of the requested delay. Measured on a Hi3518EV200 (28BYJ-48 steppers): 200 steps took 33s at delay 15 and still 18s at delay 4 - the delay barely mattered, because the tick rounding dominated. With a CLOCK_MONOTONIC spin for delays below one tick the same 200 steps complete in ~4s at 1.5ms per micro-step, and the delay argument finally means what it says. Delays of 10ms and up still use usleep, so slow moves do not spin. Busy-waiting below that is a deliberate trade: moves are short and bounded, and a stepper mid-move needs the CPU for milliseconds, not ticks.
PR Summary by Qodogpio-motors: add CLOCK_MONOTONIC busy-wait for sub-tick delays on HZ=100
AI Description
Diagram
High-Level Assessment
Files changed (1)
|
Code Review by Qodo
1.
|
Review follow-up: - compute the elapsed time in long long: on 32-bit targets a long overflows after ~2.1s, which a preemption in the middle of the spin can reach, and signed overflow is undefined behavior - reject a negative delay at the CLI and treat non-positive delays as zero in delay_us, instead of spinning unthrottled - fall back to usleep if clock_gettime fails, so the wait stays bounded
The delay argument has never actually worked below 10 ms. These cameras run HZ=100 kernels without high-resolution timers, so every
usleep()rounds up to a 10 ms tick:usleep(1500)waits ~10 ms, and 8 micro-steps × 10 ms puts a hard ~80 ms floor under every step regardless of the requested delay.Measured on a Hi3518EV200 with 28BYJ-48-style steppers:
usleepusleepCLOCK_MONOTONICspinThe requested delay barely mattered before because tick rounding dominated everything. With a
CLOCK_MONOTONICspin for delays below one tick, the delay argument finally means what it says.Delays of 10 ms and up still go through
usleep, so slow moves do not spin. Busy-waiting below that is a deliberate trade: moves are short and bounded, and a stepper mid-move needs the CPU for milliseconds, not ticks.If there is interest, a follow-up can add constant-acceleration ramping (ease in/out over the first and last steps) — at the speeds this fix unlocks, a stepper driven at a flat rate from standstill lurches and can slip steps.