Lua script kill command

Hi,

I am beginning to experiment with Lua scripts, and I wonder whether there is something like script kill command which could be called from a different script. The scenario I am thinking about is this: I would have a script which would take control of the aircraft, and if something goes wrong, I want a foolproof way of killing/quiting the script.

I know that one can embed something within the main script which would read a channel, and then terminate the script once this channel is switched, but there is always room for error, and to prevent a flyaway or similar, I would want to have a more assertive way of killing the script. Is there one?

No, each script runs in an isolated environment. Mission Planner provides a command to stop execution of all scripts.

Thanks for the answer.

Did not know that there is such a command. Could it be called from a script? I could not find a reference to such a command in documentation, you are not talking about a simple reboot I assume, which would be disastrous for FC in flight.

The ideal idea would be like having two scripts, one main one, which is being developped, and the other one, safety script the sole objective of which would be to stop the execution of all scripts (including the safety script) which could be activated through a switch.

You should be able to use MAVLink API to send the termination command though that won’t be reversible as it will kill all scripts until you either reboot or send scripting restart command through other means such as GCS.

If you want to be able to stop your script you should use pcall (protected call) that will handle errors and stop calling the function if conditions aren’t met.

The MAVLink command only stops scripts. For up to date list of API functions you need to check the source code in libraries/AP_Scripting/docs/docs.lua.

You could code the script to look for a scripting1 high command, and when received bypass the return statement with the loop timer.

The code fragment that follows is not exactly what you’re seeking, as I still have a return with loop timer regardless of high or low (return is omitted from code fragment here). But I think it illustrates the concept well enough.

local scripting1 = rc:get_aux_cached(RC_AUX_FUNC.SCRIPTING1)
-- Ignore RC Medium setting.
if scripting1 == DispenserUtil.RC_AUX_LVL.MEDIUM then
    if isTestingEnabled then
        rc:run_aux_function(RC_AUX_FUNC.SCRIPTING1, DispenserUtil.RC_AUX_LVL.HIGH)
    else
        rc:run_aux_function(RC_AUX_FUNC.SCRIPTING1, DispenserUtil.RC_AUX_LVL.LOW)
    end
else
    isTestingEnabled = (scripting1 == DispenserUtil.RC_AUX_LVL.HIGH)
end

You can configure a button on the GCS or use the Aux Function tab in Mission Planner to send the scripting1 high command.

Thank you very much, I will see how I adopt it…