Timer for Lua script - Rover

Hi all,

Can someone please suggest a way to create a timer (1s-10s) using Lua scripting?

In my application, during a mission for my rover, I would like the rover to stay in position (even in Auto mode) until a button on the rover is pressed. Once pressed, the rover should move along the path for a pre-defined time (set by a dial on the RC controller). Once the timer expires, the rover should stop and wait for the button press again. This continues until the mission is complete.

Any suggestions are greatly appreciated.

local TIMEOUT_MS = 5000
local RUN_INTERVAL_MS = 200

local timeout = uint32_t(0)

function update()
    local now = millis()
    if now - timeout > TIMEOUT_MS then
        gcs:send_text(6, 'Timeout expired')
        timeout = now
    end
    return update, RUN_INTERVAL_MS
end

return update()

Many Thanks @Yuri_Rage, your support is greatly appreciated.

Hi @Yuri_Rage,
I am currently working on a method to safely pause a mission for my rover in a Lua script (once a timer expires following a button press).
My rover is a small farming vehicle, so when i pause a mission, i want to be certain that is has stopped safely until the operator presses the button on the back of the vehicle to enable the mission to continue.
Would you have any suggestion on how to achieve a safe mission pause?

I was thinking that I could decelerate until a stop, and then switch to a HOLD control mode, or just switch to Manual mode and then restart the mission from the current position.

Thanks,
Zoran

You could insert a mission pause waypoint, indefinite loiter point, or switch to hold mode.

Have a look at this one:

ardupilot/libraries/AP_Scripting/examples/rover-MinFixType.lua at master · ArduPilot/ardupilot (github.com)

Thanks @Yuri_Rage,
That’s a great example. I also just came across your live stream YouTube video, so learning a lot from that also. In that video, you mentioned setting up a mapping while driving the rover around. Is that an example you may have shared also?

Amazing! Thank you @Yuri_Rage