Access serial port in LUA

Hello,

I have managed to connect 3 LIDAR sensors (TF-Luna to be precise) and now want to try and implement some basic object avoidance. I am trying to write a script in LUA that will read from the serial ports however I am having issues with this.

My question is how do I access the data from a serial port in the LUA script?

For context i have my three sensors connected to telem 2, telem 3 and GPS2. They are all working as expected and i am able to get the integrated readings coming up on ardupilot mission planner.

Many thanks in advance

Did you try it with the function
AP_RangeFinder_Backend_ud:distance()
?
See list of AP lua functions

Rolf

1 Like

Before I begin I must apologise but i am a complete beginner.

Currently this is the code I have written:

– define serial port
local serialPort1 = “/dev/ttyS3” – telem2 rangefinder 1
local serialPort2 = “/dev/ttyS6” – gps2 rangefinder 2
local serialPort3 = “/dev/ttyS1” – telem3 rangefinder 3

– define local baud rate
local baudRate1 = 115200
local baudRate2 = 115200
local baudRate3 = 115200

– open serial ports
local serial1 = io.open(serialPort1, “r”)
local serial2 = io.open(serialPort2, “r”)
local serial3 = io.open(serialPort3, “r”)

if not serial1 or not serial2 or not serial3 then
print(“failed to open at least one serial port”)
return
end

My hope is that his would work but honestly I’ve no idea what is going on. At the moment I am just trying to do the most basic of bits and I think that the code you sent may be a little too advanced for me right now.

Just use ArduPilot’s existing obstacle avoidance features:
Object Avoidance — Copter documentation (ardupilot.org)

To be a bit blunt and perhaps pessimistic, if you can’t understand the examples designed to help beginners, you are highly unlikely to arrive at a Lua based obstacle avoidance solution that outshines the existing features. The code above follows exactly zero of the guidelines in the documentation and will not work with ArduPilot at all.

1 Like

Thanks for your response.

Its a bit of a tricky one really because I need to implement a bare bones version of it myself :confused:

I am aware this is a big feat to attempt but from my coding knowledge I am imagining one giant if statement once I get the ports open.

Any help or even direction to the source code for the object avoidance would be greatly appreciated.
Would love to just be able to print the distance within LUA.

The following example achieves your first goal:
ardupilot/libraries/AP_Scripting/examples/rangefinder_test.lua at master · ArduPilot/ardupilot · GitHub

There’s no need to parse the raw serial data. Just use the existing rangefinder bindings.

1 Like

Dear @Yuri_Rage,

We have successfully managed to upload basic code however we cannot find the serial port. We are using the following code:

local instance = 0

local port1 = serial:find_serial(instance)

if not port1 then
gcs:send_text(4, ‘port not found’)
return
end

port1:begin(115200)
port1:set_flow_control(0)

function find_serial()
if port1:available() > 0 then
gcs:send_text(0,‘bytes available:’ … port1:available())
gcs:send_text(0,‘reading:’ … port1:read())
return
end
end

return find_serial()

We have tried several numbers in the place of 0 however it is not working.

Thanks in advance for any response. i think we are nearly there :slight_smile:

You need to set SERIALx_PROTOCOL to 28 (scripting) to access it from within a script. This precludes the use of the rangefinder in normal operation.

But, again, you’re spinning your wheels trying to parse serial data. Use the rangefinder example linked above as your guide.

1 Like

Dear @Yuri_Rage,

Following your advice we have crumbled and decided to not spin the wheel. We are now using the built in function with rangefinder_test.lua this script tells us that the three rangefinders connected are found. The script then gives us the value for the rangefinder that is pointing forwards.

Can you tell us how to access the results from each rangefinder or is it only the one?

Many thanks,
The wheel reinventors

Silly me i have figured it out.

:rofl:

what did you figure out, can you tell me the numbering of serial ports ?