Some suggestions:
1. in arduino or ESP32, check the raw data first.
2. then add the logic step by step to rule out areas of influence.
3. delay function may also affect
For your information:
TF series
# -*- coding: utf-8 -*-
import serial.tools.list_ports
import time
import numpy as np
ser = serial.Serial()
ser.port = 'COM85' #设置端口
ser.baudrate = 115200 #设置雷达的波特率
def getTFminiData():
while True:
count = ser.in_waiting #获取接收到的数据长度
if count > 8:
recv = ser.read(9)#读取数据并将数据存入recv
ser.reset_input_buffer()#清除输入缓冲区
## print(hex(sum(recv[0:8]))[-2:])
## print(recv[8])
if recv[0] == 0x59 and recv[1] == 0x59 and recv[8] == int(hex(sum(recv[0:8]))[-2:],16): #python3 frame head and checksum check
distance = np.int16(recv[2] + np.int16(recv[3] << 8))
strength = recv[4] + recv[5] * 256
#print(strength)
#TFmini-s A01
if distance < 10 and distance >= 0:#测量盲区
print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
else:
if strength < 100:#信号强度过小
print('low strength: distance = %5d strength = %5d' % (distance, strength))
elif strength == 65535:#信号强度过饱和
print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
elif strength > 65535:#环境光饱和
print('unreliable data: ambient light saturation')
else:#正常打印
print('distance = %5d strength = %5d' % (distance, strength))
## #TFmini-plus A05
## if distance < 10 and distance >0:#测量盲区
## print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
## else:
## if strength < 100:#信号强度过小
## print('low strength: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
## #TFLuna A05
## if distance < 20 and distance > 0:#测量盲区
## print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
## else:
## if strength < 100:#信号强度过小
## print('low strength: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
##
## #TF02-Pro A01
## if strength < 60:#信号强度过小
## print('low strength: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 60 and distance >= 45 and distance <=60:
## print('distance = %5d strength = %5d' % (distance, strength))
## elif strength > 60 and distance > 60:
## print('abnormal: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
## #TF03 180
## if distance < 10:
## print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
## else:
## if strength < 40 or distance > 18000:#信号强度过小or超测量范围
## print('low strength or outof range: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
ser.reset_input_buffer()
if recv[0] == 'Y' and recv[1] == 'Y'and int(recv[8].encode('hex'),16) == int( hex(sum(int(i.encode('hex'),16) for i in recv[0:8]))[-2:],16): # 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)
distance = np.int16(lowD + np.int16(highD << 8))
strength = lowS + highS * 256
#TFmini-s A01
if distance < 10 and distance >= 0:#测量盲区
print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
else:
if strength < 100:#信号强度过小
print('low strength: distance = %5d strength = %5d' % (distance, strength))
elif strength == 65535:#信号强度过饱和
print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
elif strength > 65535:#环境光饱和
print('unreliable data: ambient light saturation')
else:#正常打印
print('distance = %5d strength = %5d' % (distance, strength))
## #TFmini-plus A05
## if distance < 10 and distance >0:#测量盲区
## print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
## else:
## if strength < 100:#信号强度过小
## print('low strength: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
## #TFLuna A05
## if distance < 20 and distance > 0:#测量盲区
## print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
## else:
## if strength < 100:#信号强度过小
## print('low strength: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
##
## #TF02-Pro A01
## if strength < 60:#信号强度过小
## print('low strength: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 60 and distance >= 45 and distance <=60:
## print('distance = %5d strength = %5d' % (distance, strength))
## elif strength > 60 and distance > 60:
## print('abnormal: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
## #TF03 180
## if distance < 10:
## print('unreliable data: range hole: distance = %5d strength = %5d' % (distance, strength))
## else:
## if strength < 40 or distance > 18000:#信号强度过小or超测量范围
## print('low strength or outof range: distance = %5d strength = %5d' % (distance, strength))
## elif strength == 65535:#信号强度过饱和
## print('strength saturation: distance = %5d strength = %5d' % (distance, strength))
## elif strength > 65535:#环境光饱和
## print('unreliable data: ambient light saturation')
## else:#正常打印
## print('distance = %5d strength = %5d' % (distance, strength))
ser.reset_input_buffer()
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()
only tf-luna
import serial
import time
ser = serial.Serial("/dev/ttyS0", 115200)
# we define a new function that will get the data from LiDAR and publish it
def read_data():
time.sleep(1) # Sleep 1000ms
while True:
counter = ser.in_waiting # count the number of bytes of the serial port
if counter > 8:
bytes_serial = ser.read(9)
ser.reset_input_buffer()
if bytes_serial[0] == 0x59 and bytes_serial[1] == 0x59: # python3
distance = bytes_serial[2] + bytes_serial[3]*256
strength = bytes_serial[4] + bytes_serial[5]*256
temperature = bytes_serial[6] + bytes_serial[7]*256 # For TFLuna
temperature = (temperature/8) - 256
print("TF-Luna python3 portion")
print("Distance:"+ str(distance) + "cm")
print("Strength:" + str(strength))
if temperature != 0:
print("Chip Temperature:" + str(temperature)+ "℃")
ser.reset_input_buffer()
if bytes_serial[0] == "Y" and bytes_serial[1] == "Y":
distL = int(bytes_serial[2].encode("hex"), 16)
distH = int(bytes_serial[3].encode("hex"), 16)
stL = int(bytes_serial[4].encode("hex"), 16)
stH = int(bytes_serial[5].encode("hex"), 16)
distance = distL + distH*256
strength = stL + stH*256
tempL = int(bytes_serial[6].encode("hex"), 16)
tempH = int(bytes_serial[7].encode("hex"), 16)
temperature = tempL + tempH*256
temperature = (temperature/8) - 256
print("TF-Luna python2 portion")
print("Distance:"+ str(distance) + "cm\n")
print("Strength:" + str(strength) + "\n")
print("Chip Temperature:" + str(temperature) + "℃\n")
ser.reset_input_buffer()
if __name__ == "__main__":
try:
if ser.isOpen() == False:
ser.open()
read_data()
except KeyboardInterrupt:
if ser != None:
ser.close()
print("program interrupted by the user")