一 . 环境配置
参考: 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目录下打开终端
- 卸载之前安装的默认版本(若有)
pip uninstall pymavlink
- 安装依赖项:
python -m pip install -r requirements.txt
- 编译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/