From 6309f5301e7c3845c01e69da733308823ed9ab11 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 17 Mar 2018 17:16:20 +0100 Subject: [PATCH] - Power offsets now passed through conf instead of source code - More robust handling of temporary sensor failures --- platypush/backend/sensor/ir/zeroborg.py | 22 ++++++----- platypush/plugins/gpio/zeroborg/__init__.py | 39 +++++++++++++------ .../plugins/gpio/zeroborg/lib/__init__.py | 4 +- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/platypush/backend/sensor/ir/zeroborg.py b/platypush/backend/sensor/ir/zeroborg.py index 74f4b640a8..068709887f 100644 --- a/platypush/backend/sensor/ir/zeroborg.py +++ b/platypush/backend/sensor/ir/zeroborg.py @@ -26,16 +26,20 @@ class SensorIrZeroborgBackend(Backend): 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)) + try: + 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 \ + self.last_message = message + self.last_message_timestamp = time.time() + except OSError as e: + logging.warning('Failed reading IR sensor status') + + if 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)) diff --git a/platypush/plugins/gpio/zeroborg/__init__.py b/platypush/plugins/gpio/zeroborg/__init__.py index 123613b599..fe423647da 100644 --- a/platypush/plugins/gpio/zeroborg/__init__.py +++ b/platypush/plugins/gpio/zeroborg/__init__.py @@ -22,20 +22,12 @@ class GpioZeroborgPlugin(Plugin): _drive_thread = None _can_run = False _direction = None - _power_offsets = { - Direction.DIR_LEFT: { - Direction.DIR_UP: 0.325, - Direction.DIR_DOWN: 0, - }, - - Direction.DIR_RIGHT: { - Direction.DIR_UP: -0.132, - Direction.DIR_DOWN: 0.387, - }, - } + _power_offsets = {} - def __init__(self, v_in=8.4, v_out=6.0): + def __init__(self, v_in=8.4, v_out=6.0, + power_offset_left_up=0.0, power_offset_right_up=0.0, + power_offset_left_down=0.0, power_offset_right_down=0.0): import platypush.plugins.gpio.zeroborg.lib as ZeroBorg self.v_in = v_in @@ -43,6 +35,17 @@ class GpioZeroborgPlugin(Plugin): self.max_power = v_out / float(v_in) self.auto_mode = False self._direction = None + self._power_offsets = { + Direction.DIR_LEFT: { + Direction.DIR_UP: power_offset_left_up, + Direction.DIR_DOWN: power_offset_left_down, + }, + + Direction.DIR_RIGHT: { + Direction.DIR_UP: power_offset_right_up, + Direction.DIR_DOWN: power_offset_right_down, + }, + } self.zb = ZeroBorg.ZeroBorg() self.zb.Init() @@ -83,10 +86,22 @@ class GpioZeroborgPlugin(Plugin): if self.auto_mode: distance = None + last_recorded_distance_timestamp = None + distance_record_timeout = 2.0 + while distance is None: distance = self.get_distance() logging.info('Closest obstacle distance: {} mm'.format(distance)) + if last_recorded_distance_timestamp and \ + time.time() - last_recorded_distance_timestamp > distance_record_timeout: + # Stop the motors if we have been unable + # to access the distance sensor data + self._direction = None + break + + last_recorded_distance_timestamp = time.time() + if distance > 400.0: # distance in mm self._direction = Direction.DIR_UP.value else: diff --git a/platypush/plugins/gpio/zeroborg/lib/__init__.py b/platypush/plugins/gpio/zeroborg/lib/__init__.py index 326d0fa299..87a15aa07a 100644 --- a/platypush/plugins/gpio/zeroborg/lib/__init__.py +++ b/platypush/plugins/gpio/zeroborg/lib/__init__.py @@ -758,9 +758,9 @@ If True there has been a new IR message which can be read using GetIrMessage(). i2cRecv = self.RawRead(COMMAND_GET_NEW_IR, I2C_NORM_LEN) except KeyboardInterrupt: raise - except: + except Exception as e: self.Print('Failed reading new IR message received flag!') - return + raise e if i2cRecv[1] == COMMAND_VALUE_OFF: return False