'Getting message interval/data_stream to work in MAVlink - Python

I've been playing with this for a couple of days but cannot work out how to get a MAVlink message interval to change. I'm trying to get a nice smooth stream of GLOBAL_POSITION_INT, but no matter what I change I seem to be getting data 1-2 times a second.

I have tried using both 'request_data_stream' and 'MAV_CMD_SET_MESSAGE_INTERVAL' with no luck. this is my attempt at message_interval (it's mostly copy-pasted from examples):

    def request_message_interval(message_id: int, frequency_hz: float):

    master.mav.command_long_send(
        master.target_system, master.target_component,
        mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0,
        message_id,  # The MAVLink message ID
        1e6 / frequency_hz,
        # The interval between two messages in microseconds. Set to -1 to disable and 0 to request default rate.
        0, 0, 0, 0,  # Unused parameters
        0,
    ) 

   request_message_interval(mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT,
                          20)

and this is my attempt to get data_stream to work:

    def request_data_stream(message_id: int, frequency_hz: float):

    master.mav.request_data_stream_send(
        master.target_system, master.target_component,
        message_id,  # The MAVLink message ID
        frequency_hz,
        1,#start
  )

  request_data_stream(mavutil.mavlink.MAV_DATA_STREAM_POSITION, 20)

I receive the messages then pass it onto other parts of the program here:

while True:
time.sleep(0.1)
try:
    master.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS, mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
    msg = master.recv_match(type='GLOBAL_POSITION_INT', blocking=True)
    # print("Message: %s" % msg)
    lon = master.messages['GLOBAL_POSITION_INT'].lon
    lat = master.messages['GLOBAL_POSITION_INT'].lat
    alt = master.messages['GLOBAL_POSITION_INT'].alt
    hdg = master.messages['GLOBAL_POSITION_INT'].hdg
    print(f"lon:{lon}, lat:{lat}, alt:{alt}, hdg:{hdg}")
    calc_azimuth(str(ref_lon), str(ref_lat), str(lon), str(lat))

except:
    pass

I feel like I am missing something obvious here, but I cannot work out why messages are coming in so slow.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source