- Added IR sensor backend

- Fixed default power offsets
- Changed threshold distance for ultrasound sensor
- Fixed power multiplier coefficient
This commit is contained in:
Fabio Manganiello 2018-03-04 17:55:48 +01:00
parent 0f97678a78
commit 8b733fb859
8 changed files with 79 additions and 11 deletions

View file

View file

View file

@ -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:

View file

@ -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)

View file

@ -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:

View file

@ -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)