From da0d715d4991cab08d38bb0a0bef62508f6e1225 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 2 Mar 2018 16:02:04 +0100 Subject: [PATCH] Getting distance synchronously --- .../plugins/gpio/sensor/distance/__init__.py | 99 +++++++------------ platypush/plugins/gpio/zeroborg/__init__.py | 15 +-- 2 files changed, 37 insertions(+), 77 deletions(-) diff --git a/platypush/plugins/gpio/sensor/distance/__init__.py b/platypush/plugins/gpio/sensor/distance/__init__.py index b528ee12b..e61f5403f 100644 --- a/platypush/plugins/gpio/sensor/distance/__init__.py +++ b/platypush/plugins/gpio/sensor/distance/__init__.py @@ -12,7 +12,6 @@ class GpioSensorDistancePlugin(Plugin): def __init__(self, trigger_pin, echo_pin): self.trigger_pin = trigger_pin self.echo_pin = echo_pin - self.measured_distance_mm = None self._is_probing = False gpio.setmode(gpio.BCM) @@ -24,75 +23,45 @@ class GpioSensorDistancePlugin(Plugin): 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): - 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): - self._is_probing = False - self.measured_distance_mm = None + pulse_start = time.time() + pulse_on = pulse_start - if self._probe_thread and threading.get_ident() != self._probe_thread.ident: - self._probe_thread.join() + 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 - 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: diff --git a/platypush/plugins/gpio/zeroborg/__init__.py b/platypush/plugins/gpio/zeroborg/__init__.py index dad4b588e..b00b40218 100644 --- a/platypush/plugins/gpio/zeroborg/__init__.py +++ b/platypush/plugins/gpio/zeroborg/__init__.py @@ -40,7 +40,6 @@ class GpioZeroborgPlugin(Plugin): self.v_in = v_in self.v_out = v_out self.max_power = v_out / float(v_in) - self.distance_sensor = None self.auto_mode = False self.zb = ZeroBorg.ZeroBorg() @@ -50,16 +49,11 @@ class GpioZeroborgPlugin(Plugin): def get_distance(self): - if not self.distance_sensor: - self.distance_sensor = get_plugin('gpio.sensor.distance') - - if not self.distance_sensor: + distance_sensor = get_plugin('gpio.sensor.distance') + if not distance_sensor: raise RuntimeError('No gpio.sensor.distance configuration found') - if self.distance_sensor.get_distance() is None: - self.distance_sensor.start() - - return self.distance_sensor.get_distance() + return distance_sensor.get_distance() def drive(self, direction): @@ -126,9 +120,6 @@ class GpioZeroborgPlugin(Plugin): self.zb.MotorsOff() self.zb.ResetEpo() - if self.distance_sensor: - self.distance_sensor.stop() - return Response(output={'status':'stopped'})