Benewake(北醒) TFmini-S\TFmini Plus\TF-Luna\TF02-Pro\TF03 串口版本雷达在Linux(Ubuntu)-Python上的运用

前言

此文档简单介绍北醒单点系列雷达(串口版本)通过TTL转USB转接板在Ubuntu系统上使用python读取雷达数据

测试环境搭建

软件环境

  • Ubuntu 16.04 桌面版
    在这里插入图片描述
  • Python 3.5.2
    在这里插入图片描述
  • Python 第三方库
    pyserial
    numpy

第三方库安装说明

如果已安装pip可忽略此命令

sudo apt-get install python3-pip

安装pyserial

pip3 install pyserial

安装numpy

pip3 install numpy

接线说明

在这里插入图片描述
雷达、TTL转USB转接板,USB线、电脑
在这里插入图片描述

操作说明

确认接好线后,查询一下设备端口

ls -l /dev/ttyUSB*

如图:
在这里插入图片描述
此次使用的接口是 /dev/ttyUSB1
在下方代码下更改对应的接口

ser.port = '/dev/ttyUSB1'    #设置端口

运行对应的python文件脚本test.py(此文件需要按照自己的文件命名)

sudo python3 test.py

在这里插入图片描述

程序源码

#创建日期: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()

结果输出

在这里插入图片描述

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值