def get_state(sleep_time=wheel_pulse):
# clear trigger sensor
gpio.output(trigger, False)
time.sleep(sleep_time)
# send trigger pulse
gpio.output(trigger, True)
time.sleep(0.00001)
gpio.output(trigger, False)
while gpio.input(echo) == 0:
pulse_start = time.time()
while gpio.input(echo) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 343 * 100 / 2. # speed of sound m/s * m to cm / round trip
if distance > 2 and distance < 400: # sensor range
distance = distance + distance_from_sensor_to_car_front
# don't worry about things further 4'
# this also reduces the size of the state machine
if distance >= max_distance:
distance = max_distance - 1
return int(distance)
##############################################################################
# perform action
##############################################################################
评论列表
文章目录