-
Notifications
You must be signed in to change notification settings - Fork 0
/
calib_baxter_cam_retrieve.py
335 lines (270 loc) · 9.08 KB
/
calib_baxter_cam_retrieve.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
#!/usr/bin/env python
from __future__ import division
from orientation_help import VectorOrientation
import roslib
roslib.load_manifest('baxter_examples')
import sys
import rospy
import os
import cv
import cv2
import numpy
import cPickle
import baxter_interface
import baxter_interface.digital_io as DIO
import time
import sfml as sf
import math
from geometry_msgs.msg import (
PoseStamped,
Pose,
Point,
Quaternion,
)
from std_msgs.msg import Header
from baxter_core_msgs.srv import (
SolvePositionIK,
SolvePositionIKRequest,
)
from orientation_help import VectorOrientation
from ik_solve_baxter import IKSolveBaxter
from sub_image_help import SubImageHelp
from baxter_button_help import BaxterButtonHelp
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
img = []
image_corners = []
click_count = 0
baxter_endpoints = []
def handle_mouse(event, x, y, flags, param):
global img
global image_corners
global click_count
if event == cv2.EVENT_LBUTTONDBLCLK:
click_count += 1
cv2.circle(img,(x,y),5,(255,0,0), -1)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,str(click_count), ( (x+10),(y+10) ), font, 1,(255,255,255), 3, 8)
point = [x, y]
image_corners.append(point)
print image_corners
cv2.imshow('RetrieveImage', img)
def main():
global img
global image_corners
global click_count
global baxter_endpoints
rospy.init_node('RetrieveCamToolTableCalib', disable_signals=True)
# Record Baxter Coordinates
print
print "Record arm endpoints using Cuff Button and Big Button when 4 points are done (Left Arm)"
cam_arm = baxter_interface.limb.Limb("left")
bbhObj = BaxterButtonHelp("left")
baxter_endpoints = []
click_count = 0
while_flag = True
while(while_flag):
cButtonPressed = bbhObj.getButtonStates()['cState']
bButtonPressed = bbhObj.getButtonStates()['bState']
if(cButtonPressed):
pose = cam_arm.endpoint_pose()
xyz = pose["position"]
baxter_endpoints.append(xyz)
click_count += 1
time.sleep(1)
print "Baxter Arm EndPoint %s added" % (click_count)
if(bButtonPressed):
if (click_count == 4):
while_flag = False
print "Baxter Endpoints Recorded Recorded:"
print baxter_endpoints
elif (click_count < 4):
while_flag = True
baxter_endpoints = []
click_count = 0
print "Less than 4 points recorded. Please re-record all corners"
else:
while_flag = True
baxter_endpoints = []
click_count = 0
print "More than 4 points recorded. Please record only the corners"
time.sleep(1)
##############################################################
print "Storing Retrieval Position"
# Store retrieval position
print
print "Moving baxter cam arm to desired location image frame"
cam_arm_x = ((baxter_endpoints[0].x + baxter_endpoints[2].x) / 2) + 0 #As camera not at center
cam_arm_y = (baxter_endpoints[0].y + baxter_endpoints[2].y) / 2 + 0.003 #As camera is not at center
cam_arm_z = ((baxter_endpoints[0].z + baxter_endpoints[2].z) / 2) + 0 # Height above table
# All orientation calclulations
voObj = VectorOrientation()
voObj.setVectorChange(0.00, 0.00, -1.0) # X, Y and Z Vector Axis Orientation
#voObj.setAngleOrientation(-20, 0, 0, 'd')
voObj.calcVectorOrientation()
# Set gamma in degrees
voObj.setGamma(180, 'd')
# Trick for baxter orientation change for rigth arm:
voObj.setAngleOrientation(-voObj._theta, voObj._phi, voObj._gamma, 'r')
# Calculate quaternion
quat = voObj.getQuaternionOrientation()
# Inverse Kinematics
arm = IKSolveBaxter("left")
arm.setArmEndPos(cam_arm_x, cam_arm_y, cam_arm_z, quat[0], quat[1], quat[2], quat[3])
arm.ik_solve();
ret_joints = arm.getBaxterJointSpace()
bcajpFile = open("BaxterRetArmJointPosFile.dat", "w");
cPickle.dump(ret_joints, bcajpFile);
bcajpFile.close()
# Move Arm
control_arm = baxter_interface.limb.Limb("left")
control_arm.set_joint_position_speed(0.5) # Joint movement speed
control_arm.move_to_joint_positions(ret_joints, 5.0)
time.sleep(1)
##############################################################
# Move baxter arm
print
print "Moving baxter cam arm to desired location image frame"
cam_arm_x = ((baxter_endpoints[0].x + baxter_endpoints[2].x) / 2) + 0.075 #As camera not at center
cam_arm_y = (baxter_endpoints[0].y + baxter_endpoints[2].y) / 2 + 0.005 #As camera is not at center
cam_arm_z = ((baxter_endpoints[0].z + baxter_endpoints[2].z) / 2) + 0.15 # Height above table
# All orientation calclulations
voObj = VectorOrientation()
voObj.setVectorChange(0.00, 0.00, -1.0) # X, Y and Z Vector Axis Orientation
#voObj.setAngleOrientation(-20, 0, 0, 'd')
voObj.calcVectorOrientation()
# Set gamma in degrees
voObj.setGamma(180, 'd')
# Trick for baxter orientation change for rigth arm:
voObj.setAngleOrientation(-voObj._theta, voObj._phi, voObj._gamma, 'r')
# Calculate quaternion
quat = voObj.getQuaternionOrientation()
# Inverse Kinematics
arm = IKSolveBaxter("left")
arm.setArmEndPos(cam_arm_x, cam_arm_y, cam_arm_z, quat[0], quat[1], quat[2], quat[3])
arm.ik_solve();
joints = arm.getBaxterJointSpace()
# Move Arm
control_arm = baxter_interface.limb.Limb("left")
control_arm.set_joint_position_speed(0.5) # Joint movement speed
control_arm.move_to_joint_positions(joints, 5.0)
os.system("clear")
print "Press C to continue and store baxter camera arm position"
while(not sf.Keyboard.is_key_pressed(sf.Keyboard.C)):
pass
bcajpFile = open("RetrieveBaxterCamArmJointPosFile.dat", "w");
cPickle.dump(joints, bcajpFile);
bcajpFile.close()
print
print "Baxter Camera Arm Position Stored"
# Get image frame from baxter hand camera
print
print "Waiting for image..."
sihObj = SubImageHelp("/cameras/left_hand_camera/image")
error_flag = True
rate = rospy.Rate(1)
rate.sleep()
while error_flag == True:
try:
img = sihObj.getCVImage()
error_flag = False
print "BaxterImageObtained"
rate.sleep()
except:
print
print "Error in Obtaining Image"
# All recording points stuff
print
print "Record all 4 corner points using mouse double click and then press key to continue"
img_copy = img.copy()
while_flag = True
while(while_flag):
img = img_copy.copy()
click_count = 0
image_corners = []
cv2.namedWindow('RetrieveImage')
cv2.imshow('RetrieveImage', img)
cv2.setMouseCallback('RetrieveImage',handle_mouse)
cv2.waitKey(0)
if (click_count == 4):
while_flag = False
print "Image Corners Recorded"
print
print "The image corners are:"
print image_corners[0], image_corners[1], image_corners[2], image_corners[3]
elif (click_count < 4):
while_flag = True
print "Less than 4 points recorded. Please re-record all corners"
else:
while_flag = True
print "More than 4 points recorded. Please record only the corners"
#cv2.destroyAllWindows()
time.sleep(1)
X = numpy.matrix([
[image_corners[0][0], image_corners[2][0], image_corners[3][0]],
[image_corners[0][1], image_corners[2][1], image_corners[3][1]],
[1.0, 1.0, 1.0]
])
A = numpy.matrix([
[baxter_endpoints[0].x, baxter_endpoints[2].x, baxter_endpoints[3].x],
[baxter_endpoints[0].y, baxter_endpoints[2].y, baxter_endpoints[3].y],
[baxter_endpoints[0].z, baxter_endpoints[2].z, baxter_endpoints[3].z]
])
# Add Image Clip positions and Display Clipped Image
x1 = max( [image_corners[0][0], image_corners[3][0]] )
y1 = max( [image_corners[0][1], image_corners[1][1]] )
x2 = min( [image_corners[1][0], image_corners[2][0]] )
y2 = min( [image_corners[2][1], image_corners[3][1]] )
image_clip = [x1, y1, x2, y2]
img = img_copy[:, image_clip[0] : image_clip[2] ]
img = img[image_clip[1] : image_clip[3], :]
cv2.imwrite('RetrieveImage.png', img)
ImageClipPosFile = open("RetrieveImageClipPosFile.dat", "w");
cPickle.dump(image_clip, ImageClipPosFile);
ImageClipPosFile.close()
print "Image Clip Positions Added To File"
time.sleep(1)
print
print "Press C to continue and calculate tranformation matrix"
while(not sf.Keyboard.is_key_pressed(sf.Keyboard.C)):
pass
os.system("clear")
print "Image Corner Pixel Locations:"
print X
print
print "Baxter Endpoint Locations:"
print A
print
XAPosFile = open("RetrieveXAPosFile.dat", "w");
cPickle.dump(X, XAPosFile);
cPickle.dump(A, XAPosFile);
XAPosFile.close()
time.sleep(1)
print "X(ImageCorners) and A(BaxterEndpoints) Stored in XAPosFile.dat"
print
if(numpy.linalg.det(X) == 0):
print "Determiant of image corner matrix = 0"
print "Please retry calibration"
else:
T = numpy.matrix( numpy.dot( A, numpy.linalg.inv(X) ) )
print "Tranformation Matrix:"
print T
print
print "Writing to transformation matrix file..."
tmatFile = open("RetrieveTransformationMatrixFile.dat", "w");
cPickle.dump(T, tmatFile);
tmatFile.close()
print "Tranformation matrix added"
print
time.sleep(1)
print "Press C to continue and exit program"
while(not sf.Keyboard.is_key_pressed(sf.Keyboard.C)):
pass
rospy.signal_shutdown("Shutting down ROS Node to calculate transformation")
print
print "Exit Program"
print
return 0
if __name__ == '__main__':
sys.exit(main())