forked from Cozomo-Laboratory/Cozmo-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCube Chaser Lights.py
61 lines (49 loc) · 1.83 KB
/
Cube Chaser Lights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import asyncio
import sys
import cozmo
class BlinkyCube(cozmo.objects.LightCube):
'''Subclass LightCube and add a light-chaser effect.'''
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self._chaser = None
def start_light_chaser(self):
'''Cycles the lights around the cube with 1 corner lit up green,
changing to the next corner every 0.1 seconds.
'''
if self._chaser:
raise ValueError("Light chaser already running")
async def _chaser():
while True:
for i in range(4):
cols = [cozmo.lights.off_light] * 4
cols[i] = cozmo.lights.green_light
self.set_light_corners(*cols)
await asyncio.sleep(0.1, loop=self._loop)
self._chaser = asyncio.ensure_future(_chaser(), loop=self._loop)
def stop_light_chaser(self):
if self._chaser:
self._chaser.cancel()
self._chaser = None
# Make sure World knows how to instantiate the subclass
cozmo.world.World.light_cube_factory = BlinkyCube
def cozmo_program(robot: cozmo.robot.Robot):
cube = None
look_around = robot.start_behavior(cozmo.behavior.BehaviorTypes.LookAroundInPlace)
try:
cube = robot.world.wait_for_observed_light_cube(timeout=60)
except asyncio.TimeoutError:
print("Didn't find a cube :-(")
return
finally:
look_around.stop()
cube.start_light_chaser()
try:
print("Waiting for cube to be tapped")
cube.wait_for_tap(timeout=10)
print("Cube tapped")
except asyncio.TimeoutError:
print("No-one tapped our cube :-(")
finally:
cube.stop_light_chaser()
cube.set_lights_off()
cozmo.run_program(cozmo_program)