2019-08-15 13:06:31 +02:00
|
|
|
import time
|
|
|
|
|
2019-08-15 00:24:57 +02:00
|
|
|
from platypush.plugins import action
|
|
|
|
from platypush.plugins.gpio.sensor import GpioSensorPlugin
|
|
|
|
|
|
|
|
|
2019-08-15 00:30:42 +02:00
|
|
|
class GpioSensorDistanceVl53L1XPlugin(GpioSensorPlugin):
|
2019-08-15 00:24:57 +02:00
|
|
|
"""
|
|
|
|
Plugin to interact with an `VL53L1x <https://www.st.com/en/imaging-and-photonics-solutions/vl53l1x.html>`_
|
|
|
|
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
|
2019-08-15 15:20:49 +02:00
|
|
|
def _get_device(self, ranging=1):
|
2019-08-15 00:24:57 +02:00
|
|
|
if self._device:
|
|
|
|
return self._device
|
|
|
|
|
2019-08-15 00:35:17 +02:00
|
|
|
from VL53L1X import VL53L1X
|
2019-08-15 00:24:57 +02:00
|
|
|
self._device = VL53L1X(i2c_bus=self.i2c_bus, i2c_address=self.i2c_address)
|
2019-08-15 13:06:31 +02:00
|
|
|
self._device.open()
|
2019-08-15 15:20:49 +02:00
|
|
|
self._device.start_ranging(ranging)
|
2019-08-15 00:24:57 +02:00
|
|
|
return self._device
|
|
|
|
|
|
|
|
@action
|
2019-08-15 15:20:49 +02:00
|
|
|
def get_measurement(self, short=True, medium=False, long=False):
|
2019-08-15 00:24:57 +02:00
|
|
|
"""
|
|
|
|
:param short: Enable short range measurement (default: True)
|
2019-08-15 15:20:49 +02:00
|
|
|
:param medium: Enable medium range measurement (default: False)
|
|
|
|
:param long: Enable long range measurement (default: False)
|
2019-08-15 00:24:57 +02:00
|
|
|
|
2020-01-09 23:40:59 +01:00
|
|
|
:returns: dict. Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2019-08-15 00:24:57 +02:00
|
|
|
|
|
|
|
output = {
|
|
|
|
"short": 83, # Short range measurement in mm
|
|
|
|
"medium": 103, # Medium range measurement
|
|
|
|
"long": 43, # Long range measurement
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-08-15 15:20:49 +02:00
|
|
|
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)
|
2019-08-15 00:24:57 +02:00
|
|
|
ret = {}
|
|
|
|
|
2019-08-15 13:06:31 +02:00
|
|
|
try:
|
2019-08-15 15:20:49 +02:00
|
|
|
ret[range_name] = device.get_distance()
|
2019-08-15 13:06:31 +02:00
|
|
|
except Exception as e:
|
|
|
|
self.logger.exception(e)
|
|
|
|
|
|
|
|
try:
|
2019-08-15 15:20:49 +02:00
|
|
|
self._device.stop_ranging()
|
2019-08-15 13:06:31 +02:00
|
|
|
self._device.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self._device = None
|
|
|
|
time.sleep(1)
|
2019-08-15 00:24:57 +02:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|