-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunExperiment.py
541 lines (475 loc) · 16 KB
/
runExperiment.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/env python
import numpy as np
import os
import cv
import cv2
import csv
import time
import sys
from subprocess import call
from Tkinter import *
import Image
import ImageTk
import transformations
import threading
import abbSim
# Writen by Trevor Decker
#TODO add prompt for change of station location
#TODO update joints
#TODO finish simulated robot whith the same interface as the real one
#TODO add new log folder button
#TODO mutex shit should get ride of color change in image stream
#TODO verify timing of arm stuff
#TODO handle camera not being avilable
sys.path.insert(0, 'abb/open-abb-driver/abb_node/packages/abb_communications')
import abb
######################################################
# TODO Description of function
#
# TODO REQUIRES
#
# TODO ENSURES
######################################################
def get_position_curr(R):
position_curr = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
cartesian = R.get_cartesian()
position = cartesian[0]
quaternion = cartesian[1]
position_curr[0] = position[0];
position_curr[1] = position[1];
position_curr[2] = position[2];
position_curr[3] = quaternion[0];
position_curr[4] = quaternion[1];
position_curr[5] = quaternion[2];
position_curr[6] = quaternion[3];
euler = transformations.euler_from_quaternion(quaternion, axes='sxyz')
position_curr[7] = euler[0];
position_curr[8] = euler[1];
position_curr[9] = euler[2];
return position_curr
############################################################
# Description: ucaptures an image from camrea cap was inilized with,
# returns the iamge as python Image
#
# TODO REQUIERS
#
# TODO ENSURES
###########################################################
def getImage(cap):
ret, frame = cap.read();
filePATH = "tempTestFile_IDoNotNeed.jpg"
cv2.imwrite(filePATH,frame)
return Image.open(filePATH)
#############################################################
# Description: updates the values in the gui
#
# TODO REQUIERS
#
# TODO ENSURES
#############################################################
def updateGUI(R,gui,cap,joint_labels,position_labels):
global imLabel
joint_values = R.get_joints()
position_values = R.get_cartesian()
position = position_values[0]
quaternion = position_values[1]
position_labels[0].config(text = str(position[0]))
position_labels[1].config(text = str(position[1]));
position_labels[2].config(text = str(position[2]));
position_labels[3].config(text = str(quaternion[0]));
position_labels[4].config(text = str(quaternion[1]));
position_labels[5].config(text = str(quaternion[2]));
position_labels[6].config(text = str(quaternion[3]));
euler = transformations.euler_from_quaternion(quaternion, axes='sxyz')
position_labels[7].config(text = str(euler[0]));
position_labels[8].config(text = str(euler[1]));
position_labels[9].config(text = str(euler[2]));
for jointNum in xrange(0,6):
joint_labels[jointNum].config(text = str(joint_values[jointNum]))
newImage = getImage(cap)
photo = ImageTk.PhotoImage(newImage)
imLabel.image = photo
imLabel.configure(image=photo)
print ("done updateing the gui \n")
#############################################################
# Description: returns the new directory's relative path
#
# TODO REQUIERS
#
# TODO ENSURES
#############################################################
def updateIMG():
global mutex
while True:
# mutex.acquire()
img = ImageTk.PhotoImage(getImage(cap))
imLabel.configure(image = img)
imLabel.image = img
# mutex.release()
time.sleep(.05)
#############################################################
# Description: returns the new directory's relative path
#
# TODO REQUIERS
#
# TODO ENSURES
#############################################################
def moveTo(R,x,y,z,x_rot,y_rot,z_rot):
q = transformations.quaternion_from_euler(x_rot, y_rot, z_rot, axes='sxyz')
A = R.get_cartesian()
# q = A[1]
p = [x,y,z]
pose_to_send = [p,q]
R.set_cartesian(pose_to_send)
time.sleep(1)
#############################################################
# Description: returns the new directory's relative path
#
# TODO REQUIERS
#
# TODO ENSURES
#############################################################
def moveTo_q(R,x,y,z,q0,q1,q2,q3):
q = [q0,q1,q2,q3]
p = [x,y,z]
pose_to_send = [p,q]
R.set_cartesian(pose_to_send)
time.sleep(1)
#############################################################
# Description: returns the new directory's relative path
#
# TODO REQUIERS
#
# TODO ENSURES
#############################################################
def processImage(R,i,cap,directory):
global ImageId
global ImageId_lock
# ImageId_lock.acquire()
ImageId = ImageId + 1
# ImageId_lock.release()
cartesian = R.get_cartesian()
position = cartesian[0]
quad = cartesian[1]
x = position[0]
y = position[1]
z = position[2]
q_0 = quad[0]
q_1 = quad[1]
q_2 = quad[2]
q_3 = quad[3]
imgFile = directory+'/'+str(ImageId)+'.jpg'
txtFile = directory+'/'+str(ImageId)+'armState.txt'
text_file = open(txtFile,"w")
text_file.write("x,"+str(x) + "\n")
text_file.write("y,"+str(y) + "\n")
text_file.write("z,"+str(z) + "\n")
text_file.write("q_0,"+str(q_0) + "\n")
text_file.write("q_1,"+str(q_1) + "\n")
text_file.write("q_2,"+str(q_2) + "\n")
text_file.write("q_3,"+str(q_3) + "\n")
text_file.close()
#to flush buffer
for i in xrange(0,20):
print "caputre :"+str(i)+"\n"
ret, frame = cap.read();
cv2.imwrite(imgFile,frame)
# call(["./apriltags_demo",imgFile])
#############################################################
# Description: returns the new directory's relative path
#
# TODO REQUIERS
#
# TODO ENSURES
#############################################################
def newLogFolder():
if not os.path.exists('log_files'):
os.makedirs('log_files')
#creates a folder to store all of the images
directory = 'log_files/'+str(time.time())
if not os.path.exists(directory):
os.makedirs(directory)
return directory
##############################################################
# TODO Description of function
#
# TODO REQUIERS
#
# TODO ENSURES
##############################################################
def logCurrentState(R,directory):
station = -1
i = -1
processImage(R,i,cap,directory)
###############################################################
# TODO Description of function
#
# TODO REQUIERS
#
# TODO ENSURES
###############################################################
def collectData(R,directory):
global useRealRobot
global num_pictures_per_location
global cap;
directory = newLogFolder()
#TODO set capture resolution
with open('points.csv', 'rb') as csvfile:
rowreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
count = 0
for row in rowreader:
count=count+1
total = count
with open('points.csv','rb') as csvfile:
rowreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
count = 0
for row in rowreader:
count=count+1
thisRow = row[0].split(',')
print thisRow
x = float(thisRow[0])
y = float(thisRow[1])
z = float(thisRow[2])
q0 = float(thisRow[3])
q1 = float(thisRow[4])
q2 = float(thisRow[5])
q3 = float(thisRow[6])
# x_rot = float(thisRow[3])
# y_rot = float(thisRow[4])
# z_rot = float(thisRow[5])
# station = int(thisRow[6])
if(useRealRobot):
# moveTo(R,x,y,z,x_rot,y_rot,z_rot);
moveTo_q(R,x,y,z,q0,q1,q2,q3);
block_while_moving(R)
for i in xrange(0, num_pictures_per_location):
processImage(R,i,cap,directory)
###########################################################
# TODO Description of function
#
# TODO REQUIERS
#
# TODO ENSURES
###########################################################
def block_while_moving(R):
while True:
J0 = R.get_joints()
J1 = R.get_joints()
dj = 0.0
for i in xrange(0,6):
dj = dj + abs(J1[i] - J0[i])
print "delta joints:"+str(dj)
if dj < 0.5:
break;
############################################################
# TODO Description of function
#
# TODO REQUIERS
#
# TODO ENSURES
############################################################
def movePosition(R,changed_index,delta):
global gui
global cap
position_curr = get_position_curr(R)
position_curr[changed_index] += delta
quad = position_curr[3:7]
if(changed_index > 6):
x_rot = position_curr[7]
y_rot = position_curr[8]
z_rot = position_curr[9]
quad = transformations.quaternion_from_euler(x_rot, y_rot, z_rot, axes='sxyz')
position_curr[3] = quad[0]
position_curr[4] = quad[1]
position_curr[5] = quad[2]
position_curr[6] = quad[3]
#change format of position_curr to what abb needs
point = position_curr[0:3]
pose_to_send = [point,quad]
global mutex
print pose_to_send
R.set_cartesian(pose_to_send)
block_while_moving(R)
print "Done sending cartesian \n"
global joint_labels
global position_labels
updateGUI(R,gui,cap,joint_labels,position_labels)
##########################################################
# TODO Description of function
#
# TODO REQUIERS
#
# TODO ENSURES
##########################################################
def moveJoint(R,changed_index,delta):
global gui
global cap
global joint_labels
global position_labels
axis_curr = R.get_joints();
print axis_curr
axis_curr[changed_index] += delta
print "sending "+str(axis_curr)
global mutex
R.set_joints(axis_curr)
print "Done sending joints \n"
updateGUI(R,gui,cap,joint_labels,position_labels)
########################################################
# TODO Description of function
#
# TODO REQUIERS
#
# TODO ENSURES
########################################################
def addJointControl(R,buttons,axis_curr,jNum):
axis_label = Label(buttons,text="JOINT "+str(jNum))
up_btn = Button(buttons,text="<",command=lambda:moveJoint(R,jNum-1,-10.0))
curr_value = Label(buttons,text=str(axis_curr[jNum-1]))
down_btn = Button(buttons,text=">",command=lambda:moveJoint(R,jNum-1,10.0))
axis_label.grid(row=1,column=4*(jNum-1)+1)
up_btn.grid(row=2,column=4*(jNum-1)+0)
curr_value.grid(row=2,column=4*(jNum-1)+1)
down_btn.grid(row=2,column=4*(jNum-1)+2)
return curr_value
#######################################################
# TODO description of function
#
# REQUIERS:
# R = #TODO
# buttons = #TODO
# position_curr = #TODO
# name = #TODO
# row = #TODO
# col = #TODO
# i = #TODO
#
# ENSURES:
# return = label of the current value of the axis #TODO better description
########################################################
def addPositionControl(R,buttons,name,row,col,i,delta):
axis_label = Label(buttons,text=name)
up_btn = Button(buttons,text="<",command=lambda:movePosition(R,i-1,delta))
curr_value = Label(buttons,text="0")
down_btn = Button(buttons,text=">",command=lambda:movePosition(R,i-1,-delta))
axis_label.grid(row=row,column=4*(col-1)+1)
up_btn.grid(row=row+1,column=4*(col-1)+0)
curr_value.grid(row=row+1,column=4*(col-1)+1)
down_btn.grid(row=row+1,column=4*(col-1)+2)
return curr_value
#########################################################
# descriptiong: Go to home position, zero on all joints
#
# REQUIERS:
#
# ENSURES:
#
##########################################################
def MoveToZero(R):
global gui
global cap
global joint_labels
global position_labels
R.set_joints([0,0,0,0,0,0])
updateGUI(R,gui,cap,joint_labels,position_labels)
##########################################################
# TODO description of function
#
# REQUIERS:
#
# ENSURES:
##########################################################
def drawGUI(gui,R,cap,axis_curr,directory):
global imLabel
global joint_labels
global position_labels
image = getImage(cap)
photo = ImageTk.PhotoImage(image)
imLabel = Label(image=photo)
imLabel.image=photo
imLabel.grid()
t = threading.Thread(target=updateIMG)
t.daemon = True
t.start()
# photo = ImageTk.PhotoImage(image)
# label=Label(image=photo)
# label.image = photo
# label.
# cv2.imshow("cam n",frame)
joint_labels = [Label] * 6;
position_labels = [Label]*10;
buttons = Frame(gui)
for jointNum in xrange(1,7):
joint_labels[jointNum-1] = addJointControl(R,buttons,axis_curr,jointNum)
position_labels[0] = addPositionControl(R,buttons,"X",3,1,1,50.0)
position_labels[1] = addPositionControl(R,buttons,"Y",3,2,2,50.0)
position_labels[2] = addPositionControl(R,buttons,"Z",3,3,3,50.0)
position_labels[3] = addPositionControl(R,buttons,"q0",3,4,4,0.5)
position_labels[4] = addPositionControl(R,buttons,"q1",3,5,5,0.5)
position_labels[5] = addPositionControl(R,buttons,"q2",3,6,6,0.5)
position_labels[6] = addPositionControl(R,buttons,"q3",3,7,7,0.5)
position_labels[7] = addPositionControl(R,buttons,"roll",5,1,8,.125)
position_labels[8] = addPositionControl(R,buttons,"pitch",5,2,9,.125)
position_labels[9] = addPositionControl(R,buttons,"yaw",5,3,10,.125)
buttons.grid()
b = Button(buttons,text="Collect Data",command=lambda:collectData(R,directory))
b.grid(row=7,column=0)
# TODO does not work without global
# b1 = Button(buttons,text="new Log Folder",command=lambda:newLogFolder())
# b1.grid(row=5,column=1)
b2 = Button(buttons,text="log Current State",command=lambda:logCurrentState(R,directory))
b2.grid(row=7,column=2)
b3 = Button(buttons,text="zero",command=lambda:MoveToZero(R))
b3.grid(row=7,column=3)
#we want to get the actual values for the gui
updateGUI(R,gui,cap,joint_labels,position_labels)
#######################################################
# TODO Description of function
#
# TODO REQUIERS
#
# TODO ENSURES
#######################################################
def main():
global mutex
global ImageId_lock
global ImageId
# mutex = threading.Lock()
# ImageId_lock = threading.Lock()
ImageId = 0;
global useRealRobot
global num_pictures_per_location
global cap;
global gui
# global axis_curr
# global position_curr
# should we test the system or use the real robot?
# if false then do not try to connect to the real robot
# if true then do try to connect to to the real robot
useRealRobot = True;
#number of pictures to take at each location
num_pictures_per_location = 1;
#connect to the robot
print "attempting to connect to the robot arm\n"
if(useRealRobot):
print "Attempting to connect to the actual robot, if the program hangs \
make sure that the server for the robot is running. You can also run \
this program with a simulated robot \n"
R = abb.Robot(ip='192.168.125.1')
#Can possibly hang here if can not connect to the robot #TODO quit if time out
camId = 0;
else:
R = abbSim.Robot()
camId = 0;
print "sucssfully connected to robot the robot arm\n"
axis_curr = R.get_joints()
cap = cv2.VideoCapture(camId)
directory = newLogFolder();
position_curr = get_position_curr(R)
#seting up the gui
gui = Tk()
drawGUI(gui,R,cap,axis_curr,directory)
# otherButtons.grid(row=2,column=0) #side=TOP)
mainloop()
cap.release()
cv2.destroyAllWindows()
main();