Files
PX4 Build Bot aa19069c30 New Crowdin updates (#677)
* New translations index.md (Korean)

* New translations payload.md (Korean)

* New translations traffic_management.md (Korean)

* New translations define_xml_element.md (Korean)

* New translations mavlink_version.md (Korean)

* New translations index.md (Korean)

* New translations faq.md (Korean)

* New translations contributing.md (Korean)

* New translations generate_libraries.md (Korean)

* New translations installation.md (Korean)

* New translations mavlink_2.md (Korean)

* New translations message_signing.md (Korean)

* New translations packet_loss.md (Korean)

* New translations serialization.md (Korean)

* New translations wireshark.md (Korean)

* New translations xml_schema.md (Korean)

* New translations crc.md (Korean)

* New translations index.md (Korean)

* New translations howto_requestmessages.md (Korean)

* New translations mission.md (Korean)

* New translations offboard_control.md (Korean)

* New translations parameter.md (Korean)

* New translations timesync.md (Korean)

* New translations arm_authorization.md (Korean)

* New translations battery.md (Korean)

* New translations camera_v1.md (Korean)

* New translations command.md (Korean)

* New translations events.md (Korean)

* New translations gimbal_v2.md (Korean)

* New translations component_metadata.md (Korean)

* New translations index.md (Chinese Simplified)

* New translations payload.md (Chinese Simplified)

* New translations traffic_management.md (Chinese Simplified)

* New translations define_xml_element.md (Chinese Simplified)

* New translations mavlink_version.md (Chinese Simplified)

* New translations index.md (Chinese Simplified)

* New translations faq.md (Chinese Simplified)

* New translations contributing.md (Chinese Simplified)

* New translations generate_libraries.md (Chinese Simplified)

* New translations installation.md (Chinese Simplified)

* New translations mavlink_2.md (Chinese Simplified)

* New translations message_signing.md (Chinese Simplified)

* New translations packet_loss.md (Chinese Simplified)

* New translations serialization.md (Chinese Simplified)

* New translations wireshark.md (Chinese Simplified)

* New translations xml_schema.md (Chinese Simplified)

* New translations crc.md (Chinese Simplified)

* New translations index.md (Chinese Simplified)

* New translations howto_requestmessages.md (Chinese Simplified)

* New translations mission.md (Chinese Simplified)

* New translations offboard_control.md (Chinese Simplified)

* New translations parameter.md (Chinese Simplified)

* New translations timesync.md (Chinese Simplified)

* New translations arm_authorization.md (Chinese Simplified)

* New translations battery.md (Chinese Simplified)

* New translations camera_v1.md (Chinese Simplified)

* New translations command.md (Chinese Simplified)

* New translations events.md (Chinese Simplified)

* New translations gimbal_v2.md (Chinese Simplified)

* New translations component_metadata.md (Chinese Simplified)

* New translations common.md (Korean)

* New translations common.md (Chinese Simplified)
2026-03-10 12:15:23 +11:00

5.7 KiB

生成 MAVLink 库文件

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:

  • GUI tool: mavgenerate
  • Command line tool: mavgen, which is the backend tool used also by mavgenerate

:::info These generators can build MAVLink 2 and MAVLink 1 libraries for the following programming languages: C, C++11, Python, Typescript, Java, and WLua. The following programming languages are supported for MAVLink 1 libraries only: C#, JavaScript, ObjC, Swift. :::

:::tip Generators for other programming languages are supported and documented in independent projects. For more information see Supported Languages. :::

先决条件

  1. You must already have Installed the MAVLink toolchain, which includes the mavgenerate and mavgen tools used below as well as the XML Message Definitions.

  2. If you are generating messages for a custom dialect, copy the dialect XML definition file(s) into the directory message_definitions/v1.0/.

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

Building MAVLink libraries using the Mavgenerate GUI

mavgenerate.py is GUI code generator for MAVLink, written in Python.

:::info Mavgenerate provides a GUI front end to the mavgen command line code generator, and supports the same options. :::

The GUI can be launched from anywhere using Python's -m argument:

python3 -m mavgenerate

mavgenerate UI

Steps for generating the MAVLink library code:

  1. Choose the target XML file (typically in mavlink/message_definitions/1.0).

    ::: info 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). :::

  2. Choose an output directory (e.g. mavlink/include).

  3. Select the target output programming language.

    mavgenerate UI - language list

    For JavaScript in particular there are three options:

    • JavaScript_Stable is an older version that only supports MAVLink 1.0,
    • JavaScript_NextGen is a more recent version that supports MAVLink 1 and 2 along with signing.
    • JavaScript is a "proxy" for the recommended version. Currently this is JavaScript_Stable.
  4. Select the target MAVLink protocol version. Ideally use 2.0 if the generator supports it.

    ::: warning Generation will fail if the protocol is not supported by the selected programming language. :::

  5. Optionally check Validate and/or Validate Units, which validates XML specifications.

  6. Click Generate to create the source files for the MAVLink library and the chosen dialect.

Building MAVLink libraries using the Mavgen Command Line Tool

mavgen.py is a command line tool for generating MAVLink libraries for various programming languages. You can run mavgen from the mavlink directory. However, if you are outside of the mavlink directory, you need to add the mavlink directory to the PYTHONPATH environment variable.

:::tip 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

:::info The syntax for generating Python modules is the same, except that the --output specifies a filename rather than a directory. :::

The full syntax and options can be output by running mavgen.py -h flag, which is reproduced below:

usage: mavgen.py [-h] [-o OUTPUT] [--lang {Ada,C,CS,JavaScript,JavaScript_Stable,JavaScript_NextGen,TypeScript,Python2,Python3,Python,Lua,WLua,ObjC,Swift,Java,C++11}]
                 [--wire-protocol {0.9,1.0,2.0}] [--no-validate] [--error-limit ERROR_LIMIT] [--strict-units]
                 XML [XML ...]

This tool generate implementations from MAVLink message definitions

positional arguments:
  XML                   MAVLink definitions

options:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        output directory.
  --lang {Ada,C,CS,JavaScript,JavaScript_Stable,JavaScript_NextGen,TypeScript,Python2,Python3,Python,Lua,WLua,ObjC,Swift,Java,C++11}
                        language of generated code [default: Python]
  --wire-protocol {0.9,1.0,2.0}
                        MAVLink protocol version. [default: 1.0]
  --no-validate         Do not perform XML validation. Can speed up code generation if XML files are known to be correct.
  --error-limit ERROR_LIMIT
                        maximum number of validation errors to display
  --strict-units        Perform validation of units attributes.