sensor.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:coffee-collector-messenger 作者: pixelfusion 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号