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

Mavlink and PX4 Autopilot 2: How to Use Pymavlink for Effective Drone Attitude Data Collection and Storage

Mastering Drone Telemetry: A Step-by-Step Guide to Capturing and Archiving Flight Dynamics with Pymavlink

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

In unmanned aerial vehicles (UAVs), accurately collecting and analyzing flight data is crucial. MAVLink, a lightweight drone communication protocol, plays a pivotal role in this domain. Pymavlink, the Python implementation of MAVLink, offers an accessible way to interact with MAVLink-compatible drones. This blog post will guide you through collecting attitude data from a drone and saving it to a text file using Pymavlink.

Table of Contents

  1. Installing Pymavlink and Background
  2. Collecting Attitude Data to a Text File
  3. Running the Script
  4. Discussion
  5. Exploring Other Data Types
  6. Conclusion

I posted the first step for Pymavlink here: Basic Pymavlink

Collecting Attitude Data to a Text File

Let us write the code first. Copy and paste the code below and save it as, say, pymavlink2.py

bash

Line #:

#============================================================================== # Basic script to collect data from PX4 and save it to a file. # Author: Hongyun Elliot Lee # Date: 11/29/23 #============================================================================== from pymavlink import mavutil # Create mavserial the_connection = mavutil.mavlink_connection('COM9', baud=57600)# /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)) #============================================================================== # Function to stringify attitude message def stringify_attitude(attitude): return f"{attitude.time_boot_ms}, {attitude.roll}, {attitude.pitch}, {attitude.rollspeed}, {attitude.pitchspeed}, {attitude.yawspeed}\n" #============================================================================== file = open("attitude.txt","w") try: # 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) file.write(stringify_attitude(attitude)) finally: file.close()

Running the script

bash

Line #:

python pymavlink2.py

Console Output for Attitude Mavlink Message

The above output result shows that the message is seamlessly parsed as a dictionary-based string. The message contains the attitude data: roll, pitch, yaw, rollspeed, pitchspeed, and yawspeed.

Discussion

If you come from the first post, Basic Pymavlink , you noticed that the first half of the code is the same. It connects to the Pixhawk device.

Next, the function def stringify_attitude(attitude) is defined. Simply the function puts all the values in the attitude dictionary into a comma-separated string. As the default output is a dictionary-style string, I wished to save it as comma-separated values. The comma-separated values are often convenient to work with data analysis. For instance, numpy, pandas, and Matlab can directly read the comma-separated data file.

The last part is the simplest. It opens a file to write and write the data line by line. I put the while loop in the try-finally block to make sure flie.close() is called when a user stops the script by "ctrl+c"

Exploring Other Data Types

Yes! Thank you for asking. Many different data types exist, such as SCALED_IMU, SYS_STATUS, etc. The best and quickest way is to search through the definition list from the official website: mavlink official doc - message definitions

Next, add the target message of your interest to the script above. Once you print, you will notice which variables are in the dictionary. Then, repeat the same!

Conclusion

Collecting and storing attitude data from drones using Pymavlink is a powerful skill. This blog has guided you through the basics, and the applications are limited only by your imagination. Dive in, experiment, and see what you can achieve with this exciting technology.

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...