Hi there,
I have an external sensor for my drone project. I am trying to use a Lua Script to read out the Serial Port of my Pixhawk 4. To boot the sensor the Pixhawk has to send the command @4D001F via UART to the sensor, after that it should periodically read the data from the sensor and send it to the GCS.
I have written a script, but it doesen´t work and honestly I don´t really know what i am doing, this is all new to me.
It would be huge for me, if someone with experience at Lua Scripting could look through my script and give me clues what I have to change.
Currently my script reads:
local port = serial:find_serial(0)
port:begin(115200)
port:set_flow_control(0)
if port:available() > 0 then
gcs:send_text(0, “@4D001F”)
end
function spit ()
if port:available() > 0 then
read = port:read()
gcs:send_text(0, read)
end
gcs:send_text(0, read)
return spit, 1000
end
Be more specific. What autopilot? What sensor? Which serial port?
You aren’t sending the boot string to the serial port. You are sending it to the GCS. More specifically, you probably aren’t even doing that because there will probably not be serial data available to satisfy the if condition surrounding it.
You are sending the “read” string whether or not data was read. Remove the second GCS message.
You are sending the GCS messages at a critical error severity. That’s probably not necessary, either.
It is a magnetometer from FieldLine Inc that I have plugged in the UART&I2CB port on the Pixhawk4 and I am using ArduCopter V4.1.5 OFFICIAL as firmware.
Would you mind editing my code from above in a way you would consider more useful?
Your script will never work as written. The data sheet indicates that a new line must be sent after each command. You also need to turn on channel 23 or 35 to get meaningful output. The default data rate is 250Hz (data every 4ms), but you’re only sampling it once per second. You probably need to slow the data rate to something like 50Hz to allow time for your script to run and then sample the output again.
I’m afraid I probably don’t have time to rewrite your script into a useful format, nor could I promise useful results, since I do not have that hardware at my disposal.
You’re on the right track, but the logic is very flawed right now.
Hi Yuri. I’m trying to write a serial motor driver and have hit the wall on outputting the necessary data strings which are required in hexidecimal format (eg. AB 01 4E 82 with 82 being a checksum value). If port:write only outputs integers, is there a way to output this string using the ASCII byte conversion you suggested above?