Skip to content

Commit

Permalink
Merge pull request #37 from scottcandy34/main
Browse files Browse the repository at this point in the history
Add cliff sensor event support
  • Loading branch information
shamlian authored Jan 16, 2024
2 parents d7b3cdc + 45687c1 commit 74502dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions irobot_edu_sdk/getter_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def __init__(self):
class CliffSensor:
def __init__(self):
self.disable_motors = False
self.left = False
self.front_left = False
self.right = False
self.front_right = False


class DockingSensor:
Expand Down
25 changes: 20 additions & 5 deletions irobot_edu_sdk/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,26 @@ async def _when_touched_handler(self, packet: Packet):
await event.run(self)

async def _when_cliff_sensor_handler(self, packet: Packet):
self.cliff_sensor.disable_motors = packet.payload[4] != 0
if len(packet.payload) > 4:
self.cliff_sensor.disable_motors = packet.payload[4] != 0
self.cliff_sensor.right = packet.payload[4] & 0x01 != 0
self.cliff_sensor.front_right = packet.payload[4] & 0x02 != 0
self.cliff_sensor.front_left = packet.payload[4] & 0x04 != 0
self.cliff_sensor.left = packet.payload[4] & 0x08 != 0

for event in self._when_cliff_sensor:
# TODO: Add trigger
await event.run(self)
for event in self._when_cliff_sensor:
# An empty condition list means to trigger the event on every occurrence.
if not event.condition and self.cliff_sensor.disable_motors: # Any.
await event.run(self)
elif len(event.condition) > 0 and len(event.condition) < 3:
if (event.condition[0] == self.cliff_sensor.disable_motors):
await event.run(self)
elif len(event.condition) > 3:
if ((event.condition[0] and self.cliff_sensor.left) or
(event.condition[1] and self.cliff_sensor.front_left) or
(event.condition[2] and self.cliff_sensor.front_right) or
(event.condition[3] and self.cliff_sensor.right)):
await event.run(self)

# Event Callbacks.

Expand Down Expand Up @@ -296,7 +311,7 @@ def when_touched(self, condition: list[bool, bool, bool, bool], callback: Callab
"""Register when touch callback of type: async def fn(front_left: bool, front_right: bool, back_left: bool, back_right: bool)."""
self._when_touched.append(Event(condition, callback))

def when_cliff_sensor(self, condition: list[bool], callback: Callable[[bool], Awaitable[None]]):
def when_cliff_sensor(self, condition: list[bool, bool, bool, bool], callback: Callable[[bool], Awaitable[None]]):
"""Register when cliff callback of type: async def fn(over_cliff: bool)."""
self._when_cliff_sensor.append(Event(condition, callback))

Expand Down

0 comments on commit 74502dd

Please sign in to comment.