ardupilot icon indicating copy to clipboard operation
ardupilot copied to clipboard

Calculate SITL scheduler loop variables once/loop

Open peterbarker opened this issue 7 months ago • 3 comments

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;
    }

peterbarker avatar Jun 04 '25 07:06 peterbarker

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.

neo-0007 avatar Jun 05 '25 08:06 neo-0007

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;

Ryanf55 avatar Jun 09 '25 14:06 Ryanf55

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.

neo-0007 avatar Jun 10 '25 19:06 neo-0007