由于在公司需要使用這個,我就寫一個文章用于入門級別
簡單介紹
????????
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_catcheryieldFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 561, in readdata = 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_readreturn 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 readdata: bytes = self.__fp.read(amt)^^^^^^^^^^^^^^^^^^^File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 479, in reads = self.fp.read(amt)^^^^^^^^^^^^^^^^^File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\socket.py", line 707, in readintoreturn self._sock.recv_into(b)^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1252, in recv_intoreturn self.read(nbytes, buffer)^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1104, in readreturn self._sslobj.read(len, buffer)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed outDuring 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_wrapperstatus = run_func(*args)^^^^^^^^^^^^^^^File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\req_command.py", line 245, in wrapperreturn func(self, options, args)^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\commands\install.py", line 377, in runrequirement_set = resolver.resolve(^^^^^^^^^^^^^^^^^File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 179, in resolveself.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_moreself._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_requirementsfor 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_barfor chunk in iterable:File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\network\utils.py", line 63, in response_chunksfor 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 streamdata = 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 readwith 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_catcherraise 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 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