- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdroid.py
More file actions
Latest commit
177 lines (147 loc) · 6.16 KB
/
Copy pathdroid.py
File metadata and controls
177 lines (147 loc) · 6.16 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
importasyncio
fromtimeimportsleep
frombleakimportBleakScanner, BleakClient, BleakError
classDroid():
def__init__(self, profile):
print("Initializing")
self.disabledLeds=0x00
self.profile=profile
# assumes theta in degrees and r = 0 to 100 %
# returns a tuple of percentages: (left_thrust, right_thrust)
def__throttle_angle_to_thrust__(r, theta):
theta= ((theta+180) %360) -180# normalize value to [-180, 180)
r=min(max(0, r), 100) # normalize value to [0, 100]
v_a=r* (45-theta%90) /45# falloff of main motor
v_b=min(100, 2*r+v_a, 2*r-v_a) # compensation of other motor
iftheta<-90: return-v_b, -v_a
iftheta<0: return-v_a, v_b
iftheta<90: returnv_b, v_a
returnv_a, -v_b
asyncdefconnect(self):
timeout=0.0
print("Connecting")
self.droid=BleakClient(self.profile)
awaitself.droid.connect()
whilenotself.droid.is_connectedandtimeout<10:
sleep (.1)
timeout+=.1
print ("Connected!")
connectCode=bytearray.fromhex("222001")
awaitself.droid.write_gatt_char(0x000d, connectCode, False)
awaitself.droid.write_gatt_char(0x000d, connectCode, False)
print("Locked")
light_blink=bytearray.fromhex("2C000449020001ff01ff0aff00")
awaitself.droid.write_gatt_char(0x000d, light_blink)
connect_sound=bytearray.fromhex("25000c421102")
awaitself.droid.write_gatt_char(0x000d, connect_sound)
sleep(3)
asyncdefdisconnect(self):
print ("Disconnecting")
try:
soundBank=bytearray.fromhex("27420f4444001f09")
awaitself.droid.write_gatt_char(0x000d, soundBank)
soundSelection=bytearray.fromhex("27420f4444001800")
awaitself.droid.write_gatt_char(0x000d, soundSelection)
sleep(3)
finally:
awaitself.droid.disconnect()
print("Disconnected")
asyncdefled_disable_sound(self, leds):
ledDisableCommand=bytearray.fromhex(f"27420f4444004a{leds}")
awaitself.droid.write_gatt_char(0x000d, ledDisableCommand)
self.disabledLeds=self.disabledLeds|int(leds, 16)
print(self.disabledLeds)
asyncdefled_enable_sound(self, leds):
ledEnableCommand=bytearray.fromhex(f"27420f4444004b{leds}")
awaitself.droid.write_gatt_char(0x000d, ledEnableCommand)
self.disabledLeds=self.disabledLeds-(int(leds, 16)&self.disabledLeds)
print(self.disabledLeds)
asyncdefled_flash(self, leds, duration):
pass
asyncdefled_off(self, leds):
ledOffCommand=bytearray.fromhex( f"27420f44440049{leds}" )
awaitself.droid.write_gatt_char(0x000d, ledOffCommand)
print(f"{self.disabledLeds:02x}")
print((f"{(~self.disabledLeds&0x1F):02x}"))
awaitself.led_enable_sound(f"{(~self.disabledLeds&0x1F):02x}")
asyncdefled_on(self, leds):
ledOnCommand=bytearray.fromhex(f"27420f44440048{leds}")
awaitself.droid.write_gatt_char(0x000d, ledOnCommand)
defmove (self, degrees, duration):
thrust=self.__throttle_angle_to_thrust__(degrees)
asyncdefplay_sound(self, sound_id=None, bank_id=None, cycle=False, volume=None):
ifvolume:
self.set_volume(volume)
ifbank_idand (nothasattr(self, "soundbank") orself.soundbank!=bank_id):
awaitself.set_soundbank(bank_id)
ifsound_id:
soundSelection=bytearray.fromhex("27420f44440018{}".format(sound_id))
elifcycle:
soundSelection=bytearray.fromhex("26420f4344001c")
else:
soundSelection=bytearray.fromhex("27420f44440010{}".format(self.bank_id))
awaitself.droid.write_gatt_char(0x000d, soundSelection)
asyncdefrun_routine(self, routineId):
full_id=bytearray.fromhex("25000c42{}02".format(routineId))
awaitself.droid.write_gatt_char(0x000d, full_id)
asyncdefset_soundbank(self, bank_id):
self.soundbank=bank_id
soundBank=bytearray.fromhex("27420f4444001f{}".format(bank_id))
awaitself.droid.write_gatt_char(0x000d, soundBank)
asyncdefset_volume(self, volume):
volume_command=bytearray.fromhex("27420f4444000e{}".format(volume))
awaitself.droid.write_gatt_char(0x000d, volume_command)
deffindDroid(candidate, data):
ifcandidate.name=="DROID":
returnTrue
else:
returnFalse
asyncdefdiscoverDroid(retry=False):
myDroid=None
whileretryandmyDroidisNone:
try:
myDroid=awaitBleakScanner.find_device_by_filter(findDroid)
ifmyDroidisNone:
ifnotretry:
print("Droid discovery timed out.")
return
else:
print("Droid discovery timed out. Retrying...")
continue
exceptBleakErroraserr:
print("Droid discovery failed. Retrying...")
continue
print (f"Astromech successfully discovered: [ {myDroid} ]")
d=Droid(myDroid)
returnd
asyncdefmain():
d=awaitdiscoverDroid(retry=True)
try:
awaitd.connect()
sleep (3)
# await arms.run_routine("05")
# sleep (5)
# await arms.set_soundbank("05")
# await arms.play_sound("00")
# sleep (5)
# for i in range(5):
# await arms.play_sound(cycle=True)
# sleep(5)
# await arms.play_sound("00", "00")
# sleep(8)
awaitd.led_disable_sound("01")
awaitd.play_sound("00", "00")
sleep(10)
awaitd.led_on("1f")
sleep(10)
awaitd.led_off("1f")
awaitd.play_sound("00", "00")
sleep(10)
exceptOSErroraserr:
print(f"Discovery failed due to operating system: {err}")
exceptBleakErroraserr:
print(f"Discovery failed due to Bleak: {err}")
finally:
awaitd.disconnect()
if__name__=="__main__":
asyncio.run(main())