HAL_Linux: reduce delay(ms) jitter

Fix delay(1) rarely returning immediately.
On my RPi4, this once per 5-20k calls that worked.

Reduce the last call to microsleep according to the
remaining time needed in the last loop iteration.
This commit is contained in:
Richard Allen 2024-02-11 12:15:50 -06:00 committed by Andrew Tridgell
parent 0007c7dce0
commit bf1d364201
1 changed files with 9 additions and 4 deletions

View File

@ -173,15 +173,20 @@ void Scheduler::delay(uint16_t ms)
return;
}
uint64_t start = AP_HAL::millis64();
if (ms == 0) {
return;
}
while ((AP_HAL::millis64() - start) < ms) {
uint64_t now = AP_HAL::micros64();
uint64_t end = now + 1000UL * ms + 1U;
do {
// this yields the CPU to other apps
microsleep(1000);
microsleep(MIN(1000UL, end-now));
if (in_main_thread() && _min_delay_cb_ms <= ms) {
call_delay_cb();
}
}
now = AP_HAL::micros64();
} while (now < end);
}
void Scheduler::delay_microseconds(uint16_t us)