def calculateDistance():
GPIO.output(TRIG, False)
time.sleep(0.000002)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
try:
pulse_start = pulseIn(ECHO, 0)
pulse_end = pulseIn(ECHO, 1)
except ValueError as error:
logging.warning(str(error))
return False
# Speed of sound 34300 cm/s
distance = round((pulse_end - pulse_start) * 17150)
# round distance to upper multiple of 5, e.g. 146 -> 150; 140 -> 145
distance += READING_ROUND - distance % READING_ROUND
return distance if 0 < distance < HEIGHT_CAN else False
# determines how full the can is
评论列表
文章目录