LUA - read named float mavlink msg failed

Hello everyone,

I’m facing two challenges related to Named Float values in my ArduPilot setup:

1. Issue with Mission Planner:

  • I successfully set a Named Float value using pymavlink.
  • I can observe the Named Float variable (e.g., “my_named_value”) in the Mission Planner’s Quick View Tab.
  • However, the value associated with the variable always remains 0.
  • Using Mavlink Inspector, I’ve confirmed that Mission Planner is indeed sending the Named Float message to the Autopilot.
  • I’m puzzled as to why the value isn’t correctly displayed in the Quick tab.
  1. Reading Named Float Values in Lua:

I’m encountering an issue while trying to read Named Float values in my ArduPilot Lua script.

  • I’ve written a Lua script to listen for Named Float messages.
  • The script appears to be running without errors, but it doesn’t seem to be receiving any Named Float messages from Mission Planner.
  • I’ve verified this by using gcs:send_text(0, " **** ") that the if msg then block in my script is not being executed.
  • I used gcs:send_text(0, " **** ") to confirm that the script isn’t receiving messages.
  • Additionally, I verified that the NAMED_VALUE_FLOAT_msgid is correctly defined as 251 within my script.

Can you help please.
Lua script:

function mavlink_receiver()
   local mavlink_msgs = require("mavlink_msgs")
   local NAMED_VALUE_FLOAT_msgid = mavlink_msgs.get_msgid("NAMED_VALUE_FLOAT")
   local msg_map = {}
   msg_map[NAMED_VALUE_FLOAT_msgid] = "NAMED_VALUE_FLOAT"
   -- initialise mavlink rx with number of messages, and buffer depth
   mavlink.init(1, 10)

   -- register message id to receive
   mavlink.register_rx_msgid(NAMED_VALUE_FLOAT_msgid)
   local msg,_,timestamp_ms = mavlink.receive_chan()
   
   if msg then
      local parsed_msg = mavlink_msgs.decode(msg, msg_map)
      if (parsed_msg ~= nil) and (parsed_msg.msgid == NAMED_VALUE_FLOAT_msgid) then
         -- convert remote timestamp to local timestamp with jitter correction
         --local time_boot_ms = jitter_correction.correct_offboard_timestamp_msec(parsed_msg.time_boot_ms, timestamp_ms:toint())
         local value = parsed_msg.value
         local name = bytes_to_string(parsed_msg.name)
         gcs:send_text(0, "Got mode change ....  " .. value)
      end
   end

   return mavlink_receiver, 1000
end

return mavlink_receiver()

The python code:

from pymavlink import mavutil
import time

mavlink_connection_string = "udpout:127.0.0.2:14550" #"tcp:192.168.43.1:5760" #
mav = mavutil.mavlink_connection(mavlink_connection_string)

def send_named_value_float(mav_, name, value):
    # Get the time_boot_ms as the time difference since the first heartbeat received
    time_boot_ms = int((time.time() - mav_.start_time) * 1000)
    # Ensure the name is exactly 10 bytes long
    encoded_name = name.encode('ascii').ljust(10, b'\0')
    mav_.mav.named_value_float_send(time_boot_ms, encoded_name, value)
    
def main():
    try:
        while (1):
            # Send the NAMED_VALUE_FLOAT message 
            send_named_value_float(mav, 'ty1 980', 5)
            time.sleep(0.1)
            send_named_value_float(mav, 'Battery1 voltage', 4)
            time.sleep(0.1)
            send_named_value_float(mav, 'lol voltage', 6)
            print("send   ")
            time.sleep(0.2)  # Wait for 0.5 seconds before sending the next message

    except KeyboardInterrupt:
        # Handle any cleanup or resource releasing if necessary
        print("Interrupted by user, stopping message sending.")
main()

The Mavlink inspector:

Quick view tab - Mission planner:

@Yuri_Rage

Hello Mr. Yuri,
I’ve noticed your expertise in this area and I’m hoping you can assist me with a challenge I’m facing.

I am still encountering issues with sending and receiving Named Float values using MAVLink.

After thorough investigation, I’ve observed that my ArduPilot Lua script is working wee and receiving MAV_COMP_ID_USER52 messages. in this code:

local mavlink_msgs = require("mavlink_msgs")

local COMMAND_ACK_ID = mavlink_msgs.get_msgid("COMMAND_ACK")
local COMMAND_LONG_ID = mavlink_msgs.get_msgid("COMMAND_LONG")

local msg_map = {}
msg_map[COMMAND_ACK_ID] = "COMMAND_ACK"
msg_map[COMMAND_LONG_ID] = "COMMAND_LONG"

mavlink:init(1, 10)

mavlink:register_rx_msgid(COMMAND_ACK_ID)
mavlink:register_rx_msgid(COMMAND_LONG_ID)

function update()   
   local msg, chan = mavlink:receive_chan()
   if (msg ~= nil) then
      local parsed_msg = mavlink_msgs.decode(msg, msg_map)
      if (parsed_msg ~= nil) then
         gcs:send_text(0, "msg ID is   " ..parsed_msg.msgid)
      end
   end
   return update, 1000
end
return update()

the output is:

msg ID is    76

I understand that the Ground Control Station (GCS) itself might not directly send Named Float messages to the Autopilot. Instead, the GCS may require a specific request or trigger from the Autopilot to initiate the transmission of Named Float data.

Could you please guide me on how to:

  1. Request or trigger the transmission of Named Float values from the GCS within my Lua script.
  2. Effectively receive and process the transmitted Named Float messages within my Lua script.

Note, the named float msg defined by me as a LUA script uploaded to the autopilot, is it correct?

local NAMED_VALUE_FLOAT = {}
NAMED_VALUE_FLOAT.id = 251

NAMED_VALUE_FLOAT.fields = {
 { "timestamp", "<I4" },
 { "value", "<f" },
 { "name", "<c", 10 },
}

return NAMED_VALUE_FLOAT

Thank you for your time and assistance.