生成 MAVLink 库文件
通过使用代码生成器从XML格式定义的消息中生成不同编程语言使用的MAVLink库文件。
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
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.
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/. >注意*mavgen*可以处理包含XML文件相对路径的方言(例如,最常见的common.xml),但是其他生成器可能不行。 We recommend putting custom dialects in the same folder as the ones that come with the mavlink/mavlink repository. > 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是用Python语言编写的MAVLink的图形用户界面代码生成器。
Mavgenerate provides a GUI front end to the mavgen command line code generator, and supports the same options.
可以使用Pythond的-m
参数从任何地方打开这个图形用户界面。
python3 -m mavgenerate
Steps for generating the MAVLink library code:
Choose the target XML file (typically in mavlink/message_definitions/1.0).
如果使用自定义文件,首先需要将其复制到上面的目录中(如果自定义文件依赖于common.xml,则必须确保两者位于同一目录中)。
选择输出路径(例如mavlink/include)。
Select the target output programming language.
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 isJavaScript_Stable
.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.
Building MAVLink libraries using the Mavgen Command Line Tool
mavgen.py is a command line tool for generating MAVLink libraries for various programming languages. 将mavlink
路径添加到PYTHONPATH
后,可以通过命令行来运行它。 However, if you are outside of the mavlink directory, you need to add the mavlink
directory to the PYTHONPATH
environment variable.
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
--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 {C,CS,JavaScript,JavaScript_Stable,JavaScript_NextGen,TypeScript,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
optional arguments:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
output directory.
--lang {C,CS,JavaScript,JavaScript_Stable,JavaScript_NextGen,TypeScript,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. [默认:1.0]
--no-validate 不进行XML规范验证。 如果已经确定XML文件是正确的,这样做有助于加快代码生成速度。
--error-limit ERROR_LIMIT
可以显示的最大验证错误数
--strict-units 执行单元属性的验证。 Can speed up code generation if XML files are known to be correct.
--error-limit ERROR_LIMIT
可以显示的最大验证错误数
--strict-units 执行单元属性的验证。