-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapitor_bluepy.py
executable file
·329 lines (288 loc) · 11.7 KB
/
apitor_bluepy.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/python3
import sys
import struct
from time import sleep
from binascii import hexlify,unhexlify
import threading
import lupa
from lupa import LuaRuntime
from bluepy import btle
nordic_uart_service_uuid = btle.UUID('6e400001-b5a3-f393-e0a9-e50e24dcca9e')
nordic_uart_tx_uuid = btle.UUID('6e400002-b5a3-f393-e0a9-e50e24dcca9e') #(tx on bot side notify gives replies)
nordic_uart_rx_uuid = btle.UUID('6e400003-b5a3-f393-e0a9-e50e24dcca9e')
apitor_led_colors={0:"Off",1:"Red",2:"Orange",3:"Yellow",4:"Green",5:"Cyan",6:"Blue",7:"Violett",8:"Off"}
apitor_motor_directions={-1:"Backwards",0:"Stopped",1:"Forwards"}
apitor_example_script="T = 0\nwhile true do\n if T == 0 then\n if (GD(1)) <= 5 then\n M(1,9,-1)\n L(0,4)\n DS(20)\n MS(0)\n T = 1\n end\n end\n if T == 1 then\n if (GD(1)) > 5 then\n DS(30)\n M(1,6,1)\n L(0,1)\n DS(20)\n MS(0)\n T = 0\n end\n end\nend\n"
class UartRxDelegate(btle.DefaultDelegate):
def __init__(self,device):
btle.DefaultDelegate.__init__(self)
self.dev=device
def handleNotification(self, cHandle, data):
self.dev.handle_data(data)
class ApitorMotorState():
def __init__(self):
self.speed=0
#self.direction=0
def set_speed(self,speed,direction=None):
self.speed=speed
if(direction!=None):
self.speed=self.speed*direction
def get_speed(self):
return(self.speed)
def get_direction(self):
if(self.speed==0):
return(0)
elif(self.speed>0):
return(1)
elif(self.speed<0):
return(-1)
class ApitorLedState():
def __init__(self):
self.color=0
def set_color(self,color):
self.color=color
def get_color(self):
return(self.color)
#self.=0
class ApitorDistanceSensorState():
def __init__(self):
self.distance=0
def get_distance(self):
return(self.distance)
class ApitorState():
def __init__(self):
self.motors=[ApitorMotorState(),ApitorMotorState()]
self.leds=[ApitorLedState(),ApitorLedState(),ApitorLedState(),ApitorLedState()]
self.distance_sensors=[ApitorDistanceSensorState(),ApitorDistanceSensorState()]
self.battery_level=0
#self.script_stored=False
self.mode=0
def get_battery_level(self):
return(self.battery_level)
#def has_script_stored(self):
# return(self.script_stored)
class ApitorDevice():
def __init__(self,bd_addr):
self.bd_addr=bd_addr
self.max_retries=3
self.connected=False
self.uid=[]
self._lock=threading.Lock()
self.state=ApitorState()
self.notify_thread=None
def connect(self,bd_addr=None):
if(bd_addr!=None):
self.bd_addr=bd_addr
retries=self.max_retries
self.connected=False
self.dev=None
while(retries>0):
print("connecting device...")
try:
self.dev=btle.Peripheral(self.bd_addr,"random")
except btle.BTLEDisconnectError as err:
print("retries left:",retries,"(",str(err),")")
self.dev=None
retries-=1
if(self.dev!=None):
break
if(retries>0):
self.dev.setDelegate(UartRxDelegate(self))
self.uart_service=self.dev.getServiceByUUID(nordic_uart_service_uuid)
self.uart_tx=self.uart_service.getCharacteristics(nordic_uart_tx_uuid)[0]
self.uart_rx=self.uart_service.getCharacteristics(nordic_uart_rx_uuid)[0]
self.uart_tx_handle=self.uart_tx.getHandle()
self.uart_rx_handle=self.uart_rx.getHandle()
self.uart_rx_ccc=None
for desriptor in self.dev.getDescriptors(self.uart_rx_handle,0xFFFF): # The handle range should be read from the services
if(desriptor.uuid == 0x2902): # but is not done due to a Bluez/BluePy bug :(
#print("rx Client Characteristic Configuration found at handle 0x"+ format(desriptor.handle,"02X"))
self.uart_rx_ccc=desriptor.handle
if(self.uart_rx_ccc!=None):
self.dev.writeCharacteristic(self.uart_rx_ccc, struct.pack('<bb', 0x01, 0x00))
#print("Notification is turned on for rx")
print("device connected.")
self.connected=True
else:
print("device not connected.")
def get_state(self):
return(self.state)
def update_uid(self):
# cmd in hex = "fffe0104fdfc"
cmd=[-1, -2, 1, 4, -3, -4]
self.send_data(struct.pack('<6b',*cmd))
def update_state(self):
#dev.writeCharacteristic(uart_tx_handle, unhexlify("fffe09010200050004040404fdfc"))
#"fffe09010200000000000000fdfc"
cmd=[-1, -2, 9, 1, 2, self.state.motors[0].get_speed(), self.state.motors[1].get_speed(), 0, self.state.leds[0].get_color(), self.state.leds[1].get_color(), self.state.leds[2].get_color(), self.state.leds[3].get_color(), -3, -4];
self.send_data(struct.pack('<14b',*cmd))
def upload_script(self,lua_script):
#"fffe09020009000000000000fdfc"
lua_script_len=len(lua_script)
cmd=[-1, -2, 9, 2, lua_script_len>>8 , lua_script_len&0xFF, 0, 0, 0, 0, 0, 0, -3, -4];
#self.send_data(struct.pack('<14b',*cmd))
self.send_data(struct.pack('<4b2B8b',*cmd))
#sleep(0.1)
self.send_data(bytes(lua_script,"ascii"))
def test(self):
cmd=[-1, -2, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, -3, -4];
self.send_data(struct.pack('<14b',*cmd))
#cmd=[-1, -2, -1, 4, -3, -4]
#self.send_data(struct.pack('<6b',*cmd))
def send_data(self,data):
#with self._lock:
#self.dev.writeCharacteristic(self.uart_tx_handle,data,withResponse=True)
#print("sending data:",data,flush=True)
self.dev.writeCharacteristic(self.uart_tx_handle,data)
#print("sending data done",flush=True)
def handle_data(self,data):
#print("handle data in device class:",hexlify(data)," length:",len(data))
if(len(data)==11):
sdata = struct.unpack('11b',data)
if(sdata[0]==-1 and sdata[1]==-2 and sdata[9]==-3 and sdata[10]==-4):
#print("frame detected")
if(sdata[2]==6):
self.state.battery_level=data[5]
self.state.mode=data[3]
self.state.distance_sensors[0].distance=sdata[7]
self.state.distance_sensors[1].distance=sdata[8]
#print("state update received:",self.state.battery_level,self.state.distance_sensors[0].distance,self.state.distance_sensors[1].distance,self.state.mode)
elif(len(data)==13):
sdata = struct.unpack('13b',data)
if(sdata[0]==-1 and sdata[1]==-2 and sdata[11]==-3 and sdata[12]==-4):
#print("frame detected")
if(sdata[2]==8):
self.uid=data[3:11]
print("uid update received:",hexlify(self.uid))
else:
print("handle data in device class:",hexlify(data)," length:",len(data))
def run_loop(self):
while True:
#with self._lock:
# if(self.dev.waitForNotifications(None)):
# continue
#sleep(0.1)
if(self.dev.waitForNotifications(0.1)):
self.update_state()
continue
#print("Waiting...")
def run(self):
self.notify_thread = threading.Thread(target=self.run_loop, args=())
self.notify_thread.start()
def stop(self):
if(self.notify_thread!=None):
self.notify_thread.stop()
self.notify_thread=None
apitor_example_script2='''
T = 0
while true do
if T == 0 then
if (GD(1)) <= 5 then
M(1,9,-1)
L(0,4)
DS(20)
MS(0)
T = 1
end
end
if T == 1 then
if (GD(1)) > 5 then
DS(30)
M(1,6,1)
L(0,1)
DS(20)
MS(0)
T = 0
end
end
end
'''
class ApitorScript():
def __init__(self,device=None,lua_script=None):
self.lua = LuaRuntime(unpack_returned_tuples=True)
self.device=device
if(lua_script!=None):
self.lua_script=lua_script
else:
self.lua_script=""
self.lua.globals()["M"]=self.motor_set
self.lua.globals()["L"]=self.led_set
self.lua.globals()["GD"]=self.get_distance
self.lua.globals()["DS"]=self.device_sleep
self.lua.globals()["MS"]=self.motor_stop
self.script_thread=None
def motor_set(self,motor_id,speed,direction):
print("setting motor from lua:",motor_id,speed,direction,flush=True)
if(motor_id==0 or motor_id==1):
self.device.state.motors[0].set_speed(speed,direction)
if(motor_id==0 or motor_id==2):
self.device.state.motors[1].set_speed(speed,direction)
#self.device.update_state()
#sleep(0.1)
def motor_stop(self,motor_id):
print("stopping motor from lua:",motor_id,flush=True)
if(motor_id==0 or motor_id==1):
self.device.state.motors[0].set_speed(0,0)
if(motor_id==0 or motor_id==2):
self.device.state.motors[1].set_speed(0,0)
#self.device.update_state()
#sleep(0.1)
def led_set(self,led_id,color):
print("setting led from lua:",led_id,color,flush=True)
if(led_id==0 or led_id==1):
self.device.state.leds[0].set_color(color)
if(led_id==0 or led_id==2):
self.device.state.leds[1].set_color(color)
if(led_id==0 or led_id==3):
self.device.state.leds[2].set_color(color)
if(led_id==0 or led_id==4):
self.device.state.leds[3].set_color(color)
#self.device.update_state()
#sleep(0.1)
def get_distance(self,distance_sensor_id):
#print("getting distance from lua:",distance_sensor_id,self.device.state.distance_sensors[0].get_distance(),self.device.state.distance_sensors[1].get_distance())
if(distance_sensor_id==1):
return(self.device.state.distance_sensors[0].get_distance())
if(distance_sensor_id==2):
return(self.device.state.distance_sensors[1].get_distance())
#sleep(0.1)
def device_sleep(self,ms):
print("sleeping from lua:",ms/10,flush=True)
sleep(ms/10)
print("sleep done",flush=True)
def set_script(self,lua_script):
self.lua_script=lua_script
def set_device(self,device):
self.device=device
def run_script(self,lua_script=None,apitor_device=None):
if(lua_script!=None):
self.lua_script=lua_script
if(apitor_device!=None):
self.device=apitor_device
print("executing lua script")
ret=self.lua.execute(self.lua_script)
def run(self,lua_script=None,apitor_device=None):
self.script_thread = threading.Thread(target=self.run_script, args=(lua_script,apitor_device))
self.script_thread.start()
def stop(self):
if(self.script_thread!=None):
self.script_thread.stop()
self.script_thread=None
if(__name__ == '__main__'):
if(len(sys.argv)>=2):
bd_addr = sys.argv[1]
else:
bd_addr = "F7:B8:99:22:86:B4"
apitor=ApitorDevice(bd_addr)
apitor.connect()
apitor.update_uid()
#apitor.upload_script("M(2,5,1)\nM(1,5,1)\n")
#apitor.upload_script("")
#apitor.set_motor(1,5,0)
#apitor.state.leds[0].set_color(2)
#apitor.update_state()
apitor_script=ApitorScript(apitor,apitor_example_script2)#"T = 1\nL(T,4)")
apitor.run()
apitor_script.run()
#apitor.test()
#apitor.run_loop()