Single Lua script to read from multiple serial devices

I have the Mateksys H743 Slim Flight Controller.

I’ve connected the RCIN receiver to Serial Port 4 and have successfully completed radio calibration.

I have a Pololu servo controller that is currently connected to Serial Port 6. I have been able to successfully send serial commands to the servo controller through this test Lua script:

-- Lua script to send a restart command to a Pololu servo controller

-- Find the serial port (replace 0 with the appropriate port number)
local port = serial:find_serial(0)

-- Configure serial port settings
port:begin(9600)  -- Adjust the baud rate based on your Pololu servo controller's settings
port:set_flow_control(0)

-- Function to send a restart command to a specific subroutine
function restartSubroutine(subroutineNumber)
    local restartCommand = string.char(0xA7, subroutineNumber)
    for i = 1, #restartCommand do
        port:write(restartCommand:byte(i))
    end
    gcs:send_text(0, "Sent restart command to Pololu servo controller subroutine " .. subroutineNumber)
end

-- Specify the subroutine number you want to restart (replace with the actual subroutine number)
local targetSubroutine = 4

-- Execute the restart function with the specified subroutine number
restartSubroutine(targetSubroutine)

-- Function to periodically check for data on the serial port
function checkSerial()
    if port:available() > 0 then
        local read = port:read()
        gcs:send_text(0, "Received data: " .. read)
    end
    return checkSerial, 1000
end

return checkSerial, 1000

I also have a JeVois camera that uses TX/RX communication that will be connected to Serial Port 7.

In Mission Planner, I have currently set Serial Port 6’s Protocol to “Scripting” and am assuming I should do the same thing for Serial Port 7.

However, I’m unable to find any examples online or resources on how to have a single Lua script transmit / receive data from multiple serial ports.

The confusing part in the script above is the
serial:find_serial(0)

The servo controller isn’t on port zero, so I’m not sure why it works (other than, that is the only port with a protocol currently set to “Scripting”. Though, even then, I’m not sure why “0” would target port 6.

Moreover, I’m unsure how I can have a script ALSO read from the RCIN serial port (once I can get it to read from multiple ports).

Any advice on this would be greatly appreciated!

It is 0 because it is the first set to scripting. I guess if you set serial7 to scripting it will be find_serial(1)

Okay, awesome - I’ll try that out. Any thoughts on how to intercept the RCIN port via script?

I don’t think you’re going to be able to read the raw serial RC stream in Lua and also use it for RCIN. As far as I know, those functions are mutually exclusive. Why not just use the RC bindings?

The final version of my drone is going to be doing some unconventional flight behaviour, ideally without any pilot input, other than the initial arming.

So, I was hoping there was a way to have a custom transmitter that sends the “arm” command, and the Lua script and onboard sensors take over from there.

I’m considering potentially using a custom flight mode instead so I don’t have to read the RC input at all with the Lua script. However, this still requires me to be able to “spoof” the transmitter’s “arm” signal.

All of that is possible with no RC transmitter or receiver at all, nor a custom flight mode.

1 Like

Sorry - I think I didn’t explain the situation as clearly.

The pilot arming - I want that to be done remotely, and then the drone take over from there. So, I’m going to need some kind of receiver on board to receive that initial “arm” signal, and then the rest should be autonomous.

But, I don’t want to use a conventional transmitter for the signal - I want it to come from gesture, either using an accelerometer in a peripheral device I’m wearing or possibly a signal from my phone.

All well and good, but you don’t need to emulate an RC device to arm. MavLink and Lua both provide mechanisms to arm independent of an RC signal.

1 Like