Lua Script to Look for a Mavlink Message

I am trying to use a Lua script to check for a Mavlink message. I am using the code below. I am using the most current version of mission planner and the stable copter firmware.

For some reason I get this error message: ndex a nil value (global ‘mavlink’).
I am not sure why Mavlink is not recognized. It is in the Lua API so I thought the binding should work.

– register the message id you want to listen for
mavlink:register_rx_msgid(mavlink_msgs.get_msgid(“MAV_MODE_STABILIZE_ARMED”))

– create a function to check for the message
function check_for_message()
– receive the message
local msg, chan, timestamp = mavlink:receive_chan()
if msg then
– process the message here

end

end

– create a repeating timer to check for the message every 3 seconds
local timer = mist.scheduleRepeating(check_for_message, 3)

1 Like

The mavlink bindings are not in stable.

In other words, update to a newer firmware version.

Thank you both for the information. That makes a lot of sense. I will update and give it another try.

I am using firmware 4.5.0 in simulation mode to test the code now. It fixed my problem with Mavlink not being recognized but I ran into a new issue.

Now mavlink_msgs.get_msgid is not being recognized. That is what it says to use in the API document, so I am not sure why it is not working now that I have the updated firmware.

I also tried using mavlink:register_rx_msgid(0) instead but that throws out a new error to see if putting an ID number would work. When I made that change the message I get now says Out of Mavlink.

I hate to ask so many questions, but I can’t find much information about this online. I appreciate any advice I can get.

This example should help:
ardupilot/libraries/AP_Scripting/examples/MAVLinkHL.lua at master · ArduPilot/ardupilot · GitHub