def measureDistance_HCSRO4(TRIG_ID, ECHO_ID):
GPIO.output(TRIG_ID, True)
time.sleep(0.00001)
GPIO.output(TRIG_ID, False)
edge_detect = GPIO.wait_for_edge(ECHO_ID, GPIO.RISING, timeout = 100)
if edge_detect is not None:
pulse_start = time.time()
else: return MAX_DIST
edge_detect = GPIO.wait_for_edge(ECHO_ID, GPIO.FALLING, timeout = 100)
if edge_detect is not None:
pulse_end = time.time()
else: return MAX_DIST
pulse_duration = pulse_end - pulse_start
distance = round(pulse_duration * 17150, 2)
return distance
评论列表
文章目录