QGroundControl开发之使用自定义mavlink

    工具

    对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 the PYTHONPATH, 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文件。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值