I am trying to wrap my head around Ardupilot and LUA scripts in general, so if I am missing something obvious my apologies in advance.
I am seeing unexpected behaviour when trying to schedule a callback by passing parameters to the function in an attempt to avoid using a global variables. When scheduling the callback via return and passing parameter(s) to the callback function the specified time is being ignored resulting it in the callback being called immediately. If I avoid passing parameters to the callback function and use global variables instead the callback is called according to the specified schedule. However, I prefer to avoid the use of global variables whenever possible
for example, return update(1), 1000 does not work as expected; the function is called with the correct parameter but the time is being ignored and the function is called immediately instead of in 1000ms
the following script demonstrates the issue:
-- dummy script to test callback timing... local knMaxIterations = 9 local knFrequency = 1000 function dosomething( nIteration ) gcs:send_text( 6, "dummy::dosomething #" .. tostring(nIteration) ) end function update( nIteration ) gcs:send_text( 6, "dummy::update #" .. tostring(nIteration) ) if not arming:is_armed() then dosomething( nIteration ) end nIteration = nIteration + 1 if nIteration > knMaxIterations then nIteration = 1 end return update(nIteration),knFrequency end return update(1),knFrequency
hopefully this is just an issue of incorrect syntax and not a shortcoming of the implementation.
any assistance will be greatly appreciated