我正在研究声纳传感器,该传感器回声信号并接收回声。还创建了一个算法,可根据读数计算距离。但是我面临一些奇怪的问题,有时传感器发送的随机数据根本不相关,例如与目标物的实际距离相差5cm-160cm,而传感器发送的数据却是2000 + cm。很难弄清原因,而且它也会自行关闭。
我的问题是
我在Raspberry pi上使用Sonar传感器并使用此代码
# set Trigger to HIGH
GPIO.output(TRIGGER, True)
#print "GPIO", GPIO
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
try:
now = time.time()
# save the time it takes just before ECHO hears the TRIGGER pulse (0 means nothing received)
while GPIO.input(ECHO) == 0:
StartTime = time.time()
#print "StartTime ", StartTime
#print "now: ", now
if StartTime-now > 1:
return -1
#print("StartTime: " , StartTime) #Printing this causes incorrect measurements.
# save time of arrival
while GPIO.input(ECHO) == 1:
StopTime = time.time()
#print("StopTime: ", StopTime)
except KeyboardInterrupt:
return -1
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
我正在使用此代码来计算距离