From 8b733fb859ae8522b7108189d95d910e87886ce1 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 4 Mar 2018 17:55:48 +0100 Subject: [PATCH] - Added IR sensor backend - Fixed default power offsets - Changed threshold distance for ultrasound sensor - Fixed power multiplier coefficient --- platypush/backend/sensor/__init__.py | 0 platypush/backend/sensor/distance/__init__.py | 0 platypush/backend/sensor/ir/__init__.py | 0 platypush/backend/sensor/ir/zeroborg.py | 48 +++++++++++++++++++ platypush/context/__init__.py | 9 ++-- platypush/message/event/sensor/__init__.py | 0 platypush/message/event/sensor/ir/__init__.py | 20 ++++++++ platypush/plugins/gpio/zeroborg/__init__.py | 13 ++--- 8 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 platypush/backend/sensor/__init__.py create mode 100644 platypush/backend/sensor/distance/__init__.py create mode 100644 platypush/backend/sensor/ir/__init__.py create mode 100644 platypush/backend/sensor/ir/zeroborg.py create mode 100644 platypush/message/event/sensor/__init__.py create mode 100644 platypush/message/event/sensor/ir/__init__.py diff --git a/platypush/backend/sensor/__init__.py b/platypush/backend/sensor/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/platypush/backend/sensor/distance/__init__.py b/platypush/backend/sensor/distance/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/platypush/backend/sensor/ir/__init__.py b/platypush/backend/sensor/ir/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/platypush/backend/sensor/ir/zeroborg.py b/platypush/backend/sensor/ir/zeroborg.py new file mode 100644 index 00000000..74f4b640 --- /dev/null +++ b/platypush/backend/sensor/ir/zeroborg.py @@ -0,0 +1,48 @@ +import logging +import time + +import platypush.plugins.gpio.zeroborg.lib as ZeroBorg + +from platypush.backend import Backend +from platypush.message.event.sensor.ir import IrKeyUpEvent, IrKeyDownEvent + +class SensorIrZeroborgBackend(Backend): + last_message =None + last_message_timestamp = None + + def __init__(self, no_message_timeout=0.37, **kwargs): + super().__init__(**kwargs) + self.no_message_timeout = no_message_timeout + self.zb = ZeroBorg.ZeroBorg() + self.zb.Init() + logging.info('Initialized Zeroborg infrared sensor backend') + + + def send_message(self, message): + pass + + + def run(self): + super().run() + + while True: + self.zb.GetIrMessage() + if self.zb.HasNewIrMessage(): + message = self.zb.GetIrMessage() + if message != self.last_message: + logging.info('Received key down event on the IR sensor: {}'.format(message)) + self.bus.post(IrKeyDownEvent(message=message)) + + self.last_message = message + self.last_message_timestamp = time.time() + elif self.last_message_timestamp and \ + time.time() - self.last_message_timestamp > self.no_message_timeout: + logging.info('Received key up event on the IR sensor') + self.bus.post(IrKeyUpEvent(message=self.last_message)) + + self.last_message = None + self.last_message_timestamp = None + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index 874598be..bfe243b3 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -1,4 +1,3 @@ -import functools import importlib import logging @@ -29,10 +28,10 @@ def register_backends(bus=None, global_scope=False, **kwargs): module = importlib.import_module('platypush.backend.' + name) # e.g. backend.pushbullet main class: PushbulletBackend - cls_name = functools.reduce( - lambda a,b: a.title() + b.title(), - (module.__name__.title().split('.')[2:]) - ) + 'Backend' + cls_name = '' + for token in module.__name__.title().split('.')[2:]: + cls_name += token.title() + cls_name += 'Backend' try: b = getattr(module, cls_name)(bus=bus, **cfg, **kwargs) diff --git a/platypush/message/event/sensor/__init__.py b/platypush/message/event/sensor/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/platypush/message/event/sensor/ir/__init__.py b/platypush/message/event/sensor/ir/__init__.py new file mode 100644 index 00000000..a6f32e93 --- /dev/null +++ b/platypush/message/event/sensor/ir/__init__.py @@ -0,0 +1,20 @@ +from platypush.message.event import Event + + +class IrSensorEvent(Event): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +class IrKeyUpEvent(IrSensorEvent): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +class IrKeyDownEvent(IrSensorEvent): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/plugins/gpio/zeroborg/__init__.py b/platypush/plugins/gpio/zeroborg/__init__.py index e97ef3e8..ddd5a84b 100644 --- a/platypush/plugins/gpio/zeroborg/__init__.py +++ b/platypush/plugins/gpio/zeroborg/__init__.py @@ -23,7 +23,7 @@ class GpioZeroborgPlugin(Plugin): _direction = None _power_offsets = { Direction.DIR_LEFT: { - Direction.DIR_UP: 0.242, + Direction.DIR_UP: 0.315, Direction.DIR_DOWN: 0.285, }, @@ -76,8 +76,9 @@ class GpioZeroborgPlugin(Plugin): distance = None while distance is None: distance = self.get_distance() + print(distance) - if distance > 250.0: # distance in mm + if distance > 300.0: # distance in mm self._direction = Direction.DIR_UP.value else: logging.info('Physical obstacle detected at {} mm'. @@ -93,16 +94,16 @@ class GpioZeroborgPlugin(Plugin): left = -1.0 - self._power_offsets[Direction.DIR_LEFT][Direction.DIR_DOWN] right = -1.0 - self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_DOWN] elif self._direction == Direction.DIR_LEFT.value: - left = 2.0 + self._power_offsets[Direction.DIR_LEFT][Direction.DIR_UP] - right = -1.25 - self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_DOWN] - elif self._direction == Direction.DIR_RIGHT.value: left = -1.25 - self._power_offsets[Direction.DIR_LEFT][Direction.DIR_DOWN] right = 2.0 + self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_UP] + elif self._direction == Direction.DIR_RIGHT.value: + left = 2.0 + self._power_offsets[Direction.DIR_LEFT][Direction.DIR_UP] + right = -1.25 - self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_DOWN] elif self._direction is not None: logging.warning('Invalid direction: {}'.format(direction)) self.stop() - power = 0.6 # Still debugging the right power range + power = 0.75 # Still debugging the right power range self.zb.SetMotor1(power * left * self.max_power) self.zb.SetMotor2(power * left * self.max_power)