Ubicoders - Robotics and AI, Coding, and Software Development!

Mavlink and PX4 Autopilot 1: Connection and Reading Messages

Python can parse mavlink messages from Pixhawk. It is called Pymavlink.

Picture of the author
Elliot Lee - Nov.29, 2023

Mavlink, a byte message stream from Pixhawk autopilot hardware, plays a crucial role in autonomous vehicle systems. Originating as a drone flight computer, Pixhawk has expanded its applications due to its open-source nature, offering a quick prototyping platform. Despite its widespread use, newcomers often face challenges due to limited documentation, creating a steep learning curve.

Table of Contents

  1. Introduction to Mavlink and Pixhawk
  2. Personal Motivation
  3. Step-by-Step Guide to Reading Mavlink Messages
  4. Discussion
  5. Conclusion

Image from Arduipilot

Personal Motivation

During my graduate studies, I encountered difficulties in reading messages from the Pixhawk. While connecting via USB seemed straightforward, deciphering the raw bytes and understanding the message protocol was not. This was evident during my initial attempts to read simple IMU data. This article aims to guide others who might face similar challenges.

Get Python. Install pip packges for mavlink. Connect USB. Write the script and run it. Here's the step by step guides. Let me assume your system already have Python on it.

a. Installing the necessary pip packages

bash

Line #:

pip install pyserial pymavlink

b. Writing the Script

Create a file named, say, mavlink1.py. Then, copy past the code below. The code is simply send a heart beat request message and receives it. Then, in the loop, it is filtering messages for attitude and scaled imu.

bash

Line #:

#============================================================================== # Basic script to connect to PX4 and read mavlink messages. # Author: Hongyun Elliot Lee # Date: 11/29/23 #============================================================================== from pymavlink import mavutil # Create mavserial the_connection = mavutil.mavlink_connection('COM9')# /dev/ttyACM0 for linux # Sending a message creates PX4's streamer to serial. the_connection.mav.heartbeat_send( 0, #type 0, #autopilot 0, #base_mode 0, #custom_mode 0, #system_status 0, #mavlink_version ) # Check the heartbeat the_connection.wait_heartbeat() print("Heartbeat from system (system %u component %u)" % (the_connection.target_system, the_connection.target_component)) # Keep reading the mavlink messages. i.e attitude and scaled imu while True: attitude = the_connection.recv_match(type='ATTITUDE') # 30 if attitude is not None: print(attitude) scaled_imu = the_connection.recv_match(type="SCALED_IMU") # 26 if scaled_imu is not None: print(scaled_imu)

c. Running the Script

bash

Line #:

python mavlink1.py

Discussion

As you noticed from the code, one very important step is to send hearbeat like below.

bash

Line #:

# Sending a message creates PX4's streamer to serial. the_connection.mav.heartbeat_send( 0, #type 0, #autopilot 0, #base_mode 0, #custom_mode 0, #system_status 0, #mavlink_version )

This is because of PX4's firmware. The PX4 firmware has a Mavlink streamer-to-telemtry instance as default; the telemetry stream works as the device, the Pixhawk hardware turns on. However, the serial streamer does not. To wake this instance up, we need to send the heartbeat message.

How did I know? I noticed that QGroundControl sends this message at the moment when the Pixhawk device is connected.

How did I notice the messages from QGroundControl? Using a PX4 debugger and custom firmware, I could watch what messages were coming in from QGroundControl when the Pixhawk device was connected.

How do I make a custom firmware and connect the debugger? It is going to be a lengthy story. Please allow me to post the series about all these.

PX4 Autopilot firmware's mavlink stream to serial isn't turned on by default

Conclusion

Understanding and working with Mavlink and Pixhawk can initially seem daunting. Even though you have installed the suitable packages for Python and the correct script, you may experience nothing is printed. This is because of the PX4's firmware. The firmware does not have a stream instance as default for serial connection. It needs to be wakened up manually; We can wake it up by simply sending a heartbeat message request.

Indeed, this manual "wake-up" step was not even murky from the official Mavlnik documentation around 2017, probably until now. This kind of "absence" makes the people turn around. Yet, I will make it easy and accessible.

PX4 Workshop

Need to master all the PX4 topics with ROS for your robotics project? I got you covered! 🚀🤖✨ Checkout this PX4-ROS workshop: PX4-ROS Workshop

PX4 Workshop

Need to master all the PX4 topics with ROS for your robotics project? I got you covered! 🚀🤖✨ Checkout this PX4-ROS workshop: PX4-ROS Workshop

Loading...