无人机避障—— 激光雷达定高北醒TF03-UART(二)

无人机避障过程,光靠大疆飞控内部的气压计不准,很容易在高度较低的时候受到地面植被等障碍物影响,使得掉高严重,因此采用激光雷达定高模块进行定高。

硬件:

北醒TF03-UART、Xavier-NX

软件代码:

北醒官方python指导:Benewake(北醒) TFmini-S\TFmini Plus\TF-Luna\TF02-Pro\TF03 串口版本雷达在Linux(Ubuntu)-Python上的运用-CSDN博客

python 复制代码
#创建日期:2020年10月10日
#版本:初版
#此程序对应北醒TF系列默认配置下串口版本有效
#此程序只提供参考和学习
# -*- coding: utf-8 -*-

import serial.tools.list_ports
import time
import numpy as np

ser = serial.Serial()
ser.port = '/dev/ttyUSB1'    #设置端口
ser.baudrate = 115200 #设置雷达的波特率
def getTFminiData():
   while True:
      count = ser.in_waiting #获取接收到的数据长度
      if count > 8:
         recv = ser.read(9)#读取数据并将数据存入recv
         #print('get data from serial port:', recv)
         ser.reset_input_buffer()#清除输入缓冲区
         if recv[0] == 0x59 and recv[1] == 0x59:  # python3
            distance = np.int16(recv[2] + np.int16(recv[3] << 8))
            strength = recv[4] + recv[5] * 256
            temp = (np.int16(recv[6] + np.int16(recv[7] << 8)))/8-256 #计算芯片温度
            print('distance = %5d  strengh = %5d  temperature = %5d' % (distance, strength, temp))
            ser.reset_input_buffer()
         if recv[0] == 'Y' and recv[1] == 'Y':  # python2 //此处标示出文件读取成功
            lowD = int(recv[2].encode('hex'), 16)
            highD = int(recv[3].encode('hex'), 16)
            lowS = int(recv[4].encode('hex'), 16)
            highS = int(recv[5].encode('hex'), 16)
            lowT = int(recv[6].encode('hex'), 16)
            highT = int(recv[7].encode('hex'), 16)
            distance = np.int16(lowD + np.int16(highD << 8))
            strength = lowS + highS * 256
            temp = (np.int16(lowD + np.int16(highD << 8)))/8-256 #计算芯片温度
            print('distance = %5d  strengh = %5d  temperature = %5d' % (distance, strength, temp))
      else:
         time.sleep(0.005) #50ms
if __name__ == '__main__':
   try:
      if ser.is_open == False:
         try:
            ser.open()
         except:
            print('Open COM failed!')
      getTFminiData()
   except KeyboardInterrupt:  # Ctrl+C 停止输出数据
      if ser != None:
         ser.close()

最重要的就是设置端口号:

python 复制代码
ls -l /dev/ttyUSB*

拔掉激光雷达串口:

确定是USB1设置就好了。

运行代码:

bash 复制代码
sudo python3 test.py

测试成功!

相关推荐
时间的拾荒人9 小时前
Linux 系统命令与网络、磁盘、日志监控(下篇):性能原理与优化
linux·运维·网络
金色熊族9 小时前
qt5.15.9编译qt-creator7.0.2源码
linux·qt
Rabitebla9 小时前
【Linux】文件篇(五):手撕简易 FILE——模拟实现 C 标准 I/O 缓冲机制
linux·服务器·c语言·笔记·学习·ubuntu
蜗牛一步步往上爬10 小时前
第十章 使用编辑器
linux
无足鸟ICT14 小时前
【RHCA+】swap和物理内存
linux
Huangjin007_19 小时前
【Linux 系统篇(四十一)】线程(二):线程的优缺点、资源共享与上下文切换
linux·运维·服务器
嵌入式 Linux手记20 小时前
嵌入式面试都考的置位清位:位运算四件套
linux·c语言·驱动开发·面试·职场和发展·嵌入式
Rabitebla20 小时前
【Linux】文件篇(六):Shell 实战——从系统调用到命令行重定向
linux·服务器·c++·笔记·学习
Evand J20 小时前
【MATLAB例程】基于APF的多无人机动态路径规划与避障仿真。避碰及安全性能分析。附完整代码的下载链接
开发语言·matlab·无人机
dingdingfish21 小时前
TLPI 第57章 读书笔记:Sockets: UNIX Domain
linux·socket·domain·sendto·recvfrom·tlpi·socketpair