Accessing real-time MAVLink messages via pymavlink

Greetings,
We are working on a AUV system. Currently i am searching that how can i access to the real-time MAVLink messages. ( I want to access the datas which can seen on QGround’s MAVLink Inspector section). I’m doing researches for a long while and had no results. We are using a Pixhawk2.4.8 and a Jetson Nano.

For instance,
i am using a depth sensor, how can i access to the current depth values in real time via pymavlink?
or
how can i access to the AHRS2 values that constantly updating on terminal?

I’ve succeeded to connect with my master computer and succeeded to print "PARAM_VALUES" on terminal, but when i try to change it to "AHRS2" from "PARAM_VALUES" it returns None on terminal.

Note: Terminal returns Null on "AHRS2" whether i arm the vehicle or not.

import time
import sys
from pymavlink import mavutil

master = mavutil.mavlink_connection("/dev/ttyACM0", baud=115200)
master.wait_heartbeat()
print("HEARTBEAT_DONE")
 
master.param_fetch_all()
 
while True:
            m = master.recv_match(type = "PARAM_VALUES", blocking = True, timeout = 1)
            print(m)
            if m is None:
                        break

Any help will be appreciated, thanks.

Hi @toosat, welcome to the forum :slight_smile:

The Pixhawk will send a small set of vital messages by default, but other messages you’re interested in need to be requested. QGC requests a large set of messages automatically, whereas if you’re connecting directly to the Pixhawk with Pymavlink you’ll need to request those messages yourself. You can request a message to be sent regularly using MAV_CMD_SET_MESSAGE_INTERVAL, which we have a Pymavlink example for here :slight_smile:

There’s an alternative implementation here, as part of a broader full program example of controlling a vehicle (requires Python >= 3.8). The code is a bit more involved, but as a result it should be easier/simpler to use.

1 Like

Thanks for the quick response @EliotBR ,
Yesterday, we implemented the code based on information that you gave me. And we succeded to see the variables that we wanted to see on terminal. I’ll be posting my code here in a few days to help other forum users.

2 Likes

As one can see in the image, I’m currently only getting a heartbeat and timesync data back from the Pixhawk 4. Any idea on how I can get more? Like Attitude or GPS data?

Thank you a lot for the help in advance :slight_smile:.

Best regards

Per

Hi @PerFrivik,

I’ve moved your comment into this post because it’s on the same topic. I’d recommend you read my comment above, and ask follow-ups if something isn’t clear :slight_smile:

1 Like

You’re the best of the best! Exactly what I needed, thank you so much again!

1 Like

Hi @PerFrivik ,
Here is my code to get ‘alt’ value through “VFR_HUD” message. I hope it’ll be helpful for you.

def request_message_interval(master, message_input: str, frequency_hz: float):

    message_name = "MAVLINK_MSG_ID_" + message_input

    message_id = getattr(mavutil.mavlink, message_name)

    master.mav.command_long_send(

        master.target_system, master.target_component,

        mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0,

        message_id,

        1e6 / frequency_hz,

        0,

        0, 0, 0, 0)

   

    print("Requested the message successfully.")

   

def get_requested_data(master, message_name: str, dict_key: str, value_unit: str, save_name: str):

    try:

        message_index = 0

        dict1 = master.recv_match(type= message_name, blocking=True, timeout=0.1).to_dict()

        dict_value = dict1[dict_key]

       

        toWrite = "Message_Index, " + message_index + " :" + str(dict_value) + value_unit

        with open(save_name, 'a') as file:

            file.write(toWrite)

            file.write('\n')  

            message_index += 1

    except:

        pass

request_message_interval(master, "VFR_HUD", 1)

while True:

    try:

        get_requested_data(master, "VFR_HUD", 'alt', "m", save_name)

    except:

        pass

You can use these functions to get any message you want. You just need to rename the message names with the ones you want. Additionally these functions are saving the message outputs to a empty file.
Regards.

1 Like

Hello Toosat,

I really appriciate the help :), thank you so much!

1 Like

I use your and some other method but it seems like I’m only getting heartbeat and timesync messages. Do you know how can I fix this? I think it’s related to some connection issue but I’m not sure. I can run the same command in my laptop but not in laptop. Baud rates is correct.

Edit: I think it somehow related to my first connection. If I open my laptop/raspberry and directly try to print the mavlink messages I’m only getting the heartbeat. But first if I give some order via that mavlink (such as calling the parameters in mission planner, or arming the motors), I can receive the all mavlink messages.

Should I put some command before the getting the parameters??

Hi @emir0723, welcome to the forum :slight_smile:

To receive messages beyond the defaults you’ll need to either

  1. Request them individually, as in my first comment, or
  2. Set the relevant stream group rate (if the message(s) you’re interested in are in one of the available groups)

As mentioned in my first comment, Control Station Software (like QGroundControl and Mission Planner) will request particular message rates when they connect to the vehicle, so if you open one of those softwares before running your own program then there will be additional messages being streamed because they’ve been requested.

Parameters are not accessed via standard MAVLink streams - they use the Parameter Protocol, which we have a couple of basic pymavlink examples for here.