mavlink 自定义消息编程
紧接着上一篇的操作方法,根据自定义的xml生成基于mavlink协议形式的消息。将其添加到工程中,并且调用起来的示例demo代码如下:
#include <iostream>
#include <mavlink.h>
#include <string>
using namespace std;int main(int argc , char * argv[]) {std::cout << "Hello mavlink !" << std::endl;uint8_t system_id = 1;uint8_t component_id = 1;mavlink_message_t msg;uint8_t buffer[MAVLINK_MAX_PACKET_LEN] = {};mavlink_upload_route_t packet_in = {963497464,963497672,963497880,1,1};mavlink_upload_route_t packet1, packet2;memset(&packet1, 0, sizeof(packet1));packet1.lon = packet_in.lon;packet1.lat = packet_in.lat;packet1.alt = packet_in.alt;packet1.route_id = packet_in.route_id;packet1.point_id = packet_in.point_id;memset(&packet2, 0, sizeof(packet2));mavlink_msg_upload_route_encode(system_id, component_id, &msg, &packet1);mavlink_msg_upload_route_decode(&msg, &packet2);MAVLINK_ASSERT(memcmp(&packet1, &packet2, sizeof(packet1)) == 0);cout << "route_id:" << to_string(packet2.route_id) << " point_id:" << to_string(packet2.point_id) << " lon:" << packet2.lon << " lat:" << packet2.lat << " alt:" << packet2.alt << endl;mavlink_msg_to_send_buffer(buffer, &msg);for (int j = 0; j < MAVLINK_MAX_PACKET_LEN; ++j) {printf("%x ", buffer[j]);if(j!=0 && j%100 == 0){putchar(10);}}putchar(10);return 0;
}
- mavlink协议帧