Lua scripts not working

Hello everyone,
I can’t get the LUA scripts to start on my pixhawk 6X,
it keeps returning the same error: Lua: /APM/scripts/main.lua returned bad result count (1)
I have tried several things with the help of ChatGPT but I can’t find my solution.
I’ve tried several simple scripts, some more complicated, and even direct examples of arducopter and it still doesn’t work.

Pixhawk 6X
arducopter 4.6 (and latest stable)

Thanks all

Provide a copy of one of the failed scripts.

my main lua for exemple:


– Adresse I²C par défaut du TCA9548A
local TCA9548A_ADDR = 0x70 – Adresse fixe pour le TCA9548A
local I2C_BUS = 1 – Numéro du bus I²C du Pixhawk (généralement 1)
local VL53L1X_ADDR = 0x29 – Adresse par défaut des capteurs VL53L1X

– Canaux utilisés sur le TCA9548A
local CHANNELS = {0, 1, 2} – Canaux pour les 3 capteurs VL53L1X

– Fonction pour sélectionner un canal sur le TCA9548A
function select_channel(channel)
local data = {1 << channel} – Commande pour activer le canal
local result = i2c.write(I2C_BUS, TCA9548A_ADDR, data)
if not result then
gcs:send_text(0, "Erreur : Impossible de sélectionner le canal " … channel)
return false
end
return true
end

– Fonction pour lire la distance d’un VL53L1X
function read_distance()
– Commande pour demander les deux octets de distance
local result, data = i2c.read(I2C_BUS, VL53L1X_ADDR, 2)
if result and #data == 2 then
– Convertir les deux octets en une distance (en millimètres)
local distance = (data:byte(1) << 8) | data:byte(2)
return distance
else
gcs:send_text(0, “Erreur de lecture sur le capteur”)
return -1
end
end

– Fonction principale appelée en boucle
function update()
for i, channel in ipairs(CHANNELS) do
– Sélectionner le canal correspondant au capteur
if select_channel(channel) then
– Pause pour stabiliser la communication après le changement de canal
coroutine.yield(50)

        -- Lire la distance sur le canal actif
        local distance = read_distance()

        -- Afficher les données sur la console GCS
        if distance ~= -1 then
            gcs:send_text(0, "Capteur " .. i .. " : " .. distance .. " mm")
        else
            gcs:send_text(0, "Capteur " .. i .. " : Erreur de lecture")
        end
    else
        gcs:send_text(0, "Erreur : Impossible d'utiliser le canal " .. channel)
    end
end

-- Attendre avant la prochaine itération
return update, 200

end

return update

But y trying too:

function update()
return update, 1000
end
return update

The formatting of your first snippet is atrocious, so I’m not even going to try and sort that out.

The second contains a syntax error. If you want to immediately execute the function, the last line must be return update()

Try this:

function update()
    gcs:send_text(6, 'Hello world')
    return update, 1000
end

return update()

Thank you,
I’ll try hello world right away.
In fact I’m trying to put several VL53L1X on the I2C port through a TCA9548A, and I don’t know much about code, so I asked ChatGPT to make me a code but as you say it’s not very pretty.

Ok, hello world works, so the problem comes from the codes I used.

It’s the lack of () in return update that is most likely causing the problem. Follow my example that uses return update()

And ChatGPT is fairly poor at ArduPilot scripting.

2 Likes

OK, The script is half working, now it gives me this error:
: pt to ca
PreArm: Scripting: /APM/scripts/main.lua:11: attempt to call il value (field ‘write’)

You haven’t explained anything about what you’re trying to do. The error is wherever “write” is called.

Recommend you reference the examples rather than attempting to get ChatGPT to write code.

In fact I’m trying to put several VL53L1X on the I2C port through a TCA9548A, and I don’t know much about code, unless you have an easier way to add multiple LiDAR sensors to a drone.

Thanks a lot for your help.

See here:

I think you need to understand the fundamental first before you can get things you wanted to do, works. In embedded solution, I2C module manufacturers usually provide a dip SW or proprietary software to configure the I2C address, so users can connect them together and change the address according to tally to the embedded driver or software.

Similar to the @Yuri_Rage reference, the discussion.

I have seen many lidars connected to Ardupilot via serial, haven’t come across through I2C. Multiple I2C class devices on the same I2C port, yes, like CAN-ESC and CAN GPS. Assuming you can change the VL53L1X address, have you wonder will Ardupilot software or driver support it? All * RNGFNDx_TYPE = 16 (VL53L0X)? Maybe can, change the RNGFNDx_ADDR.

If your drone have multiple I2C ports or your sensor support serial (Uart), then no code is require, just setup or configuration.