工具
对QGC进行二次开发时,常常会遇到想使用自定义mavlink的情况,但不像APM那样编译命令会根据xml文件自动生成mavlink协议。QGC似乎不能自动生成mavlink协议(之前试过似乎不能自动生成协议,不知道随后的版本能不能自动生成),需要使用第三方工具。
生成协议的工具有两个,一个是mavgenerate.py(带界面的),一个是mavgen.py(不带界面的),之前一直用带界面的,虽然不是很好用,但因为是图形界面的,上手比较容易,也能完成生成mavlink协议的功能,但是最近再用的时候,发现有报错(QGC 最后的提交为a54417e5ef,2019年3月5号)。界面出问题后,只能寻找其他的办法,找来找去,发现了mavgen.py这个工具,据官方说,带界面的后台运行的也是mavgen.py这个脚本。下面是官方的介绍,之后我结合实际演示怎么生成mavlink协议。
mavgen.py is a command-line tool for generating MAVLink libraries for different programming languages. After the
mavlink
directory has been added to thePYTHONPATH
, it can be run by executing from the command line.This is the backend used by mavgenerate. The documentation below explains all the options for both tools.
For example, to generate MAVLink 2 C libraries for a dialect named your_custom_dialect.xml.
python -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 with the
-h
flag (reproduced below):usage: mavgen.py [-h] [-o OUTPUT] [--lang {C,CS,JavaScript,Python,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,Python,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.
生成mavlink协议
1.下载mavlink代码库
在home文件夹下新建src文件夹(这是我的习惯,所有的源码放在src文件夹下),然后再src文件夹下打开终端,再终端中输入
git clone git@github.com:mavlink/mavlink.git #下载mavlink代码库。
cd mavlink
git submodule update --init --recursive #初始化并更新子摸快
在~/.profile文件最后新起一行加入
export PATH=/home/“你的用户名”/src/mavlink/pymavlink/generator:$PATH
然后执行
source ~/.profile
2.生成自定义mavlink协议
还在1中的终端里进入QGC的mavlink子模块中
cd ~/src/qgroundcontrol/libs/mavlink/include/mavlink/v2.0/message_definitions
打开ardupilotmega.xml文件,里面就是mavlink的定义,按照里面既有的格式添加消息包,注意不能和已有的重复。假设添加228号包
<!-- ardupilot specific mavlink messages -->
<message name="PAYLOAD_STATUS" id="228">
<description>payload status of this mav</description>
<field type="uint32_t" name="system_time_ms">FMU system time in millisecond</field>
<field type="uint8_t" name="payload_type">payload status of this message indicate</field>
<field type="uint8_t" name="payload_status0">payload_status_byte0</field>
<field type="uint8_t" name="payload_status1">payload_status_byte1</field>
<field type="uint8_t" name="payload_status2">payload_status_byte2</field>
<field type="uint8_t" name="payload_status3">payload_status_byte3</field>
<field type="uint8_t" name="payload_status4">payload_status_byte4</field>
<field type="uint8_t" name="payload_status5">payload_status_byte5</field>
<field type="uint8_t" name="payload_status6">payload_status_byte6</field>
<field type="uint8_t" name="payload_status7">payload_status_byte7</field>
</message>
保存后,运行工具生成mavlink消息包
baiyang@baiyang-ThinkPad-E450:~/src/qgroundcontrol/libs/mavlink/include/mavlink/v2.0/message_definitions$ mavgen.py --lang=C --wire-protocol=2.0
--output=./../ ardupilotmega.xml
运行成功后,在上一目录中的ardupilotmega文件夹中可以找到mavlink_msg_payload_status.h文件。