由于在公司需要使用这个,我就写一个文章用于入门级别
简单介绍
MAVSDK是PX4开源团队贡献的基于mavlink通信协议的用于无人机应用开发的SDK,其可以部署在Windows、Linux、Android等多种平台,并且支持多种语言如c/c++、python、Java等。
在官网你会看到不同语言的MAVSDK,对应新手来说可能有点懵,其实MAVSDK的核心是MAVSDK-C++,这个版本实现了包括接口驱动(串口、udp等)、mavlink通信、各种交互逻辑处理,并且创建了server,建立gRPC通信,其他不同的语言版本都是只实现了gRPC通信接口而已。
下图可能会更清晰一些:
MAVSDK包括了library和server,如果我们使用的是C++,那么可以不用server,如果是使用其他语言(如python等)必须使用server,用于跟各种语言的客户端建立gRPC通信。
除 C++ 之外的 MAVSDK 语言包装器使用 gRPC 连接到 MAVSDK C++ 核心。这个围绕 MAVSDK C++ 库的 gRPC 服务器称为 mavsdk_server(过去称为后端)。
环境配置
安装MAVSDK Library-使用安装包
官方提供了已经打包好的安装包,可以直接去MAVSDK仓库下载release版。
或者直接使用如下命令下载并安装:
wget https://github.com/mavlink/MAVSDK/releases/download/v1.0.0/mavsdk_1.0.0_ubuntu20.04_amd64.deb
sudo dpkg -i mavsdk_1.0.0_ubuntu20.04_amd64.deb
安装MAVSDK Library-源码
- 系统更新
sudo apt-get update
sudo apt-get install build-essential cmake git
$ pip3 install future
- 下载源码
git clone https://github.com/mavlink/MAVSDK.git
git submodule update --init --recursive
- 编译
cmake -DCMAKE_BUILD_TYPE=Debug -Bbuild/default -H.
cmake --build build/default -j8
在编译过程中,会需要下载一些依赖仓库,并且会放置到
MAVSDK/build/default/third_party/
文件夹下。
- 安装
sudo cmake --build build/default --target install
安装路径
完成安装后,动态链接库和头文件会安装在系统默认路径下。
链接库为:/usr/local/lib/
$ ls /usr/local/lib/
cmake libmavsdk.so libmavsdk.so.1 libmavsdk.so.1.4.0 python2.7 python3.8
头文件为:/usr/local/include
$ ls /usr/local/include/mavsdk/
connection_result.h geometry.h log_callback.h mavlink_include.h plugin_base.h server_component.h system.h
deprecated.h handle.h mavlink mavsdk.h plugins server_plugin_base.h
在使用时,需要添加环境变量,编辑~/.bashrc
文件,在末尾添加如下:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
官网地址: 简介 ·MAVLink 开发者指南
1.
ERROR: Exception:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
yield
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 561, in read
data = self._fp_read(amt) if not fp_closed else b""
^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 527, in _fp_read
return self._fp.read(amt) if amt is not None else self._fp.read()
^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 98, in read
data: bytes = self.__fp.read(amt)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 479, in read
s = self.fp.read(amt)
^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\socket.py", line 707, in readinto
return self._sock.recv_into(b)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1252, in recv_into
return self.read(nbytes, buffer)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1104, in read
return self._sslobj.read(len, buffer)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\base_command.py", line 180, in exc_logging_wrapper
status = run_func(*args)
^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\req_command.py", line 245, in wrapper
return func(self, options, args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\commands\install.py", line 377, in run
requirement_set = resolver.resolve(
^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 179, in resolve
self.factory.preparer.prepare_linked_requirements_more(reqs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 552, in prepare_linked_requirements_more
self._complete_partial_requirements(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 467, in _complete_partial_requirements
for link, (filepath, _) in batch_download:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\network\download.py", line 183, in __call__
for chunk in chunks:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\progress_bars.py", line 53, in _rich_progress_bar
for chunk in iterable:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\network\utils.py", line 63, in response_chunks
for chunk in response.raw.stream(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 622, in stream
data = self.read(amt=amt, decode_content=decode_content)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 560, in read
with self._error_catcher():
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 158, in __exit__
self.gen.throw(value)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 443, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
2.
python: can't open file 'D:\\mavlink\\mavlink-master\\mavgenerator.py': [Errno 2] No such file or directory
3. git submodule update --init --recursive`是一个用于初始化并更新 Git 子模块的命令,其中包含了三个参数:
1. update:
这个参数告诉 Git 更新子模块。如果不指定此参数,Git 将不会更新子模块,而只是确保它们处于正确的提交状态。
2. --init:
这个参数告诉 Git 初始化尚未初始化的子模块。如果你的仓库包含子模块,并且有些子模块尚未被初始化(通常是因为它们是在一个最新的版本中添加的),那么使用这个参数将初始化这些子模块。
3. --recursive:
这个参数告诉 Git 递归地初始化和更新所有的子模块。如果子模块本身包含了其他子模块,那么使用这个参数将确保所有子模块都被正确初始化和更新。
因此,git submodule update --init --recursive命令的作用是确保所有子模块都被正确地初始化和更新到仓库中的最新状态。
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 received
printf("\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