From e60660b0e6b32a1eec18c113c48c899994d94c38 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 15 Aug 2019 00:24:57 +0200 Subject: [PATCH] Added plugin and backend for VL53L1X laser ranger/distance sensor --- docs/source/backends.rst | 1 + .../backend/sensor.distance.vl53l1x.rst | 6 ++ .../plugins/gpio.sensor.distance.vl53l1x.rst | 6 ++ docs/source/plugins.rst | 1 + platypush/backend/sensor/distance/vl53l1x.py | 30 +++++++++ .../plugins/gpio/sensor/distance/vl53l1x.py | 67 +++++++++++++++++++ requirements.txt | 3 + setup.py | 1 + 8 files changed, 115 insertions(+) create mode 100644 docs/source/platypush/backend/sensor.distance.vl53l1x.rst create mode 100644 docs/source/platypush/plugins/gpio.sensor.distance.vl53l1x.rst create mode 100644 platypush/backend/sensor/distance/vl53l1x.py create mode 100644 platypush/plugins/gpio/sensor/distance/vl53l1x.py diff --git a/docs/source/backends.rst b/docs/source/backends.rst index 619281ff7..d9377ddb5 100644 --- a/docs/source/backends.rst +++ b/docs/source/backends.rst @@ -32,6 +32,7 @@ Backends platypush/backend/scard.rst platypush/backend/sensor.accelerometer.rst platypush/backend/sensor.bme280.rst + platypush/backend/sensor.distance.vl53l1x.rst platypush/backend/sensor.envirophat.rst platypush/backend/sensor.ir.zeroborg.rst platypush/backend/sensor.leap.rst diff --git a/docs/source/platypush/backend/sensor.distance.vl53l1x.rst b/docs/source/platypush/backend/sensor.distance.vl53l1x.rst new file mode 100644 index 000000000..387de06b9 --- /dev/null +++ b/docs/source/platypush/backend/sensor.distance.vl53l1x.rst @@ -0,0 +1,6 @@ +``platypush.backend.sensor.distance.vl53l1x`` +============================================= + +.. automodule:: platypush.backend.sensor.distance.vl53l1x + :members: + diff --git a/docs/source/platypush/plugins/gpio.sensor.distance.vl53l1x.rst b/docs/source/platypush/plugins/gpio.sensor.distance.vl53l1x.rst new file mode 100644 index 000000000..e08e159b1 --- /dev/null +++ b/docs/source/platypush/plugins/gpio.sensor.distance.vl53l1x.rst @@ -0,0 +1,6 @@ +``platypush.plugins.gpio.sensor.distance.vl53l1x`` +================================================== + +.. automodule:: platypush.plugins.gpio.sensor.distance.vl53l1x + :members: + diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst index d7214a798..71d9f1c37 100644 --- a/docs/source/plugins.rst +++ b/docs/source/plugins.rst @@ -27,6 +27,7 @@ Plugins platypush/plugins/gpio.sensor.accelerometer.rst platypush/plugins/gpio.sensor.bme280.rst platypush/plugins/gpio.sensor.distance.rst + platypush/plugins/gpio.sensor.distance.vl53l1x.rst platypush/plugins/gpio.sensor.envirophat.rst platypush/plugins/gpio.sensor.ltr559.rst platypush/plugins/gpio.sensor.mcp3008.rst diff --git a/platypush/backend/sensor/distance/vl53l1x.py b/platypush/backend/sensor/distance/vl53l1x.py new file mode 100644 index 000000000..e12100bd2 --- /dev/null +++ b/platypush/backend/sensor/distance/vl53l1x.py @@ -0,0 +1,30 @@ +from platypush.backend.sensor import SensorBackend + + +class SensorDistanceVl53l1xBackend(SensorBackend): + """ + Backend to poll an `VL53L1x `_ + laser ranger/distance sensor + + Requires: + + * ``smbus2`` (``pip install smbus2``) + * ``vl53l1x`` (``pip install vl53l1x``) + """ + + def __init__(self, short=True, medium=True, long=True, **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) + """ + enabled_sensors = { + 'short': short, + 'medium': medium, + 'long': long, + } + + super().__init__(plugin='gpio.sensor.distance.vl53l1x', enabled_sensors=enabled_sensors, **kwargs) + + +# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/gpio/sensor/distance/vl53l1x.py b/platypush/plugins/gpio/sensor/distance/vl53l1x.py new file mode 100644 index 000000000..debebfe72 --- /dev/null +++ b/platypush/plugins/gpio/sensor/distance/vl53l1x.py @@ -0,0 +1,67 @@ +from platypush.plugins import action +from platypush.plugins.gpio.sensor import GpioSensorPlugin + + +class GpioSensorDistanceVl53l1xPlugin(GpioSensorPlugin): + """ + Plugin to interact with an `VL53L1x `_ + laser ranger/distance sensor + + Requires: + + * ``smbus2`` (``pip install smbus2``) + * ``vl53l1x`` (``pip install vl53l1x``) + """ + + def __init__(self, i2c_bus=1, i2c_address=0x29, **kwargs): + """ + :param i2c_bus: I2C bus number (default: 1) + :param i2c_address: I2C address (default: 0x29) + """ + + super().__init__(**kwargs) + + self.i2c_bus = i2c_bus + self.i2c_address = i2c_address + self._device = None + + # noinspection PyUnresolvedReferences + def _get_device(self): + if self._device: + return self._device + + import VL53L1X + self._device = VL53L1X(i2c_bus=self.i2c_bus, i2c_address=self.i2c_address) + return self._device + + @action + def get_measurement(self, short=True, medium=True, long=True): + """ + :param short: Enable short range measurement (default: True) + :param medium: Enable medium range measurement (default: True) + :param long: Enable long range measurement (default: True) + + :returns: dict. Example:: + + output = { + "short": 83, # Short range measurement in mm + "medium": 103, # Medium range measurement + "long": 43, # Long range measurement + } + + """ + + device = self._get_device() + device.open() + ret = {} + + for i, r in enumerate(['short', 'medium', 'long']): + if eval(r): + device.start_ranging(i+1) + ret[r] = device.get_distance() + device.stop_ranging() + + return ret + + +# vim:sw=4:ts=4:et: diff --git a/requirements.txt b/requirements.txt index 1d5f9d887..5d0a3a9b6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -159,3 +159,6 @@ pyScss # Support for LTR559 light/proximity sensor # ltr559 +# Support for VL53L1X laser ranger/distance sensor +# smbus2 +# vl53l1x diff --git a/setup.py b/setup.py index c87e386cf..595c4c223 100755 --- a/setup.py +++ b/setup.py @@ -175,6 +175,7 @@ setup( 'Support for GPS': ['gps'], 'Support for BME280 environment sensor': ['pimoroni-bme280'], 'Support for LTR559 light/proximity sensor': ['ltr559'], + 'Support for VL53L1X laser ranger/distance sensor': ['smbus2','vl53l1x'], # 'Support for Leap Motion backend': ['git+ssh://git@github.com:BlackLight/leap-sdk-python3.git'], # 'Support for Flic buttons': ['git+https://@github.com/50ButtonsEach/fliclib-linux-hci.git'] # 'Support for media subtitles': ['git+https://github.com/agonzalezro/python-opensubtitles#egg=python-opensubtitles']