Is it possible to send MAVLink messages through lua scripts?

Hello guys, i’m doing a project that i need my rover to move forward until a certain point is reached, and then, go in reverse without turning 180 degrees. I saw some simillar projects that use the DO_SET_REVERSE command. Is it possible to use this command in GUIDED mode and send the MAVLink message to use this function using a lua script?

Thanks in advance!

Hi @gsouza ! Do_Set_Reverse works in auto mode but I don’t think it works in guided mode.

Anyway, here’s an example lua script to send mavlink message with COMMAND_LONG using lua:

Just copy ardupilot/libraries/AP_Scripting/modules to your scripts folder

local mavlink_msgs = require("MAVLink/mavlink_msgs")

local MAV_MSG = <Command ID>  -- Command ID for Mavlink message, set it to '194' for DO_SET_REVERSE
local MAVLINK_CHANNEL = 0

local function send_command()
    -- example param
    local param1 = 0
    local message = mavlink_msgs.encode("COMMAND_LONG", {
        target_system = 1,        -- Target system
        target_component = 1,     -- Target component
        command = MAV_MSG,        -- MAVLink command ID
        confirmation = 0,         -- 0 for no confirmation
        param1 = param1,
        param2 = 0,
        param3 = 0,
        param4 = 0,
        param5 = 0,
        param6 = 0,
        param7 = 0
    })

    -- Send the message
    local mav_send = mavlink:send_chan(MAVLINK_CHANNEL, 76, message) -- 76 = ID for COMMAND_LONG

end

return send_command()

2 Likes

Hey @snktshrma!

I’ve seen this issue right here. I guess i should be able to use this command in guided mode, as i’m using the newest firmware for ArduRover available.

Anyways, i will try to run some tests right here to confirm if this is even possible to use in guided mode.

Thank you so much for the support!

1 Like

Hello @snktshrma, hope you doing great!

So, i’ve tried to run one script that i try to send a MAVLink command through lua, but i’ve got following error.


Could you help me understand why this error occurred? I added the required scripts on the path you gave me to my scripts folder, and then this error appeared.

Thanks in advance!

Hi @gsouza !
So the folder structure should be like this:

scripts/
β”œβ”€β”€ modules
β”‚ β”œβ”€β”€ MAVLink
β”‚ β”‚ β”œβ”€β”€ mavlink_msg_COMMAND_ACK.lua
β”‚ β”‚ β”œβ”€β”€ mavlink_msg_COMMAND_LONG.lua
β”‚ β”‚ β”œβ”€β”€ mavlink_msg_FOLLOW_TARGET.lua
β”‚ β”‚ β”œβ”€β”€ mavlink_msg_GLOBAL_POSITION_INT.lua
β”‚ β”‚ β”œβ”€β”€ mavlink_msg_HEARTBEAT.lua
β”‚ β”‚ β”œβ”€β”€ mavlink_msg_SERIAL_CONTROL.lua
β”‚ β”‚ β”œβ”€β”€ mavlink_msgs.lua
β”‚ β”‚ └── Readme.md
β”‚ β”œβ”€β”€ mavport.lua
β”‚ └── NMEA_2000.lua
β”œβ”€β”€ test.lua

You have to copy β€˜modules’ folder into your scripts folder. That’ll solve the issue.

Let me know if you’ll face any issue

Hi ,
I s there a way to send SET_ATTITUDE_TARGET mavlink message in guide mode , i write a lua code but doesn’t work when i run this code

local mavlink_msgs = require(β€œMAVLink/mavlink_msgs”)

local MAV_MSG = 82
local MAVLINK_CHANNEL = 0

local message = mavlink_msgs.encode("SET_ATTITUDE_TARGET", {
    time_boot_ms = 1000,        -- Target system
    target_system = 1,     -- Target component
    target_component = 1,        -- MAVLink command ID
    type_mask = 2,         -- 0 for no confirmation
    q = {0.7071, 0, -0.7071, 0} ,
    body_roll_rate = 1.6,
    body_pitch_rate = 2.1,
    body_yaw_rate = 3.1,
    thrust = 0.6,
    thrust_body  = 0      
})

-- Send the message
mavlink:send_chan(MAVLINK_CHANNEL, 82, message) -- 76 = ID for COMMAND_LONG