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

Mavlink and PX4 Autopilot 3: Pymavlink Escaped from Pixahwk

Mavlink is not confined in PX4 projects only. Mavlink has general encoder and decoder like many other message protocols.

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

In the previous articles, we explored connecting to PX4 using Pymavlink and executing a data collection demo. Now, we delve into a broader application of Mavlink: can it be used for multiple robots without PX4 firmware or Pixhawk hardware? This article investigates this possibility.

Table of Contents

  1. Introduction
  2. Demonstration Script
  3. Understanding the Code
  4. Conclusion

Demonstration Script

Firstly, let's examine a demonstration script. The file name used here is pymavlink3.py. The script is straightforward; it encodes and decodes a Mavlink message.

bash

Line #:

from pymavlink.dialects.v20.common import MAVLink mav = MAVLink(None) # get single msg object msg_object = mav.attitude_encode(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6) print("Define a new custom message") print(msg_object) # serialize the msg bin_msg = msg_object.pack(mav) print("Encoded Message (type of bytes)") print(bin_msg) # the msg buf is the one we need lastly msg_buf = msg_object.get_msgbuf() print("Encoded Message Buffer (type of bytearray)") print(msg_buf) # now decode the binary(serialized) msg new_msg_object = mav.decode(msg_buf) print("Decoded Message") print(new_msg_object) print("Note that the variables in float are not exact...")

To execute the script, use the following command:

bash

Line #:

python pymavlink3.py

The output of this script is demonstrated below.

Understanding the Code

The pymavlink package provides a suite of tools, including Mavlink definitions in Python. To use an object that encodes and decodes messages, import and instantiate the MAVLink object:

bash

Line #:

from pymavlink.dialects.v20.common import MAVLink mav = MAVLink(None)

Encoding Attitude Message

Encoding a Mavlink message is a concise process. The first argument is an integer timestamp, set to 0 for simplicity. The subsequent arguments are roll, pitch, yaw, rollspeed, pitchspeed, and yawspeed, respectively. Here, random numbers are used for easy verification:

bash

Line #:

msg_object = mav.attitude_encode(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6)

Decoding the Byte Array

To decode a received byte array into a human-readable format, follow these steps:

  1. Obtain the buffer and assign it to a variable.
  2. Parse it using Mavlink.

bash

Line #:

msg_buf = msg_object.get_msgbuf() new_msg_object = mav.decode(msg_buf)

The parsed message will appear as follows:

bash

Line #:

ATTITUDE {time_boot_ms : 0, roll : 0.10000000149011612, pitch : 0.20000000298023224, yaw : 0.30000001192092896, rollspeed : 0.4000000059604645, pitchspeed : 0.5, yawspeed : 0.6000000238418579}

Note on Float Representation

It's important to note that floating-point variables do not precisely retain their original values. For example, an input of 0.1 for roll speed, when decoded, results in a value of 0.10000000149011612. This discrepancy is not unique to Mavlink but a general characteristic of floating-point representation in digital systems.

Conclusion

This exploration into using Mavlink for general purposes, beyond the confines of PX4 firmware or Pixhawk hardware, demonstrates its versatility. The ability to encode and decode messages efficiently opens up new possibilities for its application in diverse robotic systems.

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