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