From 59d976d681a1bcb892860bcfc9683235dfa475ec Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 15 Aug 2019 15:20:49 +0200 Subject: [PATCH] Start ranging only with one setting (to get more accurate measurements once the sensor has warmed up) and configure it only at sensor startup --- platypush/backend/sensor/distance/vl53l1x.py | 6 ++-- .../plugins/gpio/sensor/distance/vl53l1x.py | 28 ++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/platypush/backend/sensor/distance/vl53l1x.py b/platypush/backend/sensor/distance/vl53l1x.py index 01f2bae23..cd4f78757 100644 --- a/platypush/backend/sensor/distance/vl53l1x.py +++ b/platypush/backend/sensor/distance/vl53l1x.py @@ -12,11 +12,11 @@ class SensorDistanceVl53L1XBackend(SensorBackend): * ``vl53l1x`` (``pip install vl53l1x``) """ - def __init__(self, short=True, medium=True, long=True, **kwargs): + def __init__(self, short=True, medium=False, long=False, **kwargs): """ :param short: Enable short range measurement (default: True) - :param medium: Enable medium range measurement (default: True) - :param long: Enable long range measurement (default: True) + :param medium: Enable medium range measurement (default: False) + :param long: Enable long range measurement (default: False) """ enabled_sensors = { 'short': short, diff --git a/platypush/plugins/gpio/sensor/distance/vl53l1x.py b/platypush/plugins/gpio/sensor/distance/vl53l1x.py index 1e473a602..1b8759ad6 100644 --- a/platypush/plugins/gpio/sensor/distance/vl53l1x.py +++ b/platypush/plugins/gpio/sensor/distance/vl53l1x.py @@ -28,21 +28,22 @@ class GpioSensorDistanceVl53L1XPlugin(GpioSensorPlugin): self._device = None # noinspection PyUnresolvedReferences - def _get_device(self): + def _get_device(self, ranging=1): if self._device: return self._device from VL53L1X import VL53L1X self._device = VL53L1X(i2c_bus=self.i2c_bus, i2c_address=self.i2c_address) self._device.open() + self._device.start_ranging(ranging) return self._device @action - def get_measurement(self, short=True, medium=True, long=True): + def get_measurement(self, short=True, medium=False, long=False): """ :param short: Enable short range measurement (default: True) - :param medium: Enable medium range measurement (default: True) - :param long: Enable long range measurement (default: True) + :param medium: Enable medium range measurement (default: False) + :param long: Enable long range measurement (default: False) :returns: dict. Example:: @@ -54,19 +55,26 @@ class GpioSensorDistanceVl53L1XPlugin(GpioSensorPlugin): """ - device = self._get_device() + range_idx = 0 + range_name = None + + for i, r in enumerate(['short', 'medium', 'long']): + if eval(r): + range_idx = i+1 + range_name = r + break + + assert range_name is not None + device = self._get_device(ranging=range_idx) ret = {} try: - for i, r in enumerate(['short', 'medium', 'long']): - if eval(r): - device.start_ranging(i+1) - ret[r] = device.get_distance() - device.stop_ranging() + ret[range_name] = device.get_distance() except Exception as e: self.logger.exception(e) try: + self._device.stop_ranging() self._device.close() except: pass