欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 【QGroundControl二次开发】五.python生成自定义MAVLink消息及使用

【QGroundControl二次开发】五.python生成自定义MAVLink消息及使用

2024/10/25 0:37:00 来源:https://blog.csdn.net/qq_45906972/article/details/140658083  浏览:    关键词:【QGroundControl二次开发】五.python生成自定义MAVLink消息及使用

一 . 环境配置

参考: MAVLink代码生成-C#

二. 生成MAVLINK协议

在MAVlink源码下找到message_definitions/common.xml,修改其中的内容。
例如:

<message id="12" name="DISTANCE_SENSOR"><description>Dedicated Raspberry PI Detects distance of the obstacle in front of the sensor</description><field type="uint64_t" name="time_usec" units="us">Timestamp (UNIX Epoch time or time since system boot). The receiving end can infer timestamp format (since 1.1.1970 or since system boot) by checking for the magnitude of the number.</field><field type="uint8_t" name="sensor_type" enum="MAV_DISTANCE_SENSOR">Class id of the distance sensor type.</field><field type="uint16_t[72]" name="distances" units="cm" invalid="[UINT16_MAX]">Distance of obstacles around the vehicle with index 0 corresponding to north + angle_offset, unless otherwise specified in the frame. A value of 0 is valid and means that the obstacle is practically touching the sensor. A value of max_distance +1 means no obstacle is present. A value of UINT16_MAX for unknown/not used. In a array element, one unit corresponds to 1cm.</field><field type="uint8_t" name="increment" units="deg">Angular width in degrees of each array element. Increment direction is clockwise. This field is ignored if increment_f is non-zero.</field><field type="uint16_t" name="min_distance" units="cm">Minimum distance the sensor can measure.</field><field type="uint16_t" name="max_distance" units="cm">Maximum distance the sensor can measure.</field><extensions/><field type="float" name="increment_f" units="deg">Angular width in degrees of each array element as a float. If non-zero then this value is used instead of the uint8_t increment field. Positive is clockwise direction, negative is counter-clockwise.</field><field type="float" name="angle_offset" units="deg">Relative angle offset of the 0-index element in the distances array. Value of 0 corresponds to forward. Positive is clockwise direction, negative is counter-clockwise.</field><field type="uint8_t" name="frame" enum="MAV_FRAME">Coordinate frame of reference for the yaw rotation and offset of the sensor data. Defaults to MAV_FRAME_GLOBAL, which is north aligned. For body-mounted sensors use MAV_FRAME_BODY_FRD, which is vehicle front aligned.</field></message>

使用mavgenerate编译成所需语言。
在MAVLink源码根目录下打开CMD,输入命令:

python -m mavgenerate

在这里插入图片描述

XMl: 选择刚才修改后的common.xml。
Out: 输出路径
点击Generate, 将弹出成功窗口。
在这里插入图片描述

生成的内容如下所示
在这里插入图片描述

三. 编译pyMAVLink

将刚才生成的文件放入MAVLink源码\pymavlink\dialects\v20 中,
如果是MAVLink v1则选择 放入v10中。
在这里插入图片描述
在pymavlink目录下打开终端

  1. 卸载之前安装的默认版本(若有)
pip uninstall pymavlink
  1. 安装依赖项:
python -m pip install -r requirements.txt
  1. 编译pymavlink:
python setup.py install --user

最后,编译后的包在 pymavlink\build\lib,复制放到python环境下的site-package中。
在这里插入图片描述
测试:
在这里插入图片描述

四. pyMAVLink的使用

4.1 建立连接

mavutil 模块提供了通过串行端口、tcp 或 udp 通道建立与 MAVLink 系统的通信链路的方法。它还可以连接到文件对象,这在处理遥测日志时非常有用。

示例:

from pymavlink import mavutilmaster = mavutil.mavlink_connection('udp:0.0.0.0:{}'.format(port))# port 是端口号
master.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (master.target_system, master.target_system))param = 'PSC_POSXY_P'
master.param_fetch_one(param) # need connect the uav at first!message = master.recv_match(type='PARAM_VALUE', blocking=True).to_dict()
print('name: %s\t value: %f' % (message['param_id'], message['param_value']))

4.2 设置飞行模式

mavutil同样可以直接改变UAV的飞行模式

设置为loiter

master.set_mode_loiter()

解锁 uav

master.arducopter_arm()

设置为auto模式

master.set_mode_auto()

4.3 读写参数

mavutil读取无人机的参数.

master.param_fetch_all() # read all param| need connect the uav at first!
master.param_fetch_one(name) # read one param | need connect the uav at first!

mavutil直接设置参数的函数.

master.param_set_send(param, value) 

4.4 示例

接收无人机的ATTITUDE姿态消息

from pymavlink import mavutilmaster = mavutil.mavlink_connection('udp:0.0.0.0:{}'.format(port))# port 是端口号
master.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (master.target_system, master.target_system))while True:try:msg = master.recv_match(type='ATTITUDE', blocking=True)if not msg:raise ValueError()print(msg.to_dict())except KeyboardInterrupt:print('Key bordInterrupt! exit')break

4.5 文档

https://mavlink.io/en/mavgen_python/

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com