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.