通过Dronekit实现pixhawk2.4.8与树莓派4B通信

前段时间搭了一台无人机,飞控用的pixhawk2.4.8,机载电脑使用树莓派4B(安装Ubuntu22.04),想着先用dronekit完成通信,之后再用ros,但是通信老连不上,最后发现是波特率不一致的问题。为后来者避坑,遂写此文以记之。

一、Archiconda安装

Dronekit使用python2.7环境,但目前主流都已经是python3,所以需要anaconda创建一个python2.7版本的虚拟环境。但是在官网下载的anaconda在解包时被告知

package architecture (amd64) does not match system (arm64)

即安装包的架构amd64与树莓派的架构arm64不符,无法安装。
以下命令可用于显示系统架构:

sudo dpkg --print-architecture

运行结果如下:
在这里插入图片描述
Anaconda不支持arm64架构,因此树莓派开无法成功安装Anaconda。Archiconda是用于64位ARM平台的Conda发行版。

Archiconda下载地址

在这里插入图片描述
下载完成后安装

sudo sh Archiconda3-0.2.3-Linux-aarch64.sh

初始化环境添加到path选择yes

Do you wish the installer to initialize Archiconda3
in your /home/pi/.bashrc ? [yes|no]
[no] >>> yes

激活环境变量

source .bashrc

安装完成后可以发现本地文件夹增加了一个名为archiconda3的文件夹,archiconda和anaconda操作一致,接下来创建python2.7虚拟环境并激活

conda create -n dronepy27 python=2.7

激活环境

conda activate dronepy27

二、安装Dronekit

安装具体流程可参照官网

pip install dronekit 
pip install dronekit-sitl

安装dronekit-sitl用于之后的仿真。

三、树莓派与飞控的接线

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

需要一个usb转ttl模块,还需要杜邦2.54转1.25mm的6Pin信号端子线(这个1.25mm是我买了好几回的血泪教训呜呜呜,其他商家有专门卖2.4.8和树莓派通信的,但是很贵,我这一套不到15),读者要是不信可以自己测测,方法如下:
在这里插入图片描述
在这里插入图片描述

四、测试通信

1. 飞控设置波特率

将飞控连接电脑,并打开MissionPlanner

在这里插入图片描述
连接后选择 配置/调试—>全部参数树—>SERIAL2
设置115200波特率的代码115,点击写入参数保存,保存成功可以看到save successfully的提示
在这里插入图片描述

2. 树莓派测试代码

查看串口
在这里插入图片描述
若显示 ls: 无法访问 ‘dev/ttyUSB*’: 没有那个文件或目录可参照这篇博客
开放串口读写权限:

sudo chmod 666 /dev/ttyUSB0

然后创建一个test文件夹并在其中创建名为connect.py的python文件

mkdir test
cd test
touch connect.py

并插入以下代码

# connect.py for testing
# An highlighted block
from dronekit import connect
# Connect to the Vehicle (in this case a UDP endpoint)
vehicle = connect('/dev/ttyUSB0', wait_ready=True, baud=115200)
# vehicle is an instance of the Vehicle class
print "Autopilot Firmware version: %s" % vehicle.version
print "Autopilot capabilities (supports ftp): %s" % vehicle.capabilities.ftp
print "Global Location: %s" % vehicle.location.global_frame
print "Global Location (relative altitude): %s" % vehicle.location.global_relative_frame
print "Local Location: %s" % vehicle.location.local_frame    #NED
print "Attitude: %s" % vehicle.attitude
print "Velocity: %s" % vehicle.velocity   
print "GPS: %s" % vehicle.gps_0
print "Groundspeed: %s" % vehicle.groundspeed
print "Airspeed: %s" % vehicle.airspeed
print "Gimbal status: %s" % vehicle.gimbal
print "Battery: %s" % vehicle.battery
print "EKF OK?: %s" % vehicle.ekf_ok
print "Last Heartbeat: %s" % vehicle.last_heartbeat
print "Rangefinder: %s" % vehicle.rangefinder
print "Rangefinder distance: %s" % vehicle.rangefinder.distance
print "Rangefinder voltage: %s" % vehicle.rangefinder.voltage
print "Heading: %s" % vehicle.heading
print "Is Armable?: %s" % vehicle.is_armable
print "System status: %s" % vehicle.system_status.state
print "Mode: %s" % vehicle.mode.name    # settable
print "Armed: %s" % vehicle.armed    # settable

注意这里的波特率要和飞控接口的波特率一致,否则会连接超时

最后在python2.7环境下运行该脚本:
在这里插入图片描述
如果你连接成功,那么恭喜你,前进了一小(da)步!如果没有成功,请检查之前的步骤有无遗漏,再次尝试。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值