-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphero.py
128 lines (88 loc) · 3.21 KB
/
sphero.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from microbit import uart
class LEDs:
RIGHT_HEADLIGHT = [0x00, 0x00, 0x00, 0x07]
LEFT_HEADLIGHT = [0x00, 0x00, 0x00, 0x38]
LEFT_STATUS = [0x00, 0x00, 0x01, 0xC0]
RIGHT_STATUS = [0x00, 0x00, 0x0E, 0x00]
BATTERY_DOOR_FRONT = [0x00, 0x03, 0x80, 0x00]
BATTERY_DOOR_REAR = [0x00, 0x00, 0x70, 0x00]
POWER_BUTTON_FRONT = [0x00, 0x1C, 0x00, 0x00]
POWER_BUTTON_REAR = [0x00, 0xE0, 0x00, 0x00]
LEFT_BRAKELIGHT = [0x07, 0x00, 0x00, 0x00]
RIGHT_BRAKELIGHT = [0x38, 0x00, 0x00, 0x00]
class RawMotorModes:
OFF = 0
FORWARD = 1
BACKWARD = 2
class RVRDrive:
@staticmethod
def drive(speed, heading):
flags = 0x00
if speed < 0:
speed *= -1
heading += 180
heading %= 360
flags = 0x01
drive_data = [
0x8D, 0x3E, 0x12, 0x01, 0x16, 0x07, 0x00,
speed, heading >> 8, heading & 0xFF, flags
]
drive_data.extend([~((sum(drive_data) - 0x8D) % 256) & 0x00FF, 0xD8])
uart.write(bytearray(drive_data))
return
@staticmethod
def stop(heading):
RVRDrive.drive(0, heading)
return
@staticmethod
def set_raw_motors(left_mode, left_speed, right_mode, right_speed):
if left_mode < 0 or left_mode > 2:
left_mode = 0
if right_mode < 0 or right_mode > 2:
right_mode = 0
raw_motor_data = [
0x8D, 0x3E, 0x12, 0x01, 0x16, 0x01, 0x00,
left_mode, left_speed, right_mode, right_speed
]
raw_motor_data.extend([~((sum(raw_motor_data) - 0x8D) % 256) & 0x00FF, 0xD8])
uart.write(bytearray(raw_motor_data))
return
@staticmethod
def reset_yaw():
drive_data = [0x8D, 0x3E, 0x12, 0x01, 0x16, 0x06, 0x00]
drive_data.extend([~((sum(drive_data) - 0x8D) % 256) & 0x00FF, 0xD8])
uart.write(bytearray(drive_data))
return
class RVRLed:
@staticmethod
def set_all_leds(red, green, blue):
led_data = [
0x8D, 0x3E, 0x11, 0x01, 0x1A, 0x1A, 0x00,
0x3F, 0xFF, 0xFF, 0xFF
]
for _ in range (10):
led_data.extend([red, green, blue])
led_data.extend([~((sum(led_data) - 0x8D) % 256) & 0x00FF, 0xD8])
uart.write(bytearray(led_data))
return
@staticmethod
def set_rgb_led_by_index(index, red, green, blue):
led_data = [0x8D, 0x3E, 0x11, 0x01, 0x1A, 0x1A, 0x00]
led_data.extend(index)
led_data.extend([red, green, blue])
led_data.extend([~((sum(led_data) - 0x8D) % 256) & 0x00FF, 0xD8])
uart.write(bytearray(led_data))
return
class RVRPower:
@staticmethod
def wake():
power_data = [0x8D, 0x3E, 0x11, 0x01, 0x13, 0x0D, 0x00]
power_data.extend([~((sum(power_data) - 0x8D) % 256) & 0x00FF, 0xD8])
uart.write(bytearray(power_data))
return
@staticmethod
def sleep():
power_data = [0x8D, 0x3E, 0x11, 0x01, 0x13, 0x01, 0x00]
power_data.extend([~((sum(power_data) - 0x8D) % 256) & 0x00FF, 0xD8])
uart.write(bytearray(power_data))
return