MAVLink Messaging
MAVLink is a very lightweight messaging protocol that has been designed for the drone ecosystem.
PX4 uses MAVLink to communicate with QGroundControl (and other ground stations), and as the integration mechanism for connecting to drone components outside of the flight controller: companion computers, MAVLink enabled cameras etc.
The protocol defines a number of standard messages and microservices for exchanging data (many, but not all, messages/services have been implemented in PX4).
This tutorial explains how you can add PX4 support for your own new "custom" messages.
The tutorial assumes you have a custom uORB ca_trajectory
message in msg/ca_trajectory.msg
and a custom MAVLink ca_trajectory
message in mavlink/include/mavlink/v2.0/custom_messages/mavlink_msg_ca_trajectory.h
.
Defining Custom MAVLink Messages
The MAVLink developer guide explains how to define new messages and build them into new programming-specific libraries:
Your message needs to be generated as a C-library for MAVLink 2. Once you've installed MAVLink you can do this on the command line using the command:
For your own use/testing you can just copy the generated headers into PX4-Autopilot/mavlink/include/mavlink/v2.0.
To make it easier for others to test your changes, a better approach is to add your generated headers to a fork of https://github.com/mavlink/c_library_v2. PX4 developers can then update the submodule to your fork in the PX4-Autopilot repo before building.
Sending Custom MAVLink Messages
This section explains how to use a custom uORB message and send it as a MAVLink message.
Add the headers of the MAVLink and uORB messages to mavlink_messages.cpp
Create a new class in mavlink_messages.cpp
Finally append the stream class to the streams_list
at the bottom of mavlink_messages.cpp
Then make sure to enable the stream, for example by adding the following line to the startup script (e.g. /ROMFS/px4fmu_common/init.d-posix/rcS on NuttX or ROMFS/px4fmu_common/init.d-posix/rcS) on SITL. Note that -r
configures the streaming rate and -u
identifies the MAVLink channel on UDP port 14556).
You can use the uorb top [<message_name>]
command to verify in real-time that your message is published and the rate (see uORB Messaging). This approach can also be used to test incoming messages that publish a uORB topic (for other messages you might use printf
in your code and test in SITL).
To see the message on QGroundControl you will need to build it with your MAVLink library, and then verify that the message is received using MAVLink Inspector Widget (or some other MAVLink tool).
Receiving Custom MAVLink Messages
This section explains how to receive a message over MAVLink and publish it to uORB.
Add a function that handles the incoming MAVLink message in mavlink_receiver.h
Add a function that handles the incoming MAVLink message in the MavlinkReceiver
class in mavlink_receiver.h
Add an uORB publisher in the MavlinkReceiver
class in mavlink_receiver.h
Implement the handle_message_ca_trajectory_msg
function in mavlink_receiver.cpp
and finally make sure it is called in MavlinkReceiver::handle_message()
Alternative to Creating Custom MAVLink Messages
Sometimes there is the need for a custom MAVLink message with content that is not fully defined.
For example when using MAVLink to interface PX4 with an embedded device, the messages that are exchanged between the autopilot and the device may go through several iterations before they are stabilized. In this case, it can be time-consuming and error-prone to regenerate the MAVLink headers, and make sure both devices use the same version of the protocol.
An alternative - and temporary - solution is to re-purpose debug messages. Instead of creating a custom MAVLink message CA_TRAJECTORY
, you can send a message DEBUG_VECT
with the string key CA_TRAJ
and data in the x
, y
and z
fields. See this tutorial. for an example usage of debug messages.
This solution is not efficient as it sends character string over the network and involves comparison of strings. It should be used for development only!
General
Set streaming rate
Sometimes it is useful to increase the streaming rate of individual topics (e.g. for inspection in QGC). This can be achieved by typing the following line in the shell:
You can get the port number with mavlink status
which will output (amongst others) transport protocol: UDP (<port number>)
. An example would be:
Last updated