ardupilot开发 --- MavLink篇

简单的介绍

MAVLink是一种非常轻量级的消息传递协议,用于与无人机通信(以及在无人机机载组件之间)。
MAVLink遵循现代混合发布-订阅和点对点设计模式。
消息是在XML文件中定义的。每个XML文件定义特定MAVLink系统支持的消息集,也称为“dialect(方言)”。大多数地面控制站和自动驾驶仪实现的参考消息集在common.xml中定义(大多数dialect都建立在这个定义的基础上)。
代码生成器根据这些XML消息定义创建特定编程语言的软件库,然后无人机、地面控制站和其他MAVLink系统可以使用这些软件库进行通信。生成的库通常是MIT许可的,因此可以在任何闭源应用程序中无限制地使用,而无需发布闭源应用的源代码。

代码生成器

针对不同的编程语言生成mavlink库文件。

协议

协议1和协议2,向后兼容。

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)

MAVLink 1数据包格式类似,但省略了这些字段:compatt_flags、compat_flags和signature,并且只有一个字节用于消息地址。有关详细信息,请参阅序列化>数据包格式。

序列化

为了优化而重新排序Mavlink包的字段;

通信模式

topic 模式

发布、订阅;
节省链路带宽;
多个订户都可以接收这些数据;

Point-to-Point 模式

保证发送能够到达;
发送“任务、参数、命令”等一般用这个模式;

完整性检查

  • 完整性检查 CRC-16/MCRF4XX checksum;
  • 数据描述检查 CRC_EXTRA

使用者和开发者一些常见的疑问

https://mavlink.io/en/about/faq.html

都有哪些应用?

地面站

飞控固件

封装的API

  • MAVSDK - MAVLink API Library (C++, Python, Swift (iOS), Java, JS) that aims to be fully standards-compliant with MAVLink common microservices.
  • MAVLink Camera Manager - Extensible cross-platform MAVLink Camera Server (implements Camera Protocol) built on top of GStreamer and Rust-MAVLink.
  • Rosetta Drone - MAVLink wrapper around DJI SDK (fly a DJI drone with a Mavlink-based GCS, code: https://github.com/RosettaDrone/rosettadrone).
  • pymavlink - MAVLink python bindings.
  • MAVROS - ROS to MAVLink bridge.

These projects have some activity but are not as well maintained:

  • DroneKit - MAVLink API Library (Python, Android) and Log analysis tool (optimised for ArduPilot).
  • Dronecode Camera Manager - Adds Camera Protocol interface for cameras connected to Linux computer.

如何使用MavLink?

下载源码

如何include到自己的项目中

如何使用?

以C MAVLink库为例:

  • 解包 Parsing Packets 或称解码 decoding of a packet.
    mavlink_parse_char()函数
    其他函数?
    。。。。。。

自己的一些理解

文档
MavLink系统收发data,这data有两种形式message、conmmand;
conmmand本质上是一种特殊的enums;
什么时候发送message什么时候发送command?或者说什么时候将data定义成message什么时候定义成command?
所有的message和command都定义在XML文件中;
一些通用的message和command都定义在common.XML中,此外还有
若需要定义自己的message或command,如***.XML,这些文件被称为Dialect File,即方言(翻译自Dialect ),这些XML必须遵循这些格式:xml_schema;

如何定义和使用自己的message、conmmand、enums?需要注意哪些?

https://mavlink.io/en/guide/define_xml_element.html

MAVLINK Common Message Set

The MAVLink common message set contains standard definitions that are managed by the MAVLink project. The definitions cover functionality that is considered useful to most ground control stations and autopilots. MAVLink-compatible systems are expected to use these definitions where possible (if an appropriate message exists) rather than rolling out variants in their own dialects.
The original definitions are defined in common.xml.

可以查询所有通用的message、command、enums的定义和作用,有哪些参数,Id或value是几:
https://mavlink.io/en/messages/common.html

什么是MavLink微服务?有什么用?

MAVLink“微服务”定义了MAVLink系统可以采用的更高级别协议,以便更好地相互操作。例如,QGroundControl、ArduPilot和PX4自动驾驶都共享一个通用的命令协议,用于发送需要确认的点对点消息。
微服务用于交换多种类型的数据,包括:参数、任务、轨迹、图像和其他文件。如果数据可能远大于单个消息所能容纳的数据,那么服务将定义如何拆分和重新组装数据,以及如何确保任何丢失的数据被重新传输。其他服务提供命令确认和/或错误报告。
大多数服务使用客户端-服务器模式,因此地面军事系统(客户端)发起请求,车辆(服务器)用数据进行响应。
主要的微服务显示在侧边栏中(下面列出了大多数):
https://mavlink.io/en/services/

心跳包?

广播本大爷(MavLink系统)已经上线,可以进行数据收发。
心跳包数据包括:系统和组件id、车辆类型、飞行堆栈、组件类型和飞行模式。

message、conmmand、enums之间的关系

enums被用作message、conmmand中参数的取值(范围)

举个栗子:
枚举MAV_MODE的取值和描述如下:
ardupilot开发 --- MavLink篇_第1张图片
下面的command和massage就使用到了这个枚举:
ardupilot开发 --- MavLink篇_第2张图片
ardupilot开发 --- MavLink篇_第3张图片

conmmand被用作中参数的取值(范围)

下面的massage就用到了 command 和 enum :
ardupilot开发 --- MavLink篇_第4张图片

cammand 和 massage 都可以被单独发送和接收

什么时候用command?什么时候用massage??

  • commands 被 packaged 在特定的 messages 中进行使用,如COMMAND_INT、COMMAND_LONG 等massage中,一般是Mission Protocol and Command Protocol 微服务中;
    commands 必有7个参数;
    原文:These commands define the values of up to 7 parameters that are packaged INSIDE specific messages used in the Mission Protocol and Command Protocol.
  • 在 missions 中使用 command 否则使用 massage
    Use commands for actions in missions or if you need acknowledgment and/or retry logic from a request. Otherwise use messages.
  • 微服务,请参考:http://mavlink.io/zh/services/

微服务 Microservices

心跳包 Heartbeat/Connection Protocol

。。。。。。

Mission Protocol

本质上是基于现有的massage,按照以下约定来发送massage的一个过程:

  • 地面站发:
    ardupilot开发 --- MavLink篇_第5张图片
    地面站请求:
    ardupilot开发 --- MavLink篇_第6张图片

command protocol

ardupilot开发 --- MavLink篇_第7张图片

Parameter Protocol

  • Read All Parameters
    ardupilot开发 --- MavLink篇_第8张图片
  • Read Single Parameter
  • Write Parameters
    ardupilot开发 --- MavLink篇_第9张图片

在地面站MAVProxy中测试MavLink massage发送

  • 打开测试环境,MAVProxy
    sim_vehicle.py -v ArduCopter --console --map
  • 测试通过发送massage来更改飞行模式
    module load message
    message COMMAND_LONG 0 0 176 0 1 6 0 0 0 0 0
    参看:https://ardupilot.org/dev/docs/mavlink-get-set-flightmode.html

编程时,根据具体需求,参看以下文档

【1】https://ardupilot.org/dev/docs/mavlink-commands.html
【2】http://mavlink.io/zh/services/

你可能感兴趣的:(笔记)