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
}