如何创建一个Mavlink Message(一)

来源:互联网 发布:3d打印 软件 编辑:程序博客网 时间:2024/05/29 02:13

一 创建一个新的MAVLink消息

MAVLink消息以XML格式定义,然后转换为C / C ++,C#或Python代码(存在多个生成器)。 这里用公共心跳包来解释添加消息的过程。请注意,心跳包是需要使用的唯一消息,所有其他消息是可选的。

1、在XLM文件中定义一个消息

下面是XML中一个消息的定义,是mavlink / message_definitions / common.xml文件的一部分。

<message id="0" name="HEARTBEAT">  <description>The heartbeat message shows that a system is present and responding. The type of the MAV and Autopilot hardware allow the receiving system to treat further messages from this system appropriate (e.g. by laying out the user interface based on the autopilot).</description>  <field type="uint8_t" name="type">Type of the MAV (quadrotor, helicopter, etc., up to 15 types, defined in MAV_TYPE ENUM)</field>  <field type="uint8_t" name="autopilot">Autopilot type / class. defined in MAV_CLASS ENUM</field>  <field type="uint8_t" name="base_mode">System mode bitfield, see MAV_MODE_FLAGS ENUM in mavlink/include/mavlink_types.h</field>  <field type="uint32_t" name="custom_mode">Navigation mode bitfield, see MAV_AUTOPILOT_CUSTOM_MODE ENUM for some examples. This field is autopilot-specific.</field>  <field type="uint8_t" name="system_status">System status flag, see MAV_STATUS ENUM</field>  <field type="uint8_t_mavlink_version" name="mavlink_version">MAVLink version</field></message>

1.1不同的XML部分编码:

a.每个消息由<message> </ message>封装;

b.id =“0”表示此消息的id /索引号为零。有效的数字范围为0到255,其中ids 150-240保留用于扩展(因此你需要新增个人消息可以使用索引号为150-240);

c.name =“HEARTBEAT”编码可读的名称。该名称仅在代码中使用,不传输。系统本身只引用了ID;

d.<description> </ description>是一个非常重要的,可选字段。此描述显示在用户界面和代码注释中。它应该包含所有信息(和超链接),以完全理解消息;

e.<field> </ field>对消息的一个字段进行编码。它类似于C-struct中的一个变量。字段可以是8,16,32和64位长度的整数(有符号和无符号)和单/双精度IEEE754浮点数;

f.type =“uint8_t”将此字段定义为8位大小的无符号整数。数组可以这样定义:type =“uint8_t [5]”用于大小为5的数组。类型uint8_t_mavlink_version是一种特殊类型:它编码保存当前协议版本的无符号8位数。此字段为只读字段,在传输期间由MAVLink自动填充。它允许接收器解码协议版本。

2、自定义消息文件

在你自己的自动驾驶仪中,您可能需要一些自己的自定义消息,并将它们组织成一个消息定义文件(如common.xml和message_definitions /下的其他XML文件)。 XML定义文件可能如下所示。 请注意version和include标记。 如果此文件与common.xml文件位于同一目录中,则该文件的内容将包含在最终生成的MAVLink代码中。 这可能是您想要组织自定义消息定义文件的方式。

<?xml version="1.0"?><mavlink>        <include>common.xml</include>        <!-- NOTE: If the included file already contains a version tag, remove the version tag here, else uncomment to enable. --><!--<version>3</version>--><enums></enums><messages><message id="150" name="RUDDER_RAW"><description>This message encodes all of the raw rudder sensor data from the USV.</description><field type="uint16_t" name="position">The raw data from the position sensor, generally a potentiometer.</field><field type="uint8_t" name="port_limit">Status of the rudder limit sensor, port side. 0 indicates off and 1 indicates that the limit is hit. If this sensor is inactive set to 0xFF.</field><field type="uint8_t" name="center_limit">Status of the rudder limit sensor, port side. 0 indicates off and 1 indicates that the limit is hit. If this sensor is inactive set to 0xFF.</field><field type="uint8_t" name="starboard_limit">Status of the rudder limit sensor, starboard side. 0 indicates off and 1 indicates that the limit is hit. If this sensor is inactive set to 0xFF.</field></message></messages></mavlink>

3、将XML编译为C / C ++或Python

在存储这个消息定义文件之后,它可以被编译成C代码。 此过程在本页描述:MAVLink生成器(C / C ++,Python),对于没有耐心的读者,命令将是:

git clone https://github.com/mavlink/mavlink mavlink-generatorcd mavlink-generatorpython generate.py
它将启动一个Python GUI,允许您选择适当的输入和输出文件/目录。 编译代码后,生成的C-struct如下所示:

#define MAVLINK_MSG_ID_HEARTBEAT 0 typedef struct __mavlink_heartbeat_t{ uint32_t custom_mode; ///< Navigation mode bitfield, see MAV_AUTOPILOT_CUSTOM_MODE ENUM for some examples. This field is autopilot-specific. 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_CLASS ENUM uint8_t base_mode; ///< System mode bitfield, see MAV_MODE_FLAGS ENUM in mavlink/include/mavlink_types.h uint8_t system_status; ///< System status flag, see MAV_STATUS ENUM uint8_t mavlink_version; ///< MAVLink version} mavlink_heartbeat_t;
此外,MAVLink生成序列化(打包)和反序列化(解包)消息的函数:

/** * @brief Pack a heartbeat message * @param system_id ID of this system * @param component_id ID of this component (e.g. 200 for IMU) * @param msg The MAVLink message to compress the data into * * @param type Type of the MAV (quadrotor, helicopter, etc., up to 15 types, defined in MAV_TYPE ENUM) * @param autopilot Autopilot type / class. defined in MAV_CLASS ENUM * @param base_mode System mode bitfield, see MAV_MODE_FLAGS ENUM in mavlink/include/mavlink_types.h * @param custom_mode Navigation mode bitfield, see MAV_AUTOPILOT_CUSTOM_MODE ENUM for some examples. This field is autopilot-specific. * @param system_status System status flag, see MAV_STATUS ENUM * @return length of the message in bytes (excluding serial stream start sign) */static inline uint16_t mavlink_msg_heartbeat_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,       uint8_t type, uint8_t autopilot, uint8_t base_mode, uint32_t custom_mode, uint8_t system_status)  /** * @brief Encode a heartbeat struct into a message * * @param system_id ID of this system * @param component_id ID of this component (e.g. 200 for IMU) * @param msg The MAVLink message to compress the data into * @param heartbeat C-struct to read the message contents from */static inline uint16_t mavlink_msg_heartbeat_encode(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, const mavlink_heartbeat_t* heartbeat)

当然还要解码消息的内容:

/** * @brief Decode a heartbeat message into a struct * * @param msg The message to decode * @param heartbeat C-struct to decode the message contents into */static inline void mavlink_msg_heartbeat_decode(const mavlink_message_t* msg, mavlink_heartbeat_t* heartbeat)

关于MAVLink新增消息,后期还会继续更新,欢迎大家关注。








0 0
原创粉丝点击