forked from platypush/platypush
- Power offsets now passed through conf instead of source code
- More robust handling of temporary sensor failures
This commit is contained in:
parent
dcd4f9eddf
commit
6309f5301e
3 changed files with 42 additions and 23 deletions
|
@ -26,6 +26,7 @@ class SensorIrZeroborgBackend(Backend):
|
||||||
super().run()
|
super().run()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
try:
|
||||||
self.zb.GetIrMessage()
|
self.zb.GetIrMessage()
|
||||||
if self.zb.HasNewIrMessage():
|
if self.zb.HasNewIrMessage():
|
||||||
message = self.zb.GetIrMessage()
|
message = self.zb.GetIrMessage()
|
||||||
|
@ -35,7 +36,10 @@ class SensorIrZeroborgBackend(Backend):
|
||||||
|
|
||||||
self.last_message = message
|
self.last_message = message
|
||||||
self.last_message_timestamp = time.time()
|
self.last_message_timestamp = time.time()
|
||||||
elif self.last_message_timestamp and \
|
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:
|
time.time() - self.last_message_timestamp > self.no_message_timeout:
|
||||||
logging.info('Received key up event on the IR sensor')
|
logging.info('Received key up event on the IR sensor')
|
||||||
self.bus.post(IrKeyUpEvent(message=self.last_message))
|
self.bus.post(IrKeyUpEvent(message=self.last_message))
|
||||||
|
|
|
@ -22,20 +22,12 @@ class GpioZeroborgPlugin(Plugin):
|
||||||
_drive_thread = None
|
_drive_thread = None
|
||||||
_can_run = False
|
_can_run = False
|
||||||
_direction = None
|
_direction = None
|
||||||
_power_offsets = {
|
_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,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
import platypush.plugins.gpio.zeroborg.lib as ZeroBorg
|
||||||
|
|
||||||
self.v_in = v_in
|
self.v_in = v_in
|
||||||
|
@ -43,6 +35,17 @@ class GpioZeroborgPlugin(Plugin):
|
||||||
self.max_power = v_out / float(v_in)
|
self.max_power = v_out / float(v_in)
|
||||||
self.auto_mode = False
|
self.auto_mode = False
|
||||||
self._direction = None
|
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 = ZeroBorg.ZeroBorg()
|
||||||
self.zb.Init()
|
self.zb.Init()
|
||||||
|
@ -83,10 +86,22 @@ class GpioZeroborgPlugin(Plugin):
|
||||||
|
|
||||||
if self.auto_mode:
|
if self.auto_mode:
|
||||||
distance = None
|
distance = None
|
||||||
|
last_recorded_distance_timestamp = None
|
||||||
|
distance_record_timeout = 2.0
|
||||||
|
|
||||||
while distance is None:
|
while distance is None:
|
||||||
distance = self.get_distance()
|
distance = self.get_distance()
|
||||||
logging.info('Closest obstacle distance: {} mm'.format(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
|
if distance > 400.0: # distance in mm
|
||||||
self._direction = Direction.DIR_UP.value
|
self._direction = Direction.DIR_UP.value
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -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)
|
i2cRecv = self.RawRead(COMMAND_GET_NEW_IR, I2C_NORM_LEN)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
raise
|
raise
|
||||||
except:
|
except Exception as e:
|
||||||
self.Print('Failed reading new IR message received flag!')
|
self.Print('Failed reading new IR message received flag!')
|
||||||
return
|
raise e
|
||||||
|
|
||||||
if i2cRecv[1] == COMMAND_VALUE_OFF:
|
if i2cRecv[1] == COMMAND_VALUE_OFF:
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in a new issue