前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mavlink协议解析_jlink 串口

mavlink协议解析_jlink 串口

作者头像
全栈程序员站长
发布2022-08-03 14:13:25
3.3K0
发布2022-08-03 14:13:25
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议。是无人飞行器和地面站(Ground Control Station ,GCS)之间,以及无人飞行器之间通讯常用的协议。APM、PIXHAWK飞控,Mission Planner、QGroundControl地面站均使用了MAVLink协议进行通讯。

MAVLink源码下载地址(现已更新至v2.0):https://github.com/mavlink/qgroundcontrol

  用户手册:https://docs.qgroundcontrol.com/en/

https://mavlink.io/zh/(开发手册)

mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口

1、 common文件夹:原始的MAVLink消息,包括各种消息的头文件

    common.h:定义MAVLink各个消息包中用到的枚举类型,各个消息包对应的CRC—EXTRA值、LENGTH值,包含(include)各个消息包的头文件

    各个消息的头文件:1)定义消息内容对应的数据结构,2)打包、发送消息的便捷函数,3)消息包解析并获取各个参数

2、 autopilotmega,ASLUAV,pixhawk等文件夹:这些文件夹包含各个飞控自定义的MAVLink消息类型

3、 checksum.h:计算校验码的代码

4、 mavlink_conversions.h:dcm,欧拉角,四元数之间的转换

5、 mavlink_helper.h:提供各种便捷函数:

  1)将各个消息包补充完整并发送。将数据载荷加上消息帧的头部,如sysid和compid等,计算校验码,补充成为完整的mavlink消息包后发送;

  2)MAVLink消息包解析。

6、 mavlink_types.h:定义用到的各种基本结构体(如mavlink_message_t)、枚举类型(如 mavlink_param_union_t)

MAVLink消息包结构

  MAVLink传输时,以消息包作为基本单位,数据长度为8~263字节。消息数据包的结构如下:

mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口

注:校验(checksum)功能。加入MAVLINK_CRC_EXTRA,当两个通讯终端之间(飞行器和地面站,或飞行器和飞行器)使用不同版本的MAVLink协议时,双方计算得到的校验码会不同,则不同版本的MAVLink协议之间将无法通讯。MAVLINK_MESSAGE_CRCS中存储了每种消息包对应的MAVLINK_CRC_EXTRA。这个 MAVLINK_CRC_EXTRA在用python生成MAVLink代码时在common.h头文件中自动生成。

MAVLink数据包的结构在mavlink_types.h中用mavlink_message_t结构体定义:

mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口

MAVLINK Common Message Set

  MAVLink通用消息集可以在《MAVLLINK Common Message set specifications》文档中查看。这些消息定义了通用消息集,这是大多数地面控制站和自动驾驶仪实现的参考消息集,头文件包含在common文件夹中。

  分为两部分:MAVLink Type Enumerations(MAVLink类型枚举 )和MAVLink Messages(MAVLink消息包)。

MAVLink Type Enumerations

  MAVLink Type Enumerations在common.h文件中定义。如下,枚举变量定义了飞行器的类型MAV_AUTOPILOT。

mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口
代码语言:javascript
复制
typedef enum MAV_AUTOPILOT
  {
      MAV_AUTOPILOT_GENERIC=0, /* Generic autopilot, full support for everything | */
      MAV_AUTOPILOT_RESERVED=1, /* Reserved for future use. | */
      MAV_AUTOPILOT_SLUGS=2, /* SLUGS autopilot, http://slugsuav.soe.ucsc.edu | */
      MAV_AUTOPILOT_ARDUPILOTMEGA=3, /* ArduPilotMega / ArduCopter, http://diydrones.com | */
      MAV_AUTOPILOT_OPENPILOT=4, /* OpenPilot, http://openpilot.org | */
      MAV_AUTOPILOT_GENERIC_WAYPOINTS_ONLY=5, /* Generic autopilot only supporting simple waypoints | */
      MAV_AUTOPILOT_GENERIC_WAYPOINTS_AND_SIMPLE_NAVIGATION_ONLY=6, /* Generic autopilot supporting waypoints and other simple navigation commands | */
     MAV_AUTOPILOT_GENERIC_MISSION_FULL=7, /* Generic autopilot supporting the full mission command set | */
     MAV_AUTOPILOT_INVALID=8, /* No valid autopilot, e.g. a GCS or other MAVLink component | */
     MAV_AUTOPILOT_PPZ=9, /* PPZ UAV - http://nongnu.org/paparazzi | */
     MAV_AUTOPILOT_UDB=10, /* UAV Dev Board | */
     MAV_AUTOPILOT_FP=11, /* FlexiPilot | */
     MAV_AUTOPILOT_PX4=12, /* PX4 Autopilot - http://pixhawk.ethz.ch/px4/ | */
     MAV_AUTOPILOT_SMACCMPILOT=13, /* SMACCMPilot - http://smaccmpilot.org | */
     MAV_AUTOPILOT_AUTOQUAD=14, /* AutoQuad -- http://autoquad.org | */
     MAV_AUTOPILOT_ARMAZILA=15, /* Armazila -- http://armazila.com | */
     MAV_AUTOPILOT_AEROB=16, /* Aerob -- http://aerob.ru | */
     MAV_AUTOPILOT_ASLUAV=17, /* ASLUAV autopilot -- http://www.asl.ethz.ch | */
     MAV_AUTOPILOT_ENUM_END=18, /*  | */
 } MAV_AUTOPILOT;
mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口

心跳包的内容存放在payload数据载荷中。以心跳包为例:

mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口
代码语言:javascript
复制
typedef struct __mavlink_heartbeat_t
{
 uint32_t custom_mode; /*< A bitfield for use for autopilot-specific flags.*/
 uint8_t type; /*< Type of the MAV (quadrotor, helicopter, etc., up to 15 types, defined in MAV_TYPE ENUM)*/
 uint8_t autopilot; /*< Autopilot type / class. defined in MAV_AUTOPILOT ENUM*/
 uint8_t base_mode; /*< System mode bitfield, see MAV_MODE_FLAG ENUM in mavlink/include/mavlink_types.h*/
 uint8_t system_status; /*< System status flag, see MAV_STATE ENUM*/
 uint8_t mavlink_version; /*< MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version*/
} mavlink_heartbeat_t;

心跳包一般用来表明发出该消息的设备是否活跃(一般以1Hz发送),消息接收端会根据是否及时收到了心跳包来判断是否和消息发送端失去了联系。

  心跳包由6个数据成员组成,占用9个字节。

  1、 type:飞行器类型,表示了当前发消息的是什么飞行器,如四旋翼,直升机等。type的取值对应枚举类型MAV_TYPE(如四旋翼,对应数值2)。

  2、autopilot:飞控类型,如apm,Pixhawk等,发送心跳包的飞行器的飞控类型。autopilot的取枚举类型MAV_AUTOPILOT。

  3、base mode(基本模式):飞控现在所处的飞行模式,这个参数要看各个飞控自己的定义方式,会有不同的组合、计算方式。

  4、custom mode(用户模式):飞控现在所处的飞行模式,这个参数要看各个飞控自己的定义方式,会有不同的组合、计算方式。

  5、system status:系统状态,见MAV_STATE枚举变量。

  6、mavlink version:消息发送端的MAVLink版本。

  其余的消息也是类似的结构,基本消息的定义可以查看官方网页的说明(具体说明以各个飞控为准),也可查看各个消息包头文件的定义。

Mavlink 2.0协议:

MavLink协议目前网上可以找到的中文版资料只有1.0的。现在开发使用的固件版本号是1.7.3.串口抓的的包全部与1.0版本都不一致。爬到官方去看了下。有2.0的包结构。

uint8_t magic; ///< protocol magic marker

uint8_t len; ///< Length of payload

uint8_t incompat_flags; ///< flags that must be understood

uint8_t compat_flags; ///< flags that can be ignored if not understood

uint8_t seq; ///< Sequence of packet

uint8_t sysid; ///< ID of message sender system/aircraft

uint8_t compid; ///< ID of the message sender component

uint8_t msgid 0:7; ///< first 8 bits of the ID of the message

uint8_t msgid 8:15; ///< middle 8 bits of the ID of the message

uint8_t msgid 16:23; ///< last 8 bits of the ID of the message

uint8_t target_sysid; ///< Optional field for point-to-point messages, used for payload else

uint8_t target_compid; ///< Optional field for point-to-point messages, used for payload else

uint8_t payload[max 253]; ///< A maximum of 253 payload bytes

uint16_t checksum; ///< X.25 CRC

uint8_t signature[13]; ///< Signature which allows ensuring that the link is tamper-proof

包结构大致如下:

mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口

2.0包的总长为10+n+3(+2),其中括号的2为可选内容。抓包未发现。

抓包获得来自飞控的Msgind如下。

0,36,74,105,30,31,32,140,1,147,65,70,141,230,241,111,83,69,24

這些都是飞控无请求时主动发送过来的数据。

根据msgid:

mavlink协议解析_jlink 串口
mavlink协议解析_jlink 串口

mavlink协议源码: mavlink

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124668.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档