MAVLink

      MAVLink 是一种用于飞行器上的轻量级成熟通信协议,与很多自定的协议相比,它的开发难度非常小,有现成的配置工具,可直接生成MSG代码(简单来说,就是生成对应的函数接口供你直接调用即可),直接调用几个相应的函数接口即可,不需要自己封装、解析等;突出的优点在于配置调用比较简单


Pixhawk/APM都是采用MAVLINK协议实现的飞控的数据链路传输。先简单介绍下mavlink协议。Mavlink协议最早由 苏黎世联邦理工学院 计算机视觉与几何实验组 的  Lorenz Meier于2009年发布,并遵循LGPL开源协议。Mavlink协议是在串口通讯基础上的一种更高层的开源通讯协议,主要应用在微型飞行器(micro aerial vehicle)的通讯上。Mavlink是为小型飞行器和地面站(或者其他飞行器)通讯时常常用到的那些数据制定一种发送和接收的规则并加入了校验(checksum)功能。据说亿航四轴的初期版本就是参考的MAVLINK协议或者说参考的APM飞控系统。
下面是mavlink的消息简介和官方指导连接:

https://pixhawk.ethz.ch/mavlink/

http://qgroundcontrol.org/mavlink/start


 MAVLink 1 packet

MAVLink_第1张图片

 

Byte Index C version Content Value Explanation
0 uint8_t magic Packet start marker 0xFE Protocol-specific start-of-text (STX) marker used to indicate the beginning of a new packet. Any system that does not understand protocol version will skip the packet.
1 uint8_t len Payload length 0 - 255 Indicates length of the following payload section (fixed for a particular message).
2 uint8_t seq Packet sequence number 0 - 255 Used to detect packet loss. Components increment value for each message sent.
3 uint8_t sysid System ID 1 - 255 ID of system (vehicle) sending the message. Used to differentiate systems on network. Note that the broadcast address 0 may not be used in this field as it is an invalid source address.
4 uint8_t compid Component ID 1 - 255 ID of component sending the message. Used to differentiate components in a system (e.g. autopilot and a camera). Use appropriate values in MAV_COMPONENT. Note that the broadcast address MAV_COMP_ID_ALL may not be used in this field as it is an invalid source address.
5 uint8_t msgid Message ID 0 - 255 ID of message type in payload. Used to decode data back into message object.
6 to (n+6) uint8_t payload[max 255] Payload data   Message data. Content depends on message type (i.e. Message ID).
(n+7) to (n+8) uint16_t checksum Checksum (low byte, high byte)   X.25 CRC for message (excluding magic byte). Includes CRC_EXTRA byte.
  • The minimum packet length is 8 bytes for acknowledgment packets without payload.
  • The maximum packet length is 263 bytes for full payload.

MavlinkMessage的具体消息格式

MAVLink_第2张图片

STX-红色的是起始标志位(stx),在v1.0版本中以“FE”作为起始标志。这个标志位在mavlink消息帧接收端进行消息解码时有用处。
LEN-第二个格子代表的是灰色部分(payload,称作有效载荷,要用的数据在有效载荷里面)的字节长度(len),范围从0到255之间。在mavlink消息帧接收端可以用它和实际收到的有效载荷的长度比较,以验证有效载荷的长度是否正确。
SEQ-第三个格子代表的是本次消息帧的序号(seq),每次发完一个消息,这个字节的内容会加1,加到255后会从0重新开始。这个序号用于mavlink消息帧接收端计算消息丢失比例用的,相当于是信号强度。
SYS-第四个格子代表了发送本条消息帧的设备的系统编号(sys),使用PIXHAWK刷PX4固件时默认的系统编号为1,用于mavlink消息帧接收端识别是哪个设备发来的消息。
COMP-第五个格子代表了发送本条消息帧的设备的单元编号(comp),使用PIXHAWK刷PX4固件时默认的单元编号为50,用于mavlink消息帧接收端识别是设备的哪个单元发来的消息(暂时没什么用) 。
MSG-第六个格子代表了有效载荷中消息包的编号(msg),注意它和序号是不同的,这个字节很重要,mavlink消息帧接收端要根据这个编号来确定有效载荷里到底放了什么消息包并根据编号选择对应的方式来处理有效载荷里的信息包。
PAYLOAD-数据包
CKACKB-最后两个字节是16位校验位。


MAVLink的C源代码都是.h头文件。头文件只需要添加对应路径,包含头文件即可(#include "mavlink.h")

1

替换代码:#define MAVPACKED( __Declaration__ )       __Declaration__ 

为              #define MAVPACKED( __Declaration__ )      

2

#pragma   anon_unions

keil中默认是不支持匿名结构体的,需要编译指令#pragma anon_unions指名。

3

去掉  #include


    mavlink_msg_test_types_encode_chan(1, 2, 0, &gMavlinkMsgSnd, (const mavlink_test_types_t*)&gTestTypesSnd);
    mavlink_msg_to_send_buffer(gMavSndBuf, (const mavlink_message_t *)&gMavlinkMsgSnd);
   ------------------------------------------------------------------------


int8_t TestMAVLinkParse(uint8_t* rxBuf, uint16_t len)
{

    uint16_t i=0,res=0;
    for(i=0; i         res =  mavlink_parse_char(0, rxBuf[i], &gMavlinkMsgRcv, &gMavlinkStatus);
        if (res){
            printf("Received message with msgID %d, sequence: %d from component %d of system %d\r\n", 
                   gMavlinkMsgRcv.msgid, gMavlinkMsgRcv.seq, gMavlinkMsgRcv.compid, gMavlinkMsgRcv.sysid);
            return 0;
        }
    }   

}

 

你可能感兴趣的:(协议)