MAVLink Messaging
Last updated
Last updated
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 and 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.
:::note The tutorial assumes you have a 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
. :::
PX4 includes the repo as a submodule under , and generates the MAVLink 2 C header files at build time.
There are are number of XML dialect files in . The dialect that is built is specified using the variable MAVLINK_DIALECT
in ; by default this is . The files are generated into the build directory: /build/<build target>/mavlink/
.
In order to add your message we recommend that you create your messages in a new dialect file in the same directory, for example PX4-Autopilot/src/modules/mavlink/mavlink/message_definitions/v1.0/custom_messages.xml
, and set MAVLINK_DIALECT
to build the new file. This dialect file should include development.xml
.
You can alternatively add your messages to common.xml
or development.xml
. Whatever dialect file you use must eventually be built in QGroundControl (or whatever software you use to communicate with PX4).
The MAVLink developer guide explains how to define new messages in .
You can check that your new messages are built by inspecting the headers generated in the build directory. If your messages are not built they may be incorrectly formatted, or use clashing ids. Inspect the build log for information.
:::note The has more information about using the MAVLink toolchain. :::
This section explains how to use a custom uORB message and send it as a MAVLink message.
This section explains how to receive a message over MAVLink and publish it to uORB.
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.
:::note 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! :::
Ultimately you'll want to test your new MAVLink interface is working by providing the corresponding ground station or MAVSDK implementation. As a first step, and while debugging, commonly you'll just want to confirm that any messages you've created are being sent/received as you expect.
There are several approaches you can use to view traffic:
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:
Add the headers of the MAVLink and uORB messages to
Create a new class in
Finally append the stream class to the streams_list
at the bottom of
Then make sure to enable the stream, for example by adding the following line to the (e.g. on NuttX or ) on SITL. Note that -r
configures the streaming rate and -u
identifies the MAVLink channel on UDP port 14556).
:::tip You can use the uorb top [<message_name>]
command to verify in real-time that your message is published and the rate (see ). 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 , and then verify that the message is received using (or some other MAVLink tool). :::
Add a function that handles the incoming MAVLink message in
Add a function that handles the incoming MAVLink message in the MavlinkReceiver
class in
Add an uORB publisher in the MavlinkReceiver
class in
Implement the handle_message_ca_trajectory_msg
function in
and finally make sure it is called in
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 . for an example usage of debug messages.
Create a for your dialect. This allows you to inspect MAVLink traffic on an IP interface - for example between QGroundControl or MAVSDK and your real or simulated version of PX4.
associate with your MAVLink message.
View received messages in the QGroundControl . For the messages to appear you will need to including a pre-built C library that contains your custom messages.
QGC uses a pre-built C library that must be located at in the QGC source. By default this is pre-included as a submodule from https://github.com/mavlink/c_library_v2 but you can
QGC uses the ArduPilotMega.xml dialect by default, which includes common.xml. You can include your messages in either file or in your own dialect. However if you use your own dialect then it should include ArduPilotMega.xml (or it will miss all the existing messages), and you will need to change the dialect used by setting it in when running qmake.