Lua script to execute "on demand"

Hello,

I am trying to set up a Lua script to execute “on demand,” so that I can activate it from a mission, for example, when executing a trigger. Step by step, I have managed to configure the mount to move and take pictures with the script; what I need now is to be able to invoke it when needed from the mission.

I have seen examples where they invoke it from an RC, but that wouldn’t be the case here.

An interesting idea would also be for it to execute upon detecting that the drone stops for a time, say 3 seconds, and then it executes at that moment.

For practical purposes, using a “do set servo” to activate it would be ideal.

I have searched, read, and experimented without success.

Any idea on how to achieve this?

Seems you want to do this while in auto mode. NAV_SCRIPT_TIME is probably the answer:

Thank you very much for the reference, Yuri.

I will review the code in detail.

"Hi @Robotic , I want to activate my Lua script only when I flip a switch on my controller. For example, when I’m near the area where I want to use the script, I can just flip the switch to activate it. Is it possible to set it up like that?

The script must execute in a callback loop, polling for the switch position to change. Include logic to execute your custom functions when the switch is in the position you desire.

That approach could work, but I’m not sure which command to use in a Lua script to check the status of a switch on my controller. I’ve looked through the common Lua scripts for Rover but didn’t find a command for this

It’s the only approach that will work with the current Lua engine.

Here’s one where you’d set RCx_OPTION = 300 and use the cached aux state bindings to switch EKF versions. It’d be better practice to use a global variable to keep track of the switch state and only update values on switch change, but the example gets you the basic idea:

1 Like

Got it, I now understand. I was reading your response on Switch off - on Lua script and select 2nd script to control LEDs - ArduPilot Lua Scripting - ArduPilot Discourse, but this explanation was actually much easier to follow. I’ll test it as soon as I can. I’ve also gone through many of your Lua scripts on the forum, and they’ve been a huge help in building my own. Thanks so much for all your help!"

1 Like

I assume this will block execution of other commands while script is running or timed out? Is DO_SEND_SCRIPT_MESSAGE an alternative?

No, that’s not an alternative. Yes, scripts take CPU time, but it’s fairly transparent to the user, as they are scheduled in small time increments.

Hey @Yuri_Rage,

I have seen your lots of post where you telling people mistakes in the Lua scripts.
Can you please correct my script on this:
I have generated this with the help of chatGPT.

Can you please have a look and How can I test this in simulation? Like I want to turn off the GPS1 and then see whether the failsafe is switching to GPS 2 and coming as RTL or not.

Your check_gps_yaw() function is worthless. ChatGPT is notoriously bad at ArduPilot scripting.

This might work, but it’s kind of rough.

-- GPS Failover and RTL Handling Script

local GPS1 = 0  -- Primary GPS instance
local GPS2 = 1  -- Secondary GPS instance

function update()
    local mode = vehicle:get_mode()
    if mode == 6 then return update, 1000 end -- no need to check status if already RTL

    local gps1_status = gps:status(GPS1)
    local gps2_status = gps:status(GPS2)

    if gps1_status < 3 and gps2_status >= 3 then -- If GPS1 fails and GPS2 is working
        gcs:send_text(0, "GPS1 failed! Switching to GPS2 and initiating RTL.")
        vehicle:set_mode(6)                      -- RTL Mode
    end
    return update, 1000  -- Run every 1 second
end

return update()

To check for a GPS yaw solution, you might check for GPS2 status of less than fix type 6 (RTK Fixed) or the result of ahrs:get_posvelyaw_source_set() greater than zero (I’m less confident about that method).

I have not tested and do not really have time to deep dive on the topic at the moment.

Next time, start a new topic, as this has little to do with the original question.

2 Likes