Calculate SITL scheduler loop variables once/loop
Feature request
Remove this clever stuff, calculate these values once/loop
// get the active main loop rate
uint16_t get_loop_rate_hz(void) {
if (_active_loop_rate_hz == 0) {
_active_loop_rate_hz = _loop_rate_hz;
}
return _active_loop_rate_hz;
}
Hi @peterbarker, I'm new to ArduPilot and would like to take up this issue as my first contribution. I've read through the contribution guidelines and explored the codebase.
My take on this issue:
The get_loop_rate_hz() function currently performs a check every time it's called to see if _active_loop_rate_hz is initialized, and if not, sets it to _loop_rate_hz. This results in a redundant check on every call.
My proposal:
We can initialize _active_loop_rate_hz during the initial stage maybe in AP_Scheduler::loop() or in the init() method and then simplify get_loop_rate_hz() into a simple getter.
I'm happy to work on this if no one else is currently assigned.
In AP_Scheduler::loop(), add this code:
if (_active_loop_rate_hz == 0) {
_active_loop_rate_hz = _loop_rate_hz;
}
Then, in get_loop_rate_hz, just return _active_loop_rate_hz;
I did the changes as mentioned @Ryanf55 and build and tested the code according to the docs. Could you please review when you have time? I'm happy to make any changes needed.