Start ranging only with one setting (to get more accurate measurements once the sensor has warmed up) and configure it only at sensor startup

This commit is contained in:
Fabio Manganiello 2019-08-15 15:20:49 +02:00
parent dccd3fd3d4
commit 59d976d681
2 changed files with 21 additions and 13 deletions

View file

@ -12,11 +12,11 @@ class SensorDistanceVl53L1XBackend(SensorBackend):
* ``vl53l1x`` (``pip install vl53l1x``) * ``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 short: Enable short range measurement (default: True)
:param medium: Enable medium range measurement (default: True) :param medium: Enable medium range measurement (default: False)
:param long: Enable long range measurement (default: True) :param long: Enable long range measurement (default: False)
""" """
enabled_sensors = { enabled_sensors = {
'short': short, 'short': short,

View file

@ -28,21 +28,22 @@ class GpioSensorDistanceVl53L1XPlugin(GpioSensorPlugin):
self._device = None self._device = None
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
def _get_device(self): def _get_device(self, ranging=1):
if self._device: if self._device:
return self._device return self._device
from VL53L1X import VL53L1X from VL53L1X import VL53L1X
self._device = VL53L1X(i2c_bus=self.i2c_bus, i2c_address=self.i2c_address) self._device = VL53L1X(i2c_bus=self.i2c_bus, i2c_address=self.i2c_address)
self._device.open() self._device.open()
self._device.start_ranging(ranging)
return self._device return self._device
@action @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 short: Enable short range measurement (default: True)
:param medium: Enable medium range measurement (default: True) :param medium: Enable medium range measurement (default: False)
:param long: Enable long range measurement (default: True) :param long: Enable long range measurement (default: False)
:returns: dict. Example:: :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 = {} ret = {}
try: try:
for i, r in enumerate(['short', 'medium', 'long']): ret[range_name] = device.get_distance()
if eval(r):
device.start_ranging(i+1)
ret[r] = device.get_distance()
device.stop_ranging()
except Exception as e: except Exception as e:
self.logger.exception(e) self.logger.exception(e)
try: try:
self._device.stop_ranging()
self._device.close() self._device.close()
except: except:
pass pass