Getting distance synchronously
This commit is contained in:
parent
df6790cde9
commit
da0d715d49
2 changed files with 37 additions and 77 deletions
|
@ -12,7 +12,6 @@ class GpioSensorDistancePlugin(Plugin):
|
||||||
def __init__(self, trigger_pin, echo_pin):
|
def __init__(self, trigger_pin, echo_pin):
|
||||||
self.trigger_pin = trigger_pin
|
self.trigger_pin = trigger_pin
|
||||||
self.echo_pin = echo_pin
|
self.echo_pin = echo_pin
|
||||||
self.measured_distance_mm = None
|
|
||||||
self._is_probing = False
|
self._is_probing = False
|
||||||
|
|
||||||
gpio.setmode(gpio.BCM)
|
gpio.setmode(gpio.BCM)
|
||||||
|
@ -24,75 +23,45 @@ class GpioSensorDistancePlugin(Plugin):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
self._is_probing = True
|
|
||||||
logging.info('Received distance sensor start command')
|
|
||||||
|
|
||||||
def _run():
|
|
||||||
gpio.setmode(gpio.BCM)
|
|
||||||
gpio.setup(self.trigger_pin, gpio.OUT)
|
|
||||||
gpio.setup(self.echo_pin, gpio.IN)
|
|
||||||
|
|
||||||
while self._is_probing:
|
|
||||||
gpio.output(self.trigger_pin, True)
|
|
||||||
time.sleep(0.00001) # 1 us pulse to trigger echo measurement
|
|
||||||
gpio.output(self.trigger_pin, False)
|
|
||||||
read_timeout = False
|
|
||||||
|
|
||||||
pulse_start = time.time()
|
|
||||||
pulse_on = pulse_start
|
|
||||||
|
|
||||||
while gpio.input(self.echo_pin) == 0:
|
|
||||||
pulse_on = time.time()
|
|
||||||
if pulse_on - pulse_start > 0.5:
|
|
||||||
logging.warning('Distance sensor echo timeout: 0')
|
|
||||||
read_timeout = True
|
|
||||||
break
|
|
||||||
|
|
||||||
pulse_start = pulse_on
|
|
||||||
pulse_end = time.time()
|
|
||||||
pulse_off = pulse_end
|
|
||||||
|
|
||||||
while gpio.input(self.echo_pin) == 1:
|
|
||||||
pulse_off = time.time()
|
|
||||||
if pulse_off - pulse_end > 0.5:
|
|
||||||
logging.warning('Distance sensor echo timeout: 1')
|
|
||||||
read_timeout = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if read_timeout:
|
|
||||||
continue
|
|
||||||
|
|
||||||
pulse_end = pulse_off
|
|
||||||
pulse_duration = pulse_end - pulse_start
|
|
||||||
|
|
||||||
# s = vt where v = avg speed of sound
|
|
||||||
self.measured_distance_mm = round(pulse_duration * 171500.0, 2)
|
|
||||||
|
|
||||||
gpio.output(self.trigger_pin, False)
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
gpio.cleanup()
|
|
||||||
self.measured_distance_mm = None
|
|
||||||
|
|
||||||
self._probe_thread = threading.Thread(target=_run)
|
|
||||||
self._probe_thread.start()
|
|
||||||
|
|
||||||
return Response(output={'status': 'probing'})
|
|
||||||
|
|
||||||
|
|
||||||
def get_distance(self):
|
def get_distance(self):
|
||||||
return self.measured_distance_mm
|
gpio.setmode(gpio.BCM)
|
||||||
|
gpio.setup(self.trigger_pin, gpio.OUT)
|
||||||
|
gpio.setup(self.echo_pin, gpio.IN)
|
||||||
|
|
||||||
|
gpio.output(self.trigger_pin, True)
|
||||||
|
time.sleep(0.00001) # 1 us pulse to trigger echo measurement
|
||||||
|
gpio.output(self.trigger_pin, False)
|
||||||
|
read_timeout = False
|
||||||
|
|
||||||
def stop(self):
|
pulse_start = time.time()
|
||||||
self._is_probing = False
|
pulse_on = pulse_start
|
||||||
self.measured_distance_mm = None
|
|
||||||
|
|
||||||
if self._probe_thread and threading.get_ident() != self._probe_thread.ident:
|
while gpio.input(self.echo_pin) == 0:
|
||||||
self._probe_thread.join()
|
pulse_on = time.time()
|
||||||
|
if pulse_on - pulse_start > 0.5:
|
||||||
|
logging.warning('Distance sensor echo timeout: 0')
|
||||||
|
read_timeout = True
|
||||||
|
break
|
||||||
|
|
||||||
return Response(output={'status':'stopped'})
|
pulse_start = pulse_on
|
||||||
|
pulse_end = time.time()
|
||||||
|
pulse_off = pulse_end
|
||||||
|
|
||||||
|
while gpio.input(self.echo_pin) == 1:
|
||||||
|
pulse_off = time.time()
|
||||||
|
if pulse_off - pulse_end > 0.5:
|
||||||
|
logging.warning('Distance sensor echo timeout: 1')
|
||||||
|
read_timeout = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if read_timeout:
|
||||||
|
return None
|
||||||
|
|
||||||
|
pulse_end = pulse_off
|
||||||
|
pulse_duration = pulse_end - pulse_start
|
||||||
|
|
||||||
|
# s = vt where v = avg speed of sound
|
||||||
|
return round(pulse_duration * 171500.0, 2)
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
|
@ -40,7 +40,6 @@ class GpioZeroborgPlugin(Plugin):
|
||||||
self.v_in = v_in
|
self.v_in = v_in
|
||||||
self.v_out = v_out
|
self.v_out = v_out
|
||||||
self.max_power = v_out / float(v_in)
|
self.max_power = v_out / float(v_in)
|
||||||
self.distance_sensor = None
|
|
||||||
self.auto_mode = False
|
self.auto_mode = False
|
||||||
|
|
||||||
self.zb = ZeroBorg.ZeroBorg()
|
self.zb = ZeroBorg.ZeroBorg()
|
||||||
|
@ -50,16 +49,11 @@ class GpioZeroborgPlugin(Plugin):
|
||||||
|
|
||||||
|
|
||||||
def get_distance(self):
|
def get_distance(self):
|
||||||
if not self.distance_sensor:
|
distance_sensor = get_plugin('gpio.sensor.distance')
|
||||||
self.distance_sensor = get_plugin('gpio.sensor.distance')
|
if not distance_sensor:
|
||||||
|
|
||||||
if not self.distance_sensor:
|
|
||||||
raise RuntimeError('No gpio.sensor.distance configuration found')
|
raise RuntimeError('No gpio.sensor.distance configuration found')
|
||||||
|
|
||||||
if self.distance_sensor.get_distance() is None:
|
return distance_sensor.get_distance()
|
||||||
self.distance_sensor.start()
|
|
||||||
|
|
||||||
return self.distance_sensor.get_distance()
|
|
||||||
|
|
||||||
|
|
||||||
def drive(self, direction):
|
def drive(self, direction):
|
||||||
|
@ -126,9 +120,6 @@ class GpioZeroborgPlugin(Plugin):
|
||||||
self.zb.MotorsOff()
|
self.zb.MotorsOff()
|
||||||
self.zb.ResetEpo()
|
self.zb.ResetEpo()
|
||||||
|
|
||||||
if self.distance_sensor:
|
|
||||||
self.distance_sensor.stop()
|
|
||||||
|
|
||||||
return Response(output={'status':'stopped'})
|
return Response(output={'status':'stopped'})
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue