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.
– 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
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, 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’)
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.
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.