-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArmPlannerPR2.py
382 lines (311 loc) · 13.1 KB
/
ArmPlannerPR2.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
from IKPlannerFunctions import IKInterpolationPlanner
from brett2.PR2 import PR2, Arm, IKFail
import rospy
import openravepy as opr
import numpy as np
import numpy.linalg as nla
class PlannerArm(Arm):
"""
Planner class for the Arm.
"""
def __init__ (self, pr2, rl):
Arm.__init__(self, pr2, rl)
self.planner = IKInterpolationPlanner(self, self.lr)
def goInDirection (self, d, dist, steps=10):
"""
Moves the tool tip in the specified direction in the gripper frame.
Direction of movement -> d
f -> forward (along the tip of the gripper)
b -> backward
u -> up
d -> down
l -> left
r -> right
Distance traveled by tool tip -> dist
Number of points of linear interpolation -> steps
"""
self.pr2.update_rave()
trajectory = self.planner.goInDirection(d, dist,steps)
if trajectory:
self.follow_joint_trajectory (trajectory)
else: raise IKFail
def goInWorldDirection (self, d, dist, steps=10):
"""
Moves the tool tip in the specified direction in the base_footprint frame.
Direction of movement -> d
f -> forward
b -> backward
u -> up
d -> down
l -> left
r -> right
Distance traveled by tool tip -> dist
Number of points of linear interpolation -> steps
"""
self.pr2.update_rave()
trajectory = self.planner.goInWorldDirection(d, dist,steps)
if trajectory:
self.follow_joint_trajectory (trajectory)
else: raise IKFail
def circleAroundRadius (self, pose, rad, finAng, steps=10):
"""
Moves the gripper in a circle.
Pose of needle in hand (as given below in the
function ArmPlannerPR2.moveNeedleToGripperPose) -> pose
Radius of circle -> rad
Final angle covered by circle -> finAng
Number of points of linear interpolation of angle -> steps
"""
self.pr2.update_rave()
if pose not in [1,2,3,4]:
rospy.logwarn ("Invalid option for pose.")
return
if self.lr == 'r':
pose = {1:2, 2:1, 3:4, 4:3}[pose]
d,t = {1:(-1,-1), 2:(1,1), 3:(-1,1), 4:(1,-1)}[pose]
trajectory = self.planner.circleAroundRadius (d, t, rad, finAng, steps)
if trajectory:
self.follow_joint_trajectory (trajectory)
else: raise IKFail
def goto_pose_matrix (self, matrix4, ref_frame, targ_frame, filter_options=-1):
"""
Moves the arm such that the transform between ref_frame and
targ_frame is matrix4. Also uses filter_options as specified
in self.planner (by default).
"""
if filter_options==-1:
filter_options = self.planner.filter_options
Arm.goto_pose_matrix(self, matrix4, ref_frame, targ_frame, filter_options)
def goto_pose_matrix_rave (self, matrix4, ref_frame, targ_frame, filter_options=-1):
"""
Moves the arm in openrave only. This is only for planning. Please be very careful when
doing this and make sure to update rave properly back to the real PR2 after finishing.
"""
if filter_options==-1:
filter_options = self.planner.filter_options
joints = self.cart_to_joint(matrix4, ref_frame, targ_frame, filter_options)
if joints is not None: self.pr2.robot.SetJointValues(joints, self.manip.GetArmIndices())
else: raise IKFail
def cart_to_joint(self, matrix4, ref_frame, targ_frame, filter_options=-1):
"""
Calculates IK for given transform between ref_frame and targ_frame.
"""
if filter_options==-1:
filter_options = self.planner.filter_options
return Arm.cart_to_joint(self, matrix4, ref_frame, targ_frame, filter_options)
class PlannerPR2 (PR2):
"""
Planner class for PR2 with planning arms.
"""
def __init__ (self,initPos=None):
PR2.__init__(self)
self.rarm = PlannerArm(self,'r')
self.larm = PlannerArm(self,'l')
# In case needle is not added
self.sneedle = None
self.sneedle_radius = 0.112
self.sneedle_pose = 0
self.grabbingArm = None
# In case sponge is not added (supporting sponge)
self.sponge = None
self.sponge_enabled = False
if initPos is not None:
try:
self.gotoArmPosture(initPos)
except:
rospy.logwarn ("Cannot go to pose " + str(initPos))
self.addTableToRave()
self.addSpongeToRave()
self.addNeedleToRave()
def gotoArmPosture (self, pos):
"""
Makes both arms go to the specified posture.
"""
self.larm.goto_posture(pos)
self.rarm.goto_posture(pos)
self.join_all()
def addTableToRave (self):
"""
Adds a box of predefined position/ half-extents to
the rave Environment.
"""
tablePos = [0.75,0,0.72]
tableHalfExtents = [0.5,0.45,0.05]
with self.env:
body = opr.RaveCreateKinBody(self.env,'')
body.SetName('table')
body.InitFromBoxes(np.array([tablePos + tableHalfExtents]),True)
self.env.AddKinBody(body,True)
def addSpongeToRave (self):
"""
Adds a sponge to account for the supporting sponge of the foam block.
"""
spongePos = [0.5,0,0.8]
spongeHalfExtents = [0.25,0.3,0.03]
with self.env:
self.sponge = opr.RaveCreateKinBody(self.env,'')
self.sponge.SetName('table')
self.sponge.InitFromBoxes(np.array([spongePos + spongeHalfExtents]),True)
self.env.AddKinBody(self.sponge,True)
self.enableSponge(False)
def enableSponge (self, enable):
"""
enable -> True/False
Enables or disables sponge in the environment.
Consider when motion planning needs to account for sponge collision.
"""
if self.sponge is None:
self.sponge_enabled = False
return
if enable:
self.sponge_enabled = True
self.sponge.Enable(True)
rospy.loginfo('Supporting sponge enabled for collision checking.')
else:
self.sponge_enabled = False
self.sponge.Enable(False)
rospy.loginfo('Supporting sponge disabled for collision checking.')
def addNeedleToRave (self):
"""
Adds the suturing needle from the .dae file.
Needle is kept at the edge of the table to start with.
"""
with self.env:
self.sneedle = self.env.ReadKinBodyURI('/home/ankush/sandbox/bulletsim/data/needle/sneedle.dae')
self.sneedle.SetName('sneedle')
self.env.AddKinBody(self.sneedle)
self.resetNeedlePose()
def resetNeedlePose (self):
"""
Reset needle transform to be at the edge of the table
"""
if self.sneedle is None:
return
sndTfm = np.eye(4)
sndTfm[0:3,3] = np.array([1.1,-0.11,0.78])
self.sneedle.SetTransform(sndTfm)
def moveNeedleToGripperPose (self, rl='l', pose=1):
"""
Resets needle to different poses. All explanations below assume that
the arm is in the "side" position.
Positions for right arm are opposite (in terms of left and right) to
the following.
Pose 1 - needle is in front of the gripper and pointing away.
Pose 2 - needle is in front of the gripper and pointing inward.
Pose 3 - needle is behind the gripper and pointing away.
Pose 4 - needle is behind the gripper and pointing inward.
Pose 1 <=> Pose 4 and Pose 2 <=> Pose 3.
"""
if self.sneedle is None:
return
if rl == 'r' and pose in [1,2,3,4]:
pose = {1:2, 2:1, 3:4, 4:3}[pose]
arm = {'l':self.larm, 'r':self.rarm}[rl]
WfromEE = arm.manip.GetTransform()
trans = np.eye(4)
trans[[0,1],[3,3]] = np.array([-0.07,-0.05])
if pose == 1:
rot = np.eye(4)
ind1, ind2 = [0,0,1,1], [0,1,0,1]
theta = np.pi/6
rot[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
elif pose == 2:
rot1 = np.eye(4)
ind1, ind2 = [1,1,2,2], [1,2,1,2]
theta = np.pi
rot1[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
rot2 = np.eye(4)
ind1, ind2 = [0,0,1,1], [0,1,0,1]
theta = np.pi/6
rot2[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
rot = rot1.dot(rot2)
elif pose == 3:
rot1 = np.eye(4)
ind1, ind2 = [0,0,2,2], [0,2,0,2]
theta = np.pi
rot1[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
rot2 = np.eye(4)
ind1, ind2 = [0,0,1,1], [0,1,0,1]
theta = np.pi/6
rot2[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
rot = rot1.dot(rot2)
elif pose == 4:
rot = np.eye(4)
ind1, ind2 = [0,0,1,1], [0,1,0,1]
theta = np.pi/6 + np.pi
rot[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
else:
rospy.logwarn ("Unknown needle pose. Not setting any transform.")
return
sndTfm = WfromEE.dot(rot.dot(trans))
self.sneedle.SetTransform(sndTfm)
def grabNeedle (self, rl='l', pose=1):
"""
Grabs the needle with the specified gripper and specified pose.
Pose descriptions are given in function moveNeedleToGripperPose.
Does not check for collisions on doing so.
"""
if self.sneedle is None:
return
if pose not in [1,2,3,4]:
rospy.logwarn ("Unknown needle pose. Not grabbing needle.")
return
self.moveNeedleToGripperPose(rl,pose)
oldManip = self.robot.GetActiveManipulator()
manip = {'r':self.rarm.manip, 'l':self.larm.manip}[rl]
self.robot.SetActiveManipulator(manip)
grabbed = self.robot.Grab(self.sneedle)
if grabbed is False:
rospy.logwarn("Unable to grab the needle")
self.resetNeedlePose()
self.sneedle_pose = pose
self.grabbingArm = {'r':self.rarm, 'l':self.larm}[rl]
self.robot.SetActiveManipulator(oldManip)
def releaseNeedle (self):
"""
Releases the needle if it is grabbed. Returns needle to reset position.
"""
if self.sneedle is None:
return
self.robot.Release(self.sneedle)
self.resetNeedlePose()
self.sneedle_pose = 0
self.grabbingArm = None
def needleTipTransform (self):
"""
Returns the transform of the needle tip.
Z is pointing out of the tip and X is pointing out of the plane.
"""
needleTfm = self.sneedle.GetTransform()
handleTrans = np.eye(4)
handleTrans[[0,1],[3,3]] = np.array([0.07,0.05])
handleRot = np.eye(4)
ind1, ind2 = [0,0,1,1], [0,1,0,1]
theta = -np.pi/6
handleRot[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
handleTfm = handleTrans.dot(handleRot)
# Values found offline
tipTrans = np.eye(4)
tipTrans[[0,1],[3,3]] = np.array([-0.057,0.202])
tipRot = np.eye(4)
ind1, ind2 = [0,0,1,1], [0,1,0,1]
theta = -2.43296373
tipRot[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
tipTfm = tipTrans.dot(tipRot)
finalRot = np.eye(4)
ind1, ind2 = [0,0,2,2], [0,2,0,2]
theta = np.pi/2
finalRot[ind1,ind2] = np.array([np.cos(theta), -1*np.sin(theta), np.sin(theta), np.cos(theta)])
WfromTip = needleTfm.dot(handleTfm.dot(tipTfm.dot(finalRot)))
return WfromTip
def getGripperFromNeedleTipTransform(self):
"""
Returns the transform between gripper and needle tip.
"""
if not self.grabbingArm:
rospy.logwarn('Needle not being held.')
return None
WfmNTip = self.needleTipTransform()
WfmEE = self.grabbingArm.manip.GetTransform()
EEfmNTip = nla.inv(WfmEE).dot(WfmNTip)
return EEfmNTip