forked from dimitarcodes/bci_sphero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphero_test.py
59 lines (48 loc) · 1.67 KB
/
sphero_test.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
import asyncio
from random import randrange
from sphero_mini import SpheroMini
async def init(sphero):
await sphero.connect()
await sphero.wake()
await sphero.getAcknowledgement("wake")
print('Hello World!')
await sphero.roll(0,0)
for _ in range(25):
await sphero.setLEDColor(red=randrange(255), \
green=randrange(255), blue=randrange(255))
await sphero.getAcknowledgement("led")
async def do_a_shape(sphero, n_sides):
turn_angle = int(360/n_sides)
for i in range(n_sides):
await sphero.roll(speed=150,heading=(turn_angle*i))
await asyncio.sleep(1)
async def do_a_shape_corrected(sphero, n_sides):
turn_angle = int(360/n_sides)
for i in range(n_sides-1):
await sphero.roll(speed=150,heading=(turn_angle*i))
await asyncio.sleep(1)
if i == n_sides -1:
await sphero.roll(speed=50, heading = turn_angle*i -180)
await asyncio.sleep(0.2)
async def disconnect(sphero):
await sphero.disconnect()
print('Goodbye World!')
async def disco(sphero):
for _ in range(25):
await sphero.setLEDColor(red=randrange(255), \
green=randrange(255), blue=randrange(255))
await sphero.getAcknowledgement("led")
async def play():
address = (
"F7:DF:DC:63:DA:E4"
)
# connect to sphero mini
sphero_mini = SpheroMini(address)
await init(sphero_mini)
await do_a_shape(sphero_mini, 4)
await disco(sphero_mini)
await do_a_shape_corrected(sphero_mini, 4)
# close sphero connection
await disconnect(sphero_mini)
if __name__ == "__main__":
asyncio.run(play())