-
Notifications
You must be signed in to change notification settings - Fork 2
/
com.py
358 lines (315 loc) · 10.7 KB
/
com.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from threading import Thread, Lock
import os
import os.path
import time
import traceback
from serial import Serial, SerialException
legs=[None,None,None,None]
WAITMOVE_DST=[50.0]
####### COMPUTE ACTUATOR ORDERS ########
def to_linear_actuator_order(l):
"""
transform a list of elongation in an encoded msg (to send to tell_controler())
:param l: list of 3 elongations
:return: encoded msg
"""
k=[(c-0.45)*1000.0 for c in l] # m to mm
return "A%.2f#B%.2f#C%.2f#\n"%(k[2],k[0],k[1])
from subprocess import Popen,PIPE
import select
class SimuControler:
def __init__(self,id):
self.id=id
self.port="/dev/simu_%d"%(id)
self.process=Popen(['./verrin/simulator',str(id)],stdin=PIPE,stdout=PIPE)
def isOpen(self):
return self.process.poll()==None
def close(self):
self.process.kill()
self.process.wait()
def write(self,buffer):
self.process.stdin.write(buffer.encode())
self.process.stdin.flush()
def read(self,size):
r=select.select([self.process.stdout.fileno()],[],[],0.01)
if (len(r[0])>0):
s=self.process.stdout.read(size)
return s.decode()
return ''
def reset_input_buffer(self):
self.process.stdin.flush()
self.process.stdout.flush()
class FakeControler:
def __init__(self,id):
self.id=id
self.port="/dev/fake_%d"%(id)
self.response=''
self.last=time.time()
self.la=[{'enable':False,'position':0,'order':0},{'enable':False,'position':0,'order':0},{'enable':False,'position':0,'order':0}]
def isOpen(self):
return True
def close(self):
pass
def write(self,buffer):
if buffer.find('?')!=-1:
self.response+="id[%d]\r\n"%(self.id)
else:
d=buffer.split('#')
for o in d:
if len(o)>0:
i=ord(o[0])-ord('A')
if o[1:]!='_':
self.la[i]['enable']=True
self.la[i]['order']=float(o[1:])
else:
self.la[i]['enable']=False
#print "Fake receive: %s"%(buffer)
def read(self,size):
time.sleep(0.1)
if (time.time()-self.last)>0.2:
for l in self.la:
if l['enable']:
if abs(l['order']-l['position'])<1:
l['enable']=False
else:
direction=-(l['position'] - l['order']) / abs(l['position']-l['order'])
stength=min(32,abs(l['position']-l['order']))
l['position'] += direction * (time.time()-self.last)*stength
self.last=time.time()
self.response+="S %d;%lf;%lf;0 %d;%lf;%lf;0 %d;%lf;%lf;0 \r\n"%(self.la[0]['enable'],self.la[0]['position'],self.la[0]['order'],
self.la[1]['enable'],self.la[1]['position'],self.la[1]['order'],
self.la[2]['enable'],self.la[2]['position'],self.la[2]['order'])
msg=self.response[:size]
self.response = self.response[size:]
return msg
def reset_input_buffer(self):
pass
def read_id(s):
print("read id in : ",s)
i=s.find("id[")
if i==-1:
return -1
j=s[i+len("id[")]
return int(j)
find_mutex = Lock()
def find_controler():
if find_mutex.acquire(False)==False:
return
blacklist=[] # port already up and ready
for i in range(len(legs)):
if legs[i]!=None and legs[i].isOpen():
try:
#legs[i].write('?')
blacklist.append(legs[i].port)
except:
legs[i].close()
legs[i]=None
for i in range(20):
pname="/dev/ttyUSB%d"%i
print("try ",pname)
if pname in blacklist:
continue
if os.path.exists(pname)==False:
continue
try:
port=Serial(port=pname, baudrate=115200, timeout=0.2)
print('port ',pname,' opened')
except Exception as e:
traceback.print_exc()
continue
c=0
while port.isOpen()==False and c<10:
time.sleep(0.5)
c+=1
if c==10:
continue
time.sleep(1.5)
print('retrieve id...')
port.write("???")
time.sleep(1)
port.write("???")
t=0
j=-1
while t<10 and j==-1:
j=read_id(port.read(8000))
print("find id: ",j)
if j<0 or j>3:
continue
if legs[j]!=None:
legs[j].close()
print("set port ",port.port," for leg ",j)
legs[j]=port
for i in range(len(legs)):
if legs[i]==None:
print("ADD FAKE CONTROLER!")
legs[i]=SimuControler(i)
find_mutex.release()
def tell_controler(number,msg,w=0):
"""
Write encoded 'msg' in the 'number' controler
:param number: leg id
:param msg: encoded msg with 'to_linear_activator_order'
:param w: waiting time after telling the controler
:return:
"""
print('tell controler ',number,' : ',msg)
if legs[number]==None:
return
try:
legs[number].write(msg)
if w>0:
wait_move(number,w)
except Exception as e:
traceback.print_exc()
find_controler()
time.sleep(0.5)
tell_controler(number,msg,w)
def tell_controlers(V):
"""
Send the encoded msg to all controlers from a list a 12 elongations
:param V: list of 12 elongations
:return:
"""
for i in range(4):
tell_controler(i, to_linear_actuator_order([V[3 * i] / 1000, V[3 * i + 1] / 1000, V[3 * i + 2] / 1000]))
class ControlerHandler(Thread):
def __init__(self,id):
Thread.__init__(self)
self.id=id
self.indata=''
self.shouldStop=False
self.isSuspended=False
self.la=[{'enabled':False,'position':0,'target':0,'last_order':0,'time':0},
{'enabled':False,'position':0,'target':0,'last_order':0,'time':0},
{'enabled':False,'position':0,'target':0,'last_order':0,'time':0}]
def parse_msg(self,msg):
# if self.id==0:
# print "parse <<:"
# print self.indata
# print ">>"
if len(msg)<=0:
return
if msg[0]=='S':
d=msg.split()
if len(d)!=4:
#print "error in data: ",d
return
la=0
for v in d[1:]:
x=v.split(';')
if len(x)!=4:
print("error in data values: ",x)
return
if x[0]=='0':
self.la[la]['enabled']=False
else:
self.la[la]['enabled']=True
self.la[la]['position'] = float(x[1])
self.la[la]['target'] = float(x[2])
self.la[la]['last_order'] = float(x[3])
self.la[la]['time']=time.time()
la+=1
if la!=3:
print("missing actuator in status for ",self.id)
elif msg[0]=='D':
print("[Debug:",self.id,"][",msg,"]")
def print_actuators_status(self):
print(self.id,self.la)
def moving(self,dst):
for c in self.la:
if c['enabled'] and abs(c['target']-c['position'])>=dst:
return True
if (time.time()-c['time'])>2:
print("lost status for leg ",self.id)
return False
def stop(self):
self.shouldStop=True
def suspend(self,status):
if status==False: # purge pending data
legs[self.id].reset_input_buffer()
self.isSuspended=status
def run(self):
while self.shouldStop==False:
try:
if self.isSuspended:
time.sleep(0.5)
continue
if legs[self.id]==None:
print("no com")
time.sleep(2)
continue
c=legs[self.id].read(60)
if len(c)>0:
full_msg=''
for d in c:
if d=='\r':
if len(self.indata)>0:
full_msg=self.indata
self.indata=''
elif d!='\n':
self.indata+= str(d)
if full_msg!='':
self.parse_msg(full_msg)
except SerialException as e:
legs[self.id]=None
find_controler()
except Exception as e :
print("thread ",self.id," receive an exception:",e)
traceback.print_exc()
if legs[self.id]!=None:
legs[self.id].close()
def wait_move(cl,timeout,dst=None):
global WAITMOVE_DST
if dst==None:
dst=WAITMOVE_DST[0]
if type(cl)==int:
cl=(cl,)
n=time.time()
time.sleep(0.8) # time for order to go to physical controler and get back
done=False
while done==False:
done=True
for c in cl:
done=done and (controlers[c].moving(dst)==False)
print("done => ",done)
time.sleep(0.1)
if done==False and (time.time()-n)>timeout:
print("wait end by timeout")
for c in cl:
print("leg ",c," moving:",controlers[c].moving(dst))
if controlers[c].moving(dst):
for l in controlers[c].la:
print(l['enabled'],'/',abs(l['position']-l['target']),',', end=' ')
print()
# controlers[c].print_actuators_status()
return False
print("wait end as there is no move",dst,": ", end=' ')
for c in cl:
print(c,'moving:',controlers[c].moving(dst),'[', end=' ')
for l in controlers[c].la:
print(l['enabled'],'/',abs(l['position']-l['target']),',', end=' ')
print(']')
print()
return True
def upgrade_controlers(cl=None):
if cl==None:
cl=list(range(4))
for c in controlers:
c.suspend(True)
time.sleep(0.5)
for i in cl:
os.system("cd verrin && CONTROLER_ID=%d ARDUINO_PORT=%s make clean all upload"%(i,legs[i].port))
for c in controlers:
c.suspend(False)
def stop_all_actuators():
for i in range(4):
tell_controler(i,"A_#B_#C_#")
print("find controlers")
find_controler()
print("done")
print("run controler handlers")
controlers=[]
for i in range(4):
controlers.append(ControlerHandler(i))
controlers[-1].start()
print("done")