Mavlink:send_chan function doesn't do anything

Hello everyone,

I am trying to send a MAVLink message from my LUA script. I am using SITL in Mission Planner for testing, but I tried to do the same from the CubeOrangePlus board and it did not work. I enabled MAVFTP, placed the scripts and modules in the correct paths and ran multiple successful tests with other functionalities, but when I send the MAVLink message to change the mode, it doesn’t do anything.

Here is my minimal example:

local MavlinkMessages = require("MAVLink/mavlink_msgs")

local COMMAND_LONG_ID = MavlinkMessages.get_msgid("COMMAND_LONG")

-- Initialize MAVLink to receive messages
mavlink:init(1, 10)

mavlink:register_rx_msgid(COMMAND_LONG_ID)

-- Define message IDs for the MAVLink messages we are interested in
local MAV_CMD_DO_SET_MODE = 176
local MAV_CMD_DO_CHANGE_SPEED = 178
local UAVIONIX_ADSB_OUT_CONTROL = 10007

local function handleCommandLong(parsedMessage)
    local commandLong = {}
    commandLong.param1 = 1 -- Mode value
    commandLong.param2 = 0 -- Custom mode (not used here)
    commandLong.param3 = 0 -- Custom submode (not used here)
    commandLong.param4 = 0 -- Not used
    commandLong.param5 = 0 -- Not used
    commandLong.param6 = 0 -- Not used
    commandLong.param7 = 0 -- Not used
    commandLong.command = MAV_CMD_DO_SET_MODE
    commandLong.target_system = 1 -- Target system ID (usually 1 for single system)
    commandLong.target_component = 1 -- Target component ID (usually 1 for autopilot)
    commandLong.confirmation = 0

    -- Send the command on the default channel (channel 1)
    local messageId, message = MavlinkMessages.encode("COMMAND_LONG", commandLong)
    mavlink:send_chan(0, messageId, message)
    gcs:send_text(0, "Send command long")
end

-- Function to handle incoming MAVLink messages
local function catchMAVLinkMessage()
    local message, chan = mavlink:receive_chan()
    if message ~= nil then
        local msg_map = {}
        msg_map[COMMAND_LONG_ID] = "COMMAND_LONG"
        local parsedMessage = MavlinkMessages.decode(message, msg_map)

        if parsedMessage ~= nil then
            gcs:send_text(0, "Message ID: " .. parsedMessage.msgid)

            if parsedMessage.msgid == COMMAND_LONG_ID then
                handleCommandLong(parsedMessage)
            else
                gcs:send_text(0, "Message Id is " .. parsedMessage.msgid)
            end
        end
    end
end

-- Function to update and poll data
local function update()
    gcs:send_text(0, "Updating")

    catchMAVLinkMessage()

    return update, 1000
end

return update()
// End of script

Whenever it detects a COMMAND_LONG, such as TRIGGER_CAMERA (for testing purposes), it tries to set the system mode to manual. It doesn’t change the mode though. I am starting to think that it is not possible to send MAVLink messages from LUA. I’ve set param to 1 and 0 because Mission Planner sends 1 for param 1, but it is still not doing anything.

Any critiques, ideas, and suggestions are welcome. The goal is to know how to send a MAVLink message to later on control the transponder from stby to alt.

Hi! I don’t have a known-working solution for you but have myself been trying to send Mavlink out from a lua script and only got things working after several hours of scratching my head. I’m only chiming in here because I do have send_chan working for STATUSTEXT messages…

I found the message encoding part was most prone to failing silently - are you able to prepare a working message payload in StIL/Mavproxy, like the example command here?

I also wasn’t fully understanding that I had to generate stubs for all the commands I wanted to use and was getting confusing results until then - have you done the mavegn.py stuff mentioned here?

Sorry if this isn’t what you’re hoping for - just trying to help!

AKA

I was able to produce all the MAVLink messages for LUA. MAVGEN failed to generate the LUA files (it outputs an empty folder but it works for all the other languages), but pymavlink got the job done for LUA.

I do not know why it is not working. It seems to be ignoring my MAVLink messages. I think I might be missing something.

Can you or anyone else post a working code that sends a MAVlLink message successfully to use as a reference?