6002.mavlink通過xml生成自定義消息

MAVLink通過xml文件,配置信息(MSG)

 MAVLink通過xml文件,配置信息(MSG),這個MSG可以理解成我們需要接收或者發送的數據變量,簡直不要太方便了,下面我們一起來試試吧.

1 自定義符合標準xml文件

<?xml version="1.0"?>                                                                                                                                                                                   
    <mavlink>
     <version>0</version>
     <messages>
       <message id="0" name="upload_route">
         <description>這是一個上傳航線消息測試 </description>
         <field type="uint8_t" name="route_id">航線id</field>
         <field type="uint8_t" name="point_id">航點id</field>
         <field type="double" name="lon">經度</field>
         <field type="double" name="lat">緯度</field>
         <field type="double" name="alt">高度</field>
       </message>
    </messages>
  </mavlink>
<!--有一點需要注意點 類型,名稱,嚴格影響生成程序的校驗碼 -->

2 調用生成代碼指令

 python3 -m mavgenerate

在這裏插入圖片描述

3 生成c++操作代碼

在這裏插入圖片描述
在這裏插入圖片描述
mavlink_msg_upload_route.h 內容如下

#pragma once
// MESSAGE upload_route PACKING

#define MAVLINK_MSG_ID_upload_route 0

MAVPACKED(
typedef struct __mavlink_upload_route_t {
 double lon; /*<  經度*/
 double lat; /*<  緯度*/
 double alt; /*<  高度*/
 uint8_t route_id; /*<  航線id*/
 uint8_t point_id; /*<  航點id*/
}) mavlink_upload_route_t;

#define MAVLINK_MSG_ID_upload_route_LEN 26
#define MAVLINK_MSG_ID_upload_route_MIN_LEN 26
#define MAVLINK_MSG_ID_0_LEN 26
#define MAVLINK_MSG_ID_0_MIN_LEN 26

#define MAVLINK_MSG_ID_upload_route_CRC 69
#define MAVLINK_MSG_ID_0_CRC 69



#if MAVLINK_COMMAND_24BIT
#define MAVLINK_MESSAGE_INFO_upload_route { \
    0, \
    "upload_route", \
    5, \
    {  { "route_id", NULL, MAVLINK_TYPE_UINT8_T, 0, 24, offsetof(mavlink_upload_route_t, route_id) }, \
         { "point_id", NULL, MAVLINK_TYPE_UINT8_T, 0, 25, offsetof(mavlink_upload_route_t, point_id) }, \
         { "lon", NULL, MAVLINK_TYPE_DOUBLE, 0, 0, offsetof(mavlink_upload_route_t, lon) }, \
         { "lat", NULL, MAVLINK_TYPE_DOUBLE, 0, 8, offsetof(mavlink_upload_route_t, lat) }, \
         { "alt", NULL, MAVLINK_TYPE_DOUBLE, 0, 16, offsetof(mavlink_upload_route_t, alt) }, \
         } \
}
#else
#define MAVLINK_MESSAGE_INFO_upload_route { \
    "upload_route", \
    5, \
    {  { "route_id", NULL, MAVLINK_TYPE_UINT8_T, 0, 24, offsetof(mavlink_upload_route_t, route_id) }, \
         { "point_id", NULL, MAVLINK_TYPE_UINT8_T, 0, 25, offsetof(mavlink_upload_route_t, point_id) }, \
         { "lon", NULL, MAVLINK_TYPE_DOUBLE, 0, 0, offsetof(mavlink_upload_route_t, lon) }, \
         { "lat", NULL, MAVLINK_TYPE_DOUBLE, 0, 8, offsetof(mavlink_upload_route_t, lat) }, \
         { "alt", NULL, MAVLINK_TYPE_DOUBLE, 0, 16, offsetof(mavlink_upload_route_t, alt) }, \
         } \
}
#endif

/**
 * @brief Pack a upload_route 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 route_id  航線id
 * @param point_id  航點id
 * @param lon  經度
 * @param lat  緯度
 * @param alt  高度
 * @return length of the message in bytes (excluding serial stream start sign)
 */
static inline uint16_t mavlink_msg_upload_route_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,
                               uint8_t route_id, uint8_t point_id, double lon, double lat, double alt)
{
#if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
    char buf[MAVLINK_MSG_ID_upload_route_LEN];
    _mav_put_double(buf, 0, lon);
    _mav_put_double(buf, 8, lat);
    _mav_put_double(buf, 16, alt);
    _mav_put_uint8_t(buf, 24, route_id);
    _mav_put_uint8_t(buf, 25, point_id);

        memcpy(_MAV_PAYLOAD_NON_CONST(msg), buf, MAVLINK_MSG_ID_upload_route_LEN);
#else
    mavlink_upload_route_t packet;
    packet.lon = lon;
    packet.lat = lat;
    packet.alt = alt;
    packet.route_id = route_id;
    packet.point_id = point_id;

        memcpy(_MAV_PAYLOAD_NON_CONST(msg), &packet, MAVLINK_MSG_ID_upload_route_LEN);
#endif

    msg->msgid = MAVLINK_MSG_ID_upload_route;
    return mavlink_finalize_message(msg, system_id, component_id, MAVLINK_MSG_ID_upload_route_MIN_LEN, MAVLINK_MSG_ID_upload_route_LEN, MAVLINK_MSG_ID_upload_route_CRC);
}

/**
 * @brief Pack a upload_route message on a channel
 * @param system_id ID of this system
 * @param component_id ID of this component (e.g. 200 for IMU)
 * @param chan The MAVLink channel this message will be sent over
 * @param msg The MAVLink message to compress the data into
 * @param route_id  航線id
 * @param point_id  航點id
 * @param lon  經度
 * @param lat  緯度
 * @param alt  高度
 * @return length of the message in bytes (excluding serial stream start sign)
 */
static inline uint16_t mavlink_msg_upload_route_pack_chan(uint8_t system_id, uint8_t component_id, uint8_t chan,
                               mavlink_message_t* msg,
                                   uint8_t route_id,uint8_t point_id,double lon,double lat,double alt)
{
#if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
    char buf[MAVLINK_MSG_ID_upload_route_LEN];
    _mav_put_double(buf, 0, lon);
    _mav_put_double(buf, 8, lat);
    _mav_put_double(buf, 16, alt);
    _mav_put_uint8_t(buf, 24, route_id);
    _mav_put_uint8_t(buf, 25, point_id);

        memcpy(_MAV_PAYLOAD_NON_CONST(msg), buf, MAVLINK_MSG_ID_upload_route_LEN);
#else
    mavlink_upload_route_t packet;
    packet.lon = lon;
    packet.lat = lat;
    packet.alt = alt;
    packet.route_id = route_id;
    packet.point_id = point_id;

        memcpy(_MAV_PAYLOAD_NON_CONST(msg), &packet, MAVLINK_MSG_ID_upload_route_LEN);
#endif

    msg->msgid = MAVLINK_MSG_ID_upload_route;
    return mavlink_finalize_message_chan(msg, system_id, component_id, chan, MAVLINK_MSG_ID_upload_route_MIN_LEN, MAVLINK_MSG_ID_upload_route_LEN, MAVLINK_MSG_ID_upload_route_CRC);
}

/**
 * @brief Encode a upload_route struct
 *
 * @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 upload_route C-struct to read the message contents from
 */
static inline uint16_t mavlink_msg_upload_route_encode(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, const mavlink_upload_route_t* upload_route)
{
    return mavlink_msg_upload_route_pack(system_id, component_id, msg, upload_route->route_id, upload_route->point_id, upload_route->lon, upload_route->lat, upload_route->alt);
}

/**
 * @brief Encode a upload_route struct on a channel
 *
 * @param system_id ID of this system
 * @param component_id ID of this component (e.g. 200 for IMU)
 * @param chan The MAVLink channel this message will be sent over
 * @param msg The MAVLink message to compress the data into
 * @param upload_route C-struct to read the message contents from
 */
static inline uint16_t mavlink_msg_upload_route_encode_chan(uint8_t system_id, uint8_t component_id, uint8_t chan, mavlink_message_t* msg, const mavlink_upload_route_t* upload_route)
{
    return mavlink_msg_upload_route_pack_chan(system_id, component_id, chan, msg, upload_route->route_id, upload_route->point_id, upload_route->lon, upload_route->lat, upload_route->alt);
}

/**
 * @brief Send a upload_route message
 * @param chan MAVLink channel to send the message
 *
 * @param route_id  航線id
 * @param point_id  航點id
 * @param lon  經度
 * @param lat  緯度
 * @param alt  高度
 */
#ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS

static inline void mavlink_msg_upload_route_send(mavlink_channel_t chan, uint8_t route_id, uint8_t point_id, double lon, double lat, double alt)
{
#if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
    char buf[MAVLINK_MSG_ID_upload_route_LEN];
    _mav_put_double(buf, 0, lon);
    _mav_put_double(buf, 8, lat);
    _mav_put_double(buf, 16, alt);
    _mav_put_uint8_t(buf, 24, route_id);
    _mav_put_uint8_t(buf, 25, point_id);

    _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_upload_route, buf, MAVLINK_MSG_ID_upload_route_MIN_LEN, MAVLINK_MSG_ID_upload_route_LEN, MAVLINK_MSG_ID_upload_route_CRC);
#else
    mavlink_upload_route_t packet;
    packet.lon = lon;
    packet.lat = lat;
    packet.alt = alt;
    packet.route_id = route_id;
    packet.point_id = point_id;

    _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_upload_route, (const char *)&packet, MAVLINK_MSG_ID_upload_route_MIN_LEN, MAVLINK_MSG_ID_upload_route_LEN, MAVLINK_MSG_ID_upload_route_CRC);
#endif
}

/**
 * @brief Send a upload_route message
 * @param chan MAVLink channel to send the message
 * @param struct The MAVLink struct to serialize
 */
static inline void mavlink_msg_upload_route_send_struct(mavlink_channel_t chan, const mavlink_upload_route_t* upload_route)
{
#if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
    mavlink_msg_upload_route_send(chan, upload_route->route_id, upload_route->point_id, upload_route->lon, upload_route->lat, upload_route->alt);
#else
    _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_upload_route, (const char *)upload_route, MAVLINK_MSG_ID_upload_route_MIN_LEN, MAVLINK_MSG_ID_upload_route_LEN, MAVLINK_MSG_ID_upload_route_CRC);
#endif
}

#if MAVLINK_MSG_ID_upload_route_LEN <= MAVLINK_MAX_PAYLOAD_LEN
/*
  This varient of _send() can be used to save stack space by re-using
  memory from the receive buffer.  The caller provides a
  mavlink_message_t which is the size of a full mavlink message. This
  is usually the receive buffer for the channel, and allows a reply to an
  incoming message with minimum stack space usage.
 */
static inline void mavlink_msg_upload_route_send_buf(mavlink_message_t *msgbuf, mavlink_channel_t chan,  uint8_t route_id, uint8_t point_id, double lon, double lat, double alt)
{
#if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
    char *buf = (char *)msgbuf;
    _mav_put_double(buf, 0, lon);
    _mav_put_double(buf, 8, lat);
    _mav_put_double(buf, 16, alt);
    _mav_put_uint8_t(buf, 24, route_id);
    _mav_put_uint8_t(buf, 25, point_id);

    _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_upload_route, buf, MAVLINK_MSG_ID_upload_route_MIN_LEN, MAVLINK_MSG_ID_upload_route_LEN, MAVLINK_MSG_ID_upload_route_CRC);
#else
    mavlink_upload_route_t *packet = (mavlink_upload_route_t *)msgbuf;
    packet->lon = lon;
    packet->lat = lat;
    packet->alt = alt;
    packet->route_id = route_id;
    packet->point_id = point_id;

    _mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_upload_route, (const char *)packet, MAVLINK_MSG_ID_upload_route_MIN_LEN, MAVLINK_MSG_ID_upload_route_LEN, MAVLINK_MSG_ID_upload_route_CRC);
#endif
}
#endif

#endif

// MESSAGE upload_route UNPACKING


/**
 * @brief Get field route_id from upload_route message
 *
 * @return  航線id
 */
static inline uint8_t mavlink_msg_upload_route_get_route_id(const mavlink_message_t* msg)
{
    return _MAV_RETURN_uint8_t(msg,  24);
}

/**
 * @brief Get field point_id from upload_route message
 *
 * @return  航點id
 */
static inline uint8_t mavlink_msg_upload_route_get_point_id(const mavlink_message_t* msg)
{
    return _MAV_RETURN_uint8_t(msg,  25);
}

/**
 * @brief Get field lon from upload_route message
 *
 * @return  經度
 */
static inline double mavlink_msg_upload_route_get_lon(const mavlink_message_t* msg)
{
    return _MAV_RETURN_double(msg,  0);
}

/**
 * @brief Get field lat from upload_route message
 *
 * @return  緯度
 */
static inline double mavlink_msg_upload_route_get_lat(const mavlink_message_t* msg)
{
    return _MAV_RETURN_double(msg,  8);
}

/**
 * @brief Get field alt from upload_route message
 *
 * @return  高度
 */
static inline double mavlink_msg_upload_route_get_alt(const mavlink_message_t* msg)
{
    return _MAV_RETURN_double(msg,  16);
}

/**
 * @brief Decode a upload_route message into a struct
 *
 * @param msg The message to decode
 * @param upload_route C-struct to decode the message contents into
 */
static inline void mavlink_msg_upload_route_decode(const mavlink_message_t* msg, mavlink_upload_route_t* upload_route)
{
#if MAVLINK_NEED_BYTE_SWAP || !MAVLINK_ALIGNED_FIELDS
    upload_route->lon = mavlink_msg_upload_route_get_lon(msg);
    upload_route->lat = mavlink_msg_upload_route_get_lat(msg);
    upload_route->alt = mavlink_msg_upload_route_get_alt(msg);
    upload_route->route_id = mavlink_msg_upload_route_get_route_id(msg);
    upload_route->point_id = mavlink_msg_upload_route_get_point_id(msg);
#else
        uint8_t len = msg->len < MAVLINK_MSG_ID_upload_route_LEN? msg->len : MAVLINK_MSG_ID_upload_route_LEN;
        memset(upload_route, 0, MAVLINK_MSG_ID_upload_route_LEN);
    memcpy(upload_route, _MAV_PAYLOAD(msg), len);
#endif
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章