Getting distance synchronously

This commit is contained in:
Fabio Manganiello 2018-03-02 16:02:04 +01:00
parent df6790cde9
commit da0d715d49
2 changed files with 37 additions and 77 deletions

View file

@ -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,16 +23,11 @@ class GpioSensorDistancePlugin(Plugin):
time.sleep(1) time.sleep(1)
def start(self): def get_distance(self):
self._is_probing = True
logging.info('Received distance sensor start command')
def _run():
gpio.setmode(gpio.BCM) gpio.setmode(gpio.BCM)
gpio.setup(self.trigger_pin, gpio.OUT) gpio.setup(self.trigger_pin, gpio.OUT)
gpio.setup(self.echo_pin, gpio.IN) gpio.setup(self.echo_pin, gpio.IN)
while self._is_probing:
gpio.output(self.trigger_pin, True) gpio.output(self.trigger_pin, True)
time.sleep(0.00001) # 1 us pulse to trigger echo measurement time.sleep(0.00001) # 1 us pulse to trigger echo measurement
gpio.output(self.trigger_pin, False) gpio.output(self.trigger_pin, False)
@ -61,38 +55,13 @@ class GpioSensorDistancePlugin(Plugin):
break break
if read_timeout: if read_timeout:
continue return None
pulse_end = pulse_off pulse_end = pulse_off
pulse_duration = pulse_end - pulse_start pulse_duration = pulse_end - pulse_start
# s = vt where v = avg speed of sound # s = vt where v = avg speed of sound
self.measured_distance_mm = round(pulse_duration * 171500.0, 2) return 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):
return self.measured_distance_mm
def stop(self):
self._is_probing = False
self.measured_distance_mm = None
if self._probe_thread and threading.get_ident() != self._probe_thread.ident:
self._probe_thread.join()
return Response(output={'status':'stopped'})
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -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'})