搭建环境
git克隆
git clone https://github.com/mavlink/mavlink.git --recursive
安装python的future库
pip install future
使用可视化工具生成mavlink库
- XML是选择消息格式,也可以自定义
- Out是输出路径
- Language是生成的语言,我这里是C++
- Protocol是协议版本
点击generate生成对应的头文件。
测试
MavLinkLibrary
是刚才生成的mavlink库
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(MAVLINKTEST)
set(CMAKE_CXX_FLAGS "${CAMKE_CXX_FLAGS} -std=c++11 -pthread -g")
# 当前cmakelist文件所在的目录
message(STATUS "CMAKE_CURRENT_SOURCE_DIR: " ${CMAKE_CURRENT_SOURCE_DIR})
# 项目cmakelist文件所在的目录
message(STATUS "CMAKE_SOURCE_DIR: " ${CMAKE_SOURCE_DIR})# 添加include文件
set(MAVLINK_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/MavLinkLibrary)include_directories(${MAVLINK_INCLUDE_DIRS})add_executable(test test.cpp)
test.cpp
#include "MavLinkLibrary/common/mavlink.h"
#include "MavLinkLibrary/common/mavlink_msg_local_position_ned.h"
#include "MavLinkLibrary/common/mavlink_msg_sys_status.h"
#include<iostream>
using namespace std;
#define BUFFER_LENGTH \2041 // minimum buffer size that can be used with qnx (I don't know why)int main(int argc, char *argv[]) {mavlink_system_t mavlink_system = {1, // System ID (1-255)1 // Component ID (a MAV_COMPONENT value)};char help[] = "--help";char target_ip[100];float position[6] = {};uint8_t buf[BUFFER_LENGTH];ssize_t recsize;// socklen_t fromlen = sizeof(gcAddr);int bytes_sent;mavlink_message_t msg2;// mavlink_message_t msg;mavlink_status_t status;uint16_t len;int i = 0;unsigned int temp = 0;{mavlink_msg_local_position_ned_pack(1, 200, &msg2, 123, 2, 33, 35, 43, 52,62);len = mavlink_msg_to_send_buffer(buf, &msg2);if(len){std::cout<<"message send success!"<<endl;}for (i = 0; i < len; i++)printf("%x ", buf[i]);printf("\n--------------------------------------------------------\n");mavlink_message_t msg;for (i = 0; i < len; i++) {temp = buf[i];printf("%02x ", (unsigned char)temp);if (mavlink_parse_char(12, buf[i], &msg, &status)) {// Packet receivedprintf("\nReceived --packet: SYS: %d, COMP: %d, LEN: %d, MSG ID: %d\n",msg.sysid, msg.compid, msg.len, msg.msgid);if (msg.msgid == 32) {mavlink_local_position_ned_t local_position_ned;mavlink_msg_local_position_ned_decode(&msg, &local_position_ned);printf("ned x %f, y%f, z%f\nned vx %f, vy%f, vz%f\n",local_position_ned.x, local_position_ned.y,local_position_ned.z, local_position_ned.vx,local_position_ned.vy, local_position_ned.vz);}}}}
}
编译
mkdir build
cd build
cmake .. -G "MinGW Makefiles"
mingw32-make.exe