Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.5.2 #42

Merged
merged 16 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/create3_robots/cliff_sensors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2024 iRobot Corporation. All rights reserved.
#

from irobot_edu_sdk.backend.bluetooth import Bluetooth
from irobot_edu_sdk.robots import event, Create3

robot = Create3(Bluetooth())

@event(robot.when_cliff_sensor, [True, False, False, False])
async def cliff(robot):
print('Left')

@event(robot.when_cliff_sensor, [False, True, False, False])
async def cliff(robot):
print('Front Left')

@event(robot.when_cliff_sensor, [False, False, True, False])
async def cliff(robot):
print('Front Right')

@event(robot.when_cliff_sensor, [False, False, False, True])
async def cliff(robot):
print('Right')

@event(robot.when_cliff_sensor, [True, True, True, True])
async def cliff(robot):
print('There\'s a cliff!')

robot.play()
14 changes: 14 additions & 0 deletions examples/root_robots/cliff_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2024 iRobot Corporation. All rights reserved.
#

from irobot_edu_sdk.backend.bluetooth import Bluetooth
from irobot_edu_sdk.robots import event, Root

robot = Root(Bluetooth())

@event(robot.when_cliff_sensor, [True])
async def cliff(robot):
print('There\'s a cliff!')

robot.play()
7 changes: 6 additions & 1 deletion examples/utils/create3_all_options.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2021-2023 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2021-2024 iRobot Corporation. All rights reserved.
#

from irobot_edu_sdk.backend.bluetooth import Bluetooth
Expand Down Expand Up @@ -76,6 +76,11 @@ async def touched(robot):
await robot.move(-2)


@event(robot.when_cliff_sensor, [True, True, True, True])
async def cliff(robot):
print('There\'s a cliff!')


@event(robot.when_play)
async def play(robot):
print('play 1')
Expand Down
7 changes: 6 additions & 1 deletion examples/utils/root_all_options.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2021-2023 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2021-2024 iRobot Corporation. All rights reserved.
#

from irobot_edu_sdk.backend.bluetooth import Bluetooth
Expand Down Expand Up @@ -133,6 +133,11 @@ async def bright(robot):
print('Things got brighter; sensor values are', await robot.get_light_values())


@event(robot.when_cliff_sensor, [True])
async def cliff(robot):
print('There\'s a cliff!')


@event(robot.when_play)
async def play(robot):
print('play 1')
Expand Down
19 changes: 17 additions & 2 deletions irobot_edu_sdk/create3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2023 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2024 iRobot Corporation. All rights reserved.
#

import math
Expand Down Expand Up @@ -160,7 +160,7 @@ async def navigate_to(self, x: Union[int, float], y: Union[int, float], heading:
self._responses[(dev, cmd, inc)] = completer
await self._backend.write_packet(Packet(dev, cmd, inc, payload))
timeout = self.DEFAULT_TIMEOUT + int(math.sqrt(x * x + y * y) / 10) + 4 # 4 is the timeout for a potential rotation.
packet = await completer.wait(self.DEFAULT_TIMEOUT)
packet = await completer.wait(timeout)
if self.USE_ROBOT_POSE and packet:
return self.pose.set_from_packet(packet)
else:
Expand Down Expand Up @@ -224,3 +224,18 @@ async def get_version_string(self) -> str:
return '.'.join([major, str(minor), str(patch)])
except IndexError:
return None

def get_touch_sensors_cached(self):
'''Returns list of most recently seen touch sensor state, or None if no event has happened yet'''
return super().get_touch_sensors_cached()[0:2]

def get_cliff_sensors_cached(self):
'''Returns tuple of most recently seen cliff sensor state'''
return (self.cliff_sensor.left, self.cliff_sensor.front_left,
self.cliff_sensor.front_right, self.cliff_sensor.right)

async def get_cliff_sensors(self):
'''Returns tuple of most recently seen cliff sensor state.
If there were a protocol getter, this would await that response when the cache is empty.
'''
return self.get_cliff_sensors_cached()
6 changes: 5 additions & 1 deletion irobot_edu_sdk/getter_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2023 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2024 iRobot Corporation. All rights reserved.
#

import math
Expand Down 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
52 changes: 33 additions & 19 deletions irobot_edu_sdk/robot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2023 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2024 iRobot Corporation. All rights reserved.
#

try:
Expand Down Expand Up @@ -248,28 +248,42 @@ async def _when_touched_handler(self, packet: Packet):

for event in self._when_touched:
# An empty condition list means to trigger the event on every occurrence.
not_condition = not event.condition
any = (not_condition and self.touch_sensors.front_left) or (not_condition and self.touch_sensors.front_right) or (
not_condition and self.touch_sensors.back_left) or (not_condition and self.touch_sensors.back_right)
any = (not event.condition) and (self.touch_sensors.front_left or self.touch_sensors.front_right or
self.touch_sensors.back_left or self.touch_sensors.back_right)
if any:
await event.run(self)
elif len(event.condition) > 1 and len(event.condition) < 3:
if any or ((event.condition[0] and self.touch_sensors.front_left) or
(event.condition[1] and self.touch_sensors.front_right)):
if ( (event.condition[0] and self.touch_sensors.front_left) or
(event.condition[1] and self.touch_sensors.front_right)):
await event.run(self)
elif len(event.condition) > 3:
if any or ((event.condition[0] and self.touch_sensors.front_left) or
(event.condition[1] and self.touch_sensors.front_right) or
(event.condition[2] and self.touch_sensors.back_left) or
(event.condition[3] and self.touch_sensors.back_right)):
if ( (event.condition[0] and self.touch_sensors.front_left) or
(event.condition[1] and self.touch_sensors.front_right) or
(event.condition[2] and self.touch_sensors.back_left) or
(event.condition[3] and self.touch_sensors.back_right)):
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 @@ -297,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 Expand Up @@ -632,11 +646,11 @@ async def say(self, phrase: str):
break

def get_bumpers_cached(self):
'''Returns list of most recently seen bumper state, or None if no event has happened yet'''
'''Returns tuple of most recently seen bumper state, or None if no event has happened yet'''
return (self.bumpers.left, self.bumpers.right)

async def get_bumpers(self):
'''Returns list of most recently seen bumper state, or None if no event has happened yet.
'''Returns tuple of most recently seen bumper state, or None if no event has happened yet.
If there were a protocol getter, this would await that response when the cache is empty.
'''
return self.get_bumpers_cached()
Expand All @@ -657,12 +671,12 @@ async def get_accelerometer(self):
return None

def get_touch_sensors_cached(self):
'''Returns list of most recently seen touch sensor state, or None if no event has happened yet'''
'''Returns tuple of most recently seen touch sensor state, or None if no event has happened yet'''
return (self.touch_sensors.front_left, self.touch_sensors.front_right,
self.touch_sensors.back_left, self.touch_sensors.back_right)

async def get_touch_sensors(self):
'''Returns list of most recently seen touch sensor state, or None if no event has happened yet.
'''Returns tuple of most recently seen touch sensor state, or None if no event has happened yet.
If there were a protocol getter, this would await that response when the cache is empty.
'''
return self.get_touch_sensors_cached()
22 changes: 19 additions & 3 deletions irobot_edu_sdk/root.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2023 iRobot Corporation. All rights reserved.
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2020-2024 iRobot Corporation. All rights reserved.
#

import math
Expand Down Expand Up @@ -261,11 +261,27 @@ async def get_color_values(self, lighting: ColorLighting, data_format: ColorForm
return tuple(values)

def get_color_ids_cached(self):
'''Returns list of most recently seen color sensor IDs, or None if no event has happened yet'''
'''Returns tuple of most recently seen color sensor IDs, or None if no event has happened yet'''
return tuple(self.color_sensor.colors) if self.color_sensor.colors != [] else None

async def get_color_ids(self):
'''Returns list of most recently seen color sensor IDs, or None if no event has happened yet.
'''Returns tuple of most recently seen color sensor IDs, or None if no event has happened yet.
If there were a protocol getter, this would await that response when the cache is empty.
'''
return self.get_color_ids_cached()

def get_cliff_sensors_cached(self):
'''Returns tuple of most recently seen cliff sensor state'''
return (self.cliff_sensor.disable_motors)

async def get_cliff_sensors(self):
'''Returns tuple of most recently seen cliff sensor state.
If there were a protocol getter, this would await that response when the cache is empty.
'''
return self.get_cliff_sensors_cached()

async def get_cliff_sensor(self):
'''Returns tuple of most recently seen cliff sensor state.
If there were a protocol getter, this would await that response when the cache is empty.
'''
return self.get_cliff_sensors_cached()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "irobot_edu_sdk"
version = "0.5.1"
version = "0.5.2"
description = "Python SDK for iRobot Edu robots"
authors = ["iRobot Corporation <[email protected]>"]
license = "BSD-3-Clause"
Expand Down