Language-specific MAVLink libraries can be created from XML Message Definitions using code generator tools.
This page shows how to use the following two code generators provided with the MAVLink project:
Generators for other programming languages are supported and documented in independent projects. For more information see Supported Languages.
- You must already have Installed the MAVLink toolchain, which includes the mavgenerate and mavgen tools used below as well as the XML Message Definitions.
- If you are generating messages for a custom dialect, copy the dialect XML definition file(s) into the directory message_definitions/v1.0/. > Note mavgen can handle dialects that have relative paths for included XML files (e.g typically common.xml), but other generators may not. We recommend putting custom dialects in the same folder as the ones that come with the mavlink/mavlink repository.
mavgenerate.py is GUI code generator for MAVLink, written in Python.
The GUI can be launched from anywhere using Python's
python3 -m mavgenerate
Steps for generating the MAVLink library code:
Choose the target XML file (typically in mavlink/message_definitions/1.0).
If using a custom dialect, first copy it into the above directory (if the dialect is dependent on common.xml it must be located in the same directory).
Choose an output directory (e.g. mavlink/include).
Select the target output programming language.
Select the target MAVLink protocol version. Ideally use 2.0 if the generator supports it.
Generation will fail if the protocol is not supported by the selected programming language.
Optionally check Validate and/or Validate Units, which validates XML specifications.
Click Generate to create the source files for the MAVLink library and the chosen dialect.
mavgen.py is a command line tool for generating MAVLink libraries for various programming languages. After the
mavlink directory has been added to the
PYTHONPATH, it can be run by executing from the command line.
Mavgen is the backend used by mavgenerate. The documentation below explains all the options for both tools.
Below is an example for how to generate MAVLink 2 libraries for the C programming language using a dialect named your_custom_dialect.xml:
python3 -m pymavlink.tools.mavgen --lang=C --wire-protocol=2.0 --output=generated/include/mavlink/v2.0 message_definitions/v1.0/your_custom_dialect.xml
The syntax for for generating Python modules is the same, except that the
--outputspecifies a filename rather than a directory.
The full syntax and options can be output by running
mavgen.py -h flag, which is reproduced below: