MavLinK協議

由于在公司需要使用這個,我就寫一個文章用于入門級別

簡單介紹

????????

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庫

![在這里插入圖片描述](https://img-blog.csdnimg.cn/52a95488f3814779a255082c280ff121.png

  • 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
運行結果

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:
http://www.pswp.cn/diannao/13774.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/13774.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/13774.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

GIS讀研與求職準備:GNSS專業研0

本文介紹GIS方向研究生入學初期&#xff0c;為將來轉碼、從事開發類工作所作求職準備的規劃路徑、方向選擇等方面的建議。 最近&#xff0c;有很多師弟師妹詢問關于研究生方向選擇、求職準備、就業方向選擇等方面的問題。首先非常感謝大家的盲目信任&#xff08;開個玩笑&#…

基于 debian 12 利用 kubeadm 部署 k8s 1.29 版本

基于 debian 12 利用 kubeadm 部署 k8s 1.29 版本 預先準備 準備三臺debian 12的虛擬機&#xff0c;配置如下&#xff1a; HostnameIP配置k8s-master1192.168.31.604vCPU、8GiB 內存、50GiB 硬盤k8s-worker1192.168.31.614vCPU、8GiB 內存、50GiB 硬盤k8s-worker2192.168.31.6…

python從0開始學習(九)

前言 上一篇文章我們介紹了python中的序列類型和元組類型&#xff0c;本篇文章將接著往下將。 1、字典類型 字典類型是根據一個信息查找另一個信息的方式所構成的“鍵值對”&#xff0c;它表示索引用的鍵和對應的值構成的成對關系。它是一個可變數據類型&#xff0c;也就是說它…

Leetcode 3154. Find Number of Ways to Reach the K-th Stair

Leetcode 3154. Find Number of Ways to Reach the K-th Stair 1. 解題思路2. 代碼實現 題目鏈接&#xff1a;3154. Find Number of Ways to Reach the K-th Stair 1. 解題思路 這一題思路上就是一個動態規劃&#xff0c;我們只需要確定一下運行的終止條件&#xff0c;然后寫…

React中顯示數據

SX 會讓你把標簽放到 JavaScript 中。而大括號會讓你 “回到” JavaScript 中&#xff0c;這樣你就可以從你的代碼中嵌入一些變量并展示給用戶。例如&#xff0c;這將顯示 user.name&#xff1a; return (<h1>{user.name}</h1> ); 你還可以將 JSX 屬性 “轉義到 …

《web應用技術》第9次課后作業

一、將前面的代碼繼續完善功能 1、采用XML映射文件的形式來映射sql語句&#xff1b; 2、采用動態sql語句的方式&#xff0c;實現條件查詢的分頁。 二、學習git的使用。 1、每個小組將自己的項目上傳到gitee&#xff0c;學會協作開發&#xff1b; 2、學會從gitee上拉取項目…

【Text2SQL 經典模型】TypeSQL

論文&#xff1a;TypeSQL: Knowledge-Based Type-Aware Neural Text-to-SQL Generation ??? Code: TypeSQL | GitHub 一、論文速讀 本論文是在 SQLNet 網絡上做的改進&#xff0c;其思路也是先預先構建一個 SQL sketch&#xff0c;然后再填充 slots 從而生成 SQL。 論文發…

C++函數指針,鍵值對集合的學習

這段代碼使用了 std::unordered_map 來存儲 std::wstring 作為鍵&#xff08;key&#xff09;&#xff0c;而對應的值&#xff08;value&#xff09;是一個 std::function<void(std::array<int, 5>, SomeClass&, int)> 類型的函數指針。這個結構使得根據字符串…

C++ 時間處理-日期時間類

1. 關鍵詞2. 問題3. 設計理念4. 支持的能力5. 代碼實現 5.1. datetime.h5.2. timecount.cpp 6. 測試代碼7. 運行結果8. 源碼地址 1. 關鍵詞 C 時間處理 日期時間類 跨平臺 2. 問題 為什么C就沒有一個方便好用的表示日期時間的類&#xff1f; 同樣是高級語言&#xff0c;Ja…

2024 HGDD 榮耀開發者日·成都站

HGDD 榮耀開發者日成都站 活動時間&#xff1a;2024 年 5 月 27 日 活動地點&#xff1a;成都市雙流區 LA CADIERE 蔚藍湖濱城 期待與大家的見面&#xff01;

ISO 9001認證 要換版了!

ISO TC176/SC2 第50次會議2023年10月8日至13日在盧旺達基加利舉行。 會議確定ISO 9001標準的修訂從2024年1月開始&#xff0c;將包括WD&#xff08;Working Draft&#xff09;、CD&#xff08; Committee Draft&#xff09;、DIS&#xff08;Draft for International Standard&…

js+vue3+elementplus發送驗證碼實現(含倒計時重新發送)

<template><el-form :model"formValue" :rules"rules" ref"form"><el-form-item prop"phone"><el-input v-model.number"formValue.phone" class"form-input" placeholder"請輸入手機號…

[matlab]yalmip國內源yalmip下載地址所有版本匯總

概述 MATLAB是一個強大的數值計算工具&#xff0c;用于數學建模、算法開發和數據分析。在MATLAB中&#xff0c;有很多工具箱可以幫助用戶完成不同類型的任務。本文將介紹如何在MATLAB中安裝Yalmip和Cplex&#xff0c;這兩個工具箱可以幫助用戶解決優化問題。 如果不想看文字描…

【oracle004】oracle內置函數手冊總結(已更新)

1.熟悉、梳理、總結下oracle相關知識體系。 2.日常研發過程中使用較少&#xff0c;隨著時間的推移&#xff0c;很快就忘得一干二凈&#xff0c;所以梳理總結下&#xff0c;以備日常使用參考 3.歡迎批評指正&#xff0c;跪謝一鍵三連&#xff01; 總結源文件資源下載地址&#x…

RoctetMQ使用(2):在項目中使用

一、導入相關依賴 在項目中引入MQ客戶端依賴&#xff0c;依賴版本最好和RocketMQ版本一致。 <!-- rocket客戶端--><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version&…

npm常用指令

基礎 命令&#xff1a;run 解釋&#xff1a;運行腳本 示例&#xff1a;npm run dev 命令&#xff1a;list || ls 解釋&#xff1a;查看依賴列表 示例&#xff1a;npm list || npm ls 命令&#xff1a;install || i 解釋&#xff1a;安裝依賴 示例&#xff1a;npm install ||…

一文掌握python隨機數random模塊

目錄 一、常用函數 1、 random.random() 2、random.uniform(a, b) 3、random.randint(a, b) 4、random.randrange(start, stop[, step]) 5、random.choice(sequence) 6、random.shuffle(x[, random]) 7、random.sample(population, k) 8、random.choices(population, w…

Qml:錨點

import QtQuick import QtQuick.WindowWindow {width: 800height: 600visible: truetitle: qsTr("Test Anchors")///錨點 上下左右Rectangle{id: anchor1width:200height: 150color:"#EEEEEE"Rectangle{id:rect1width:50height:50color: "red"Te…

Ubuntu 20/22 安裝 Jenkins

1. 使用 apt 命令安裝 Java Jenkins 作為一個 Java 應用程序&#xff0c;要求 Java 8 及更高版本&#xff0c;檢查系統上是否安裝了 Java。 sudo apt install -y openjdk-17-jre-headless安裝完成后&#xff0c;再次驗證 Java 是否已安裝 java --version2. 通過官方存儲庫安…

動態地控制kafka的消費速度,從而滿足業務要求

kafka是一個分布式流媒體平臺&#xff0c;它可以處理大規模的數據流&#xff0c;并允許實時消費該數據流。在實際應用中&#xff0c;我們需要動態控制kafka消費速度&#xff0c;以便處理數據流的速率能夠滿足系統和業務的需求。本文將介紹如何在kafka中實現動態控制消費速度的方…