Skip to content

Commit

Permalink
Merge pull request #32 from iRobotEducation/pre_0.5.0
Browse files Browse the repository at this point in the history
Pre 0.5.0
* Add color sensor support (close #4)
* Add docking sensor support (close #11)
* Fix logical error causing events to not save state without handlers
* Use proper Python enums for most getter types and commands
* Update and add examples
* General syntax improvements
* Add experimental and questionably-documented Turtle _backend_
* Bump minimum Python version to 3.10
  • Loading branch information
shamlian authored Dec 11, 2023
2 parents 03b79f9 + 60b97ae commit 5c30d19
Show file tree
Hide file tree
Showing 44 changed files with 1,254 additions and 240 deletions.
14 changes: 10 additions & 4 deletions examples/create3_robots/docking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

robot = Create3(Bluetooth())

POLL_SENSOR = True # Try changing this to compare the speed of events vs polling

@event(robot.when_play)
async def play(robot):
Expand All @@ -21,12 +22,17 @@ async def play(robot):

@event(robot.when_play)
async def play(robot):
# Dock sensor visualizer; could be improved with events

while True:
sensor = (await robot.get_docking_values())['IR sensor 0']
if POLL_SENSOR:
sensor = (await robot.get_docking_values())['IR sensor 0']
else:
sensor = robot.docking_sensor.sensors[0]
if sensor == None: # no event yet received
sensor = 0
r = 255 * ((sensor & 8)/8)
g = 255 * ((sensor & 4)/4)
b = 255 * (sensor & 1)
await robot.set_lights_rgb(r, g, b)
await robot.set_lights_on_rgb(r, g, b)

robot.play()
4 changes: 2 additions & 2 deletions examples/create3_robots/get_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def print_pos(robot):

@event(robot.when_play)
async def play(robot):
await robot.set_lights_rgb(30, 255, 100)
await robot.set_lights_on_rgb(30, 255, 100)
await robot.play_note(Note.A5, .5)

distance = 5
Expand All @@ -33,6 +33,6 @@ async def play(robot):
await robot.move(-distance)
print_pos(robot)

await robot.set_lights_rgb(30, 255, 100)
await robot.set_lights_on_rgb(30, 255, 100)

robot.play()
2 changes: 1 addition & 1 deletion examples/create3_robots/ir_proximity_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ async def play(robot):
r = 255 * sensors[2] / 4095
g = 255 * sensors[3] / 4095
b = 255 * sensors[4] / 4095
await robot.set_lights_rgb(r, g, b)
await robot.set_lights_on_rgb(r, g, b)

robot.play()
4 changes: 2 additions & 2 deletions examples/create3_robots/ir_proximity_obstacles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@


async def forward(robot):
await robot.set_lights_rgb(0, 255, 0)
await robot.set_lights_on_rgb(0, 255, 0)
await robot.set_wheel_speeds(speed, speed)


async def backoff(robot):
await robot.set_lights_rgb(255, 80, 0)
await robot.set_lights_on_rgb(255, 80, 0)
await robot.move(-20)
await robot.turn_left(45)

Expand Down
2 changes: 1 addition & 1 deletion examples/create3_robots/ir_proximity_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@event(robot.when_play)
async def play(robot):
await robot.set_lights_rgb(0, 255, 0)
await robot.set_lights_on_rgb(0, 255, 0)
while True:
sensors = (await robot.get_ir_proximity()).sensors
print(sensors)
Expand Down
6 changes: 3 additions & 3 deletions examples/create3_robots/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@event(robot.when_play)
async def play(robot):
await robot.set_lights_rgb(30, 255, 100)
await robot.set_lights_on_rgb(30, 255, 100)
await robot.play_note(Note.A5, .5)

distance = 16
Expand All @@ -22,7 +22,7 @@ async def play(robot):
await robot.navigate_to(distance, 0)
await robot.navigate_to(0, 0)

await robot.set_lights_rgb(30, 100, 255)
await robot.set_lights_on_rgb(30, 100, 255)

distance = -distance

Expand All @@ -31,6 +31,6 @@ async def play(robot):
await robot.navigate_to(distance, 0)
await robot.navigate_to(0, 0)

await robot.set_lights_rgb(30, 255, 100)
await robot.set_lights_on_rgb(30, 255, 100)

robot.play()
4 changes: 2 additions & 2 deletions examples/create3_robots/touch_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

@event(robot.when_touched, [True, False]) # (.) button.
async def touched(robot):
await robot.set_lights_rgb(255, 0, 0)
await robot.set_lights_on_rgb(255, 0, 0)
await robot.play_note(Note.A4, duration)


@event(robot.when_touched, [False, True]) # (..) button.
async def touched(robot):
await robot.set_lights_rgb(0, 255, 0)
await robot.set_lights_on_rgb(0, 255, 0)
await robot.play_note(Note.C5_SHARP, duration)


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

# This example uses the robot like a wand scanner; see what the robot sees!

from irobot_edu_sdk.backend.bluetooth import Bluetooth
from irobot_edu_sdk.robots import event, hand_over, Robot, Root
ColorID = Root.ColorID

try:
import colorama
from colorama import Fore, Back, Style
colorama.init()
colors = {ColorID.WHITE: Fore.WHITE + Back.WHITE + Style.BRIGHT + format(ColorID.WHITE, 'x'),
ColorID.BLACK: Fore.BLACK + Back.WHITE + Style.NORMAL + format(ColorID.BLACK, 'x'),
ColorID.RED: Fore.RED + Back.RED + Style.BRIGHT + format(ColorID.RED, 'x'),
ColorID.GREEN: Fore.GREEN + Back.GREEN + Style.BRIGHT + format(ColorID.GREEN, 'x'),
ColorID.BLUE: Fore.BLUE + Back.BLUE + Style.BRIGHT + format(ColorID.BLUE, 'x'),
ColorID.ORANGE: Fore.YELLOW + Back.BLACK + Style.NORMAL + format(ColorID.ORANGE, 'x'),
ColorID.YELLOW: Fore.YELLOW + Back.YELLOW + Style.BRIGHT + format(ColorID.YELLOW, 'x'),
ColorID.MAGENTA: Fore.MAGENTA + Back.MAGENTA + Style.BRIGHT + format(ColorID.MAGENTA, 'x'),
ColorID.LIME: Fore.GREEN + Back.BLACK + Style.BRIGHT + format(ColorID.LIME, 'x'),
ColorID.CYAN: Fore.CYAN + Back.BLACK + Style.BRIGHT + format(ColorID.CYAN, 'x'),
ColorID.VIOLET: Fore.MAGENTA + Back.BLACK + Style.NORMAL + format(ColorID.VIOLET, 'x'),
ColorID.CERISE: Fore.RED + Back.BLACK + Style.NORMAL + format(ColorID.CERISE, 'x'),
ColorID.LOW_INTENSITY: Fore.BLACK + Back.BLACK + Style.BRIGHT + format(ColorID.LOW_INTENSITY, 'x'),
ColorID.LOW_SATURATION: Fore.WHITE + Back.BLACK + Style.NORMAL + format(ColorID.LOW_SATURATION, 'x'),
'unk': Fore.RED + Back.CYAN + Style.NORMAL + '?',
'eol': Style.RESET_ALL}
except ImportError:
colors = {ColorID.WHITE: '⬜',
ColorID.BLACK: '⬛',
ColorID.RED: '🟥',
ColorID.GREEN: '🟩',
ColorID.BLUE: '🟦',
ColorID.ORANGE: '🟠',
ColorID.YELLOW: '🟨',
ColorID.MAGENTA: '🟪',
ColorID.LIME: '🟢',
ColorID.CYAN: '🔵',
ColorID.VIOLET: '🟣',
ColorID.CERISE: '🔴',
ColorID.LOW_INTENSITY: '🔳',
ColorID.LOW_SATURATION: '🔲',
'unk': '🟤',
'eol': ''}

robot = Root(Bluetooth())

@event(robot.when_play)
async def sense(robot):
await robot.set_wheel_speeds(1, 1)

while True:
try:
for c in (await robot.get_color_ids())[::-1]:
# Note -- the [::-1] reverses the order of the tuple,
# which is necessary to make it look correct in the console.
if c in ColorID:
print(colors[c], end='')
else:
print(colors['unk'], end='')
print(colors['eol'])

except TypeError:
print("Waiting for first color sensor event.")

await robot.wait(0.25)

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

from irobot_edu_sdk.backend.bluetooth import Bluetooth
from irobot_edu_sdk.robots import event, hand_over, Color, Robot, Root

robot = Root(Bluetooth())

@event(robot.when_play)
async def cake(robot):
# Body
await robot.set_marker_down()
for _ in range(4):
await robot.move(16)
await robot.turn_right(90)

# Icing
await robot.set_marker_and_eraser_up()
await robot.move(12)
await robot.set_marker_down()
for _ in range(2):
await robot.arc_right(-180, 2)
await robot.arc_left(180, 2)

# Candle
await robot.set_marker_and_eraser_up()
await robot.move(4)
await robot.turn_left(90)
await robot.move(7)
await robot.turn_right(90)

await robot.set_marker_down()
await robot.move(6)
await robot.turn_left(90)
await robot.move(1)
await robot.turn_right(90)
await robot.move(1)
await robot.turn_right(90)
await robot.arc_left(135, 1)
await robot.move(1)
await robot.turn_left(90)
await robot.move(1)
await robot.arc_left(135, 1)
await robot.turn_right(90)
await robot.move(1)
await robot.turn_right(90)
await robot.move(1)
await robot.turn_left(90)
await robot.move(6)

# Move away
await robot.set_marker_and_eraser_up()
await robot.turn_right(135)
await robot.move(16)

@event(robot.when_play)
async def cycle(robot):
while True:
await robot.set_lights_on_rgb(255, 0, 0)
await robot.wait(0.3)
await robot.set_lights_on_rgb(255, 255, 0)
await robot.wait(0.3)
await robot.set_lights_on_rgb(0, 255, 0)
await robot.wait(0.3)
await robot.set_lights_on_rgb(0, 255, 255)
await robot.wait(0.3)
await robot.set_lights_on_rgb(0, 0, 255)
await robot.wait(0.3)
await robot.set_lights_on_rgb(255, 0, 255)
await robot.wait(0.3)

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

from irobot_edu_sdk.backend.bluetooth import Bluetooth
from irobot_edu_sdk.robots import event, hand_over, Color, Robot, Root

robot = Root(Bluetooth())

@event(robot.when_play)
async def easy_as(robot):
# Base
await robot.set_marker_down()
await robot.turn_right(135)
await robot.move(8)
await robot.turn_left(45)
await robot.move(25)
await robot.turn_left(45)
await robot.move(8)
await robot.turn_left(135)
await robot.move(-3)
await robot.move(42)
await robot.turn_right(90)
await robot.move(2.5)

# Crimps
for _ in range(10):
await robot.arc_right(180, 1.24)
await robot.arc_left(180, 0.75)

# Top
await robot.arc_right(180, 1.24)
await robot.move(2.5)
await robot.move(-2.5)
await robot.arc_right(-90, 1.24)
await robot.turn_left(120)
await robot.arc_left(120, 23)

await robot.set_marker_and_eraser_up()
await robot.arc_left(-60, 23)
await robot.turn_left(90)
await robot.move(3)

# Vents
await robot.set_marker_down()
await robot.move(3)
await robot.move(-1.5)

await robot.set_marker_and_eraser_up()
await robot.turn_right(90)
await robot.move(6)
await robot.set_marker_down()
await robot.turn_right(135)
await robot.move(3)

await robot.set_marker_and_eraser_up()
await robot.turn_right(45)
await robot.move(8)
await robot.set_marker_down()
await robot.turn_right(45)
await robot.move(3)

await robot.set_marker_and_eraser_up()
await robot.move(-9)
await robot.turn_left(135)
await robot.move(4)

# Steam
await robot.set_marker_down()
await robot.turn_left(60)
await robot.arc_right(120, 2)
await robot.arc_left(120, 2)

await robot.set_marker_and_eraser_up()
await robot.turn_left(45)
await robot.move(7)
await robot.set_marker_down()
await robot.turn_left(135)
await robot.arc_right(120, 2)
await robot.arc_left(120, 2)

await robot.set_marker_and_eraser_up()
await robot.turn_left(30)
await robot.move(14)
await robot.set_marker_down()
await robot.turn_left(160)
await robot.arc_right(120, 2)
await robot.arc_left(120, 2)

# Drive away
await robot.set_marker_and_eraser_up()
await robot.move(24)

robot.play()
Loading

0 comments on commit 5c30d19

Please sign in to comment.