【无人机学习之DroidPlanner】msg_heartbeat心跳处理

█ 【无人机学习之DroidPlanner】msg_heartbeat心跳处理


█ 系列文章目录

提示:这里是收集了无人机的相关文章


█ 文章目录


█ 读前说明

  • 本文通过学习别人写demo,学习一些课件,参考一些博客,学习相关知识,如有涉及侵权请告知
  • 本文可能只简单罗列了一些相关的代码实现过程,复制了一些大神的高论,如内容有误请自行辨别
  • 涉及到的逻辑以及说明可能只做了简单的介绍,主要当做笔记,了解过程而已,如有不同看法,欢迎下方评论
  • 本文源码:https://github.com/DroidPlanner/Tower


█ MAVLink

1.简介

  • 官网:MAVLink
  • MAVLink是一种非常轻量级的通信协议,主要是用于无人机上,还有无人车等;
  • 目前MAVLink有两个版本:V1.0(2013年发布)和V2.0版本(2017年发布);
  • V2是V1的拓展版本,简化传输,向下兼容V1.0,是一个更加安全和可扩展的协议;
  • MAVLink通信内容包含常见通信协议帧头、帧尾、长度、校验等。
  • MAVLink目前最新版本是 V 2.3;

2.协议格式

官网:mavlink协议格式
比较重要的字段是msgid和payload,msgid用来区分消息类型,判断需要转换成什么类型的对话,payload是对应的值

在这里插入图片描述

在这里插入图片描述

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 payload[max 255];   ///< A maximum of 255 payload bytes
uint16_t checksum;          ///< CRC-16/MCRF4XX
uint8_t signature[13];      ///< Signature which allows ensuring that the link is tamper-proof (optional)
索引C 版内容说明
STX0uint8_t magic起始标志位0xFD表示新数据包的开始,固定以“FD”开头
LEN1uint8_t len载荷长度0 - 255有效载荷数据的字节长度(N)
INC FLAGS2uint8_t incompat_flags不兼容标志不了解标志,则实现将丢弃数据包
CMP FLAGS3uint8_t compat_flags兼容性标志不理解该标志,仍可以处理数据包
SEQ4uint8_t seq包序号0 - 255用于检测数据包丢失,每发送完一个消息,加1
SYS ID5uint8_t sysid系统ID(发送者)1 - 255发送消息的系统(飞机/车辆)的ID
COMP ID6uint8_t compid部件 ID (发送者)1 - 255发送消息的部件的ID 。用来区分在系统中的不同组件,如自动驾驶仪和照相机
MSG ID 7 to 9uint32_t msgid:24 消息ID(低,中,高字节) 0 - 16777215 有效载荷中消息类型的ID 。用于将数据解码回消息对象。
PLAYLOAD10 to (n+10)uint8_t payload[max 255] 载荷 消息数据。取决于消息类型(即消息ID)和内容。
CHECKSUM(n+10) to (n+11)uint16_t checksum冗余校验/校验和(低字节,高字节)消息的CRC-16 / MCRF4XX(不包括magic字节),包括CRC_EXTRA字节
SIGNATURE(n+12) to (n+25)uint8_t signature[13]签名(可选)签名以确保链接是防篡改的。

3.使用Python写的用于生成C、Java等语言的MavLink生成器软件:

█ 心跳包的消息格式

1.官网的描述

在这里插入图片描述

2.心跳包的消息格式

在心跳包的消息类msg_heartbeat.java中可以看出msgid=0,因此当收到msgid=0即可找到msg_heartbeat通过unpack将 payload 转换成msg_heartbeat对象,心跳包是时刻不断的发送消息的,频率为1Hz。

<mavlink>
     <version>3</version>
     <dialect>0</dialect>
     <enums>。。。。。。</enums>
     <messages>
          <message id="0" name="HEARTBEAT">
               <description>The heartbeat message shows that a system or component is present and responding. The type and autopilot fields (along with the message component id), allow the receiving system to treat further messages from this system appropriately (e.g. by laying out the user interface based on the autopilot). This microservice is documented at https://mavlink.io/en/services/heartbeat.html</description>
               <field type="uint8_t" name="type" enum="MAV_TYPE">Vehicle or component type. For a flight controller component the vehicle type (quadrotor, helicopter, etc.). For other components the component type (e.g. camera, gimbal, etc.). This should be used in preference to component id for identifying the component type.</field>
               <field type="uint8_t" name="autopilot" enum="MAV_AUTOPILOT">Autopilot type / class. Use MAV_AUTOPILOT_INVALID for components that are not flight controllers.</field>
               <field type="uint8_t" name="base_mode" enum="MAV_MODE_FLAG" display="bitmask">System mode bitmap.</field>
               <field type="uint32_t" name="custom_mode">A bitfield for use for autopilot-specific flags</field>
               <field type="uint8_t" name="system_status" enum="MAV_STATE">System status flag.</field>
               <field type="uint8_t_mavlink_version" name="mavlink_version">MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version</field>
          </message>
          。。。。。。
     </messages>
</mavlink>

在这里插入图片描述
在这里插入图片描述
也可以将对应的信息打印出来:

E/org.droidplanner.services.android.impl.core.MAVLink.MavLinkMsgHandler - handleHeartbeat(msg_heartbeat heartbeat): MAVLINK_MSG_ID_HEARTBEAT - sysid:1 compid:1 msgid:0 custom_mode:2 type:3 autopilot:3 base_mode:81 system_status:3 mavlink_version:3
  • 1.autopilot=3(MAV_AUTOPILOT.MAV_AUTOPILOT_ARDUPILOTMEGA):表示ArduPilot - Plane/Copter/Rover/Sub/Tracker(飞机/直升机/探测器/子/跟踪器

  • 2.type=3(MAV_TYPE.MAV_TYPE_COAXIAL):表示Coaxial helicopter(同轴直升机 ),使用的硬件 FirmwareType.ARDU_COPTER(APM 硬件)

    /* APM firmware types */
    ARDU_PLANE(MAV_AUTOPILOT.MAV_AUTOPILOT_ARDUPILOTMEGA, "ArduPlane", "ArduPlane"),
    ARDU_COPTER(MAV_AUTOPILOT.MAV_AUTOPILOT_ARDUPILOTMEGA, "ArduCopter2", "ArduCopter"),// 使用的硬件
    ARDU_ROVER(MAV_AUTOPILOT.MAV_AUTOPILOT_ARDUPILOTMEGA, "ArduRover", "ArduRover"),
    ARDU_SOLO(MAV_AUTOPILOT.MAV_AUTOPILOT_ARDUPILOTMEGA, "ArduCopter2", "ArduSolo"),

    /**
     * PX4 firmware type
     */
    PX4_NATIVE(MAV_AUTOPILOT.MAV_AUTOPILOT_PX4, "", "PX4 Native"),

    /**
     * Generic firmware type
     */
    GENERIC(MAV_AUTOPILOT.MAV_AUTOPILOT_GENERIC, "", "Generic");

3.msg_heartbeat.java源代码

此文件在Mavlink\src\com\MAVLink\common\msg_heartbeat.java中

public abstract class MAVLinkMessage implements Serializable {
    private static final long serialVersionUID = -7754622750478538539L;
    // The MAVLink message classes have been changed to implement Serializable, 
    // this way is possible to pass a mavlink message trought the Service-Acctivity interface
    
    /**
     *  Simply a common interface for all MAVLink Messages
     */
    
    public int sysid;
    public int compid;
    public int msgid;
    public abstract MAVLinkPacket pack();
    public abstract void unpack(MAVLinkPayload payload);

}
/**
* The heartbeat message shows that a system or component is present and responding. The type and autopilot fields (along with the message component id), allow the receiving system to treat further messages from this system appropriately (e.g. by laying out the user interface based on the autopilot). This microservice is documented at https://mavlink.io/en/services/heartbeat.html
*/
public class msg_heartbeat extends MAVLinkMessage{

    public static final int MAVLINK_MSG_ID_HEARTBEAT = 0;
    public static final int MAVLINK_MSG_LENGTH = 9;
    private static final long serialVersionUID = MAVLINK_MSG_ID_HEARTBEAT;


      
    /**
    * A bitfield for use for autopilot-specific flags
    */
    public long custom_mode;
      
    /**
    * Vehicle or component type. For a flight controller component the vehicle type (quadrotor, helicopter, etc.). For other components the component type (e.g. camera, gimbal, etc.). This should be used in preference to component id for identifying the component type.
    */
    public short type;
      
    /**
    * Autopilot type / class. Use MAV_AUTOPILOT_INVALID for components that are not flight controllers.
    */
    public short autopilot;
      
    /**
    * System mode bitmap.
    */
    public short base_mode;
      
    /**
    * System status flag.
    */
    public short system_status;
      
    /**
    * MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version
    */
    public short mavlink_version;// MAVLink 版本,MAVLink有两个版本:V1和V2版本,V2是V1的拓展版本,是一个更加安全和可扩展的协议。
    

    /**
    * 为这种类型的消息生成mavlink消息的有效负载,组包数据
    * Generates the payload for a mavlink message for a message of this type
    * @return
    */
    public MAVLinkPacket pack(){
        MAVLinkPacket packet = new MAVLinkPacket(MAVLINK_MSG_LENGTH);
        packet.sysid = 255;
        packet.compid = 190;
        packet.msgid = MAVLINK_MSG_ID_HEARTBEAT;
              
        packet.payload.putUnsignedInt(custom_mode);
              
        packet.payload.putUnsignedByte(type);
              
        packet.payload.putUnsignedByte(autopilot);
              
        packet.payload.putUnsignedByte(base_mode);
              
        packet.payload.putUnsignedByte(system_status);
              
        packet.payload.putUnsignedByte(mavlink_version);
        
        return packet;
    }

    /**
    * 将心跳信息解码到该类字段中
    * Decode a heartbeat message into this class fields
    *
    * @param payload The message to decode
    */
    public void unpack(MAVLinkPayload payload) {
        payload.resetIndex();
              
        this.custom_mode = payload.getUnsignedInt();
              
        this.type = payload.getUnsignedByte();
              
        this.autopilot = payload.getUnsignedByte();
              
        this.base_mode = payload.getUnsignedByte();
              
        this.system_status = payload.getUnsignedByte();
              
        this.mavlink_version = payload.getUnsignedByte();
        
    }

    /**
    * 构造函数,只需初始化msgid
    * Constructor for a new message, just initializes the msgid
    */
    public msg_heartbeat(){
        msgid = MAVLINK_MSG_ID_HEARTBEAT;
    }

    /**
    * 构造函数,用有效负载初始化消息,从mavlink数据包
    * Constructor for a new message, initializes the message with the payload
    * from a mavlink packet
    *
    */
    public msg_heartbeat(MAVLinkPacket mavLinkPacket){
        this.sysid = mavLinkPacket.sysid;
        this.compid = mavLinkPacket.compid;
        this.msgid = MAVLINK_MSG_ID_HEARTBEAT;
        unpack(mavLinkPacket.payload);        
    }

}

4.大致过程

通过硬件接口(USB/蓝牙等)读取消息,通过msgid判断是msg_heartbeat类型,再通过msg_heartbeat自带的unpack方法解码MAVLinkPayload 信息成msg_heartbeat对象

5.待续

█ 相关资料

提示:这里是参考的相关文章

  1. mavlink官网:MAVLink
  2. pixhawk官网:Mavlink 消息简介 - pixhawk
  3. 2020-10-06 ardupilot 如何为android 增加mavlink协议_陌城烟雨-CSDN博客
  4. 打造自己的HelloDrone 无人机APP过程《2》_陌城烟雨-CSDN博客_drone无人机怎么下app
  5. 2015-04-02 Mavlink协议理解Pixhawk APM(一)_super_mice的专栏-CSDN博客
  6. 2018-12-24 无人机中级篇:第十四讲:外部通信总线Mavlink服务 - 知乎
  7. 2019-02-15 MAVLINK 请求参数和接收参数_小马哔哔-CSDN博客

█ 免责声明

博主分享的所有文章内容,部分参考网上教程,引用大神高论,部分亲身实践,记下笔录,内容可能存在诸多不实之处,还望海涵,本内容仅供学习研究使用,切勿用于商业用途,若您是部分内容的作者,不喜欢此内容被分享出来,可联系博主说明相关情况通知删除,感谢您的理解与支持!

提示:转载请注明出处:
https://blog.csdn.net/ljb568838953/article/details/112827387

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值