-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphero_helper.py
52 lines (38 loc) · 1.4 KB
/
sphero_helper.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
import os
from sphero_driver import sphero_driver
class SpheroException(Exception):
pass
# currently not used
# turns Spheros off completely (no blinking)
def turn_off_spheros(spheros):
for s in spheros:
s.go_to_sleep(0, 0, False)
# send final commands with speed=0
def stop_spheros(spheros):
for s in spheros:
try:
s.roll(0, 0, 1, False)
except AttributeError:
continue
for cb in {'PWR_NOTIFY', 'COLLISION', 'DATA_STRM'}:
try:
s.remove_async_callback(sphero_driver.IDCODE[cb])
except KeyError:
continue
# close existing Bluetooth connections (blinking)
def disconnect_spheros(spheros):
for s in spheros:
try:
s.disconnect()
except AttributeError:
continue
# if a Sphero's battery write message to console
def power_notify_function(callback, sphero_number):
if callback == 3:
# raise SpheroException("Sphero " + str(sphero_number + 1) + ": battery low.")
print "[WARN] Sphero " + str(sphero_number + 1) + ": battery low.\n"
# if collision detected, write message to console
def collision_function(callback, sphero_number):
speed = callback.get('Speed')
if speed > 0 and os.path.exists("commandsToRobots.txt"):
print "[WARN] Sphero " + str(sphero_number + 1) + ": collision detected: speed = " + str(speed) + " cm/s\n"