00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 from __future__ import division
00040 import roslib
00041 roslib.load_manifest('pr2_gripper_stereo')
00042 import time
00043 import rospy
00044 import os
00045 import pr2_gripper_reactive_approach.controller_manager as controller_manager
00046 import sys
00047 from geometry_msgs.msg import PoseStamped, Pose, Point, Quaternion, Vector3
00048 from object_manipulation_msgs.msg import GripperTranslation, ManipulationPhase
00049 import tf
00050 import scipy
00051 import scipy.linalg
00052 import math
00053 import pdb
00054 import pr2_gripper_reactive_approach.hand_sensor_listeners as hand_sensor_listeners
00055 from object_manipulator.convert_functions import *
00056 import copy
00057 from actionlib_msgs.msg import GoalStatus
00058 from pr2_gripper_reactive_approach.reactive_grasp import ReactiveGrasper, Aborted
00059 from pr2_gripper_stereo.srv import QueryGripperStereo, QueryGripperStereoRequest, GripperStereoGraspAdjust, GripperStereoGraspAdjustRequest, GripperStereoGraspAdjustResponse
00060 import object_manipulator.draw_functions as draw_functions
00061
00062
00063
00064 class GripperStereoReactiveGrasper(ReactiveGrasper):
00065
00066 def __init__(self, cm):
00067
00068 ReactiveGrasper.__init__(self, cm)
00069
00070
00071 rospy.loginfo("waiting for gripper stereo service")
00072 rospy.wait_for_service("/gripper_stereo_grasp_adjust")
00073 rospy.loginfo("gripper service found")
00074 self._gripper_stereo_grasp_adjust_client = rospy.ServiceProxy("/gripper_stereo_grasp_adjust", GripperStereoGraspAdjust)
00075
00076 self.stereo_adjusted = 0
00077
00078
00079 self.draw_functions = draw_functions.DrawFunctions('grasp_markers')
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 def gripper_stereo_grasp_adjust(self, grasp_pose, do_global_search = 1):
00100
00101 req = GripperStereoGraspAdjustRequest()
00102 req.grasp_pose = grasp_pose
00103 req.roi_dims = Vector3(.1, .3, .3)
00104 req.do_global_search = do_global_search
00105
00106 try:
00107 resp = self._gripper_stereo_grasp_adjust_client(req)
00108 except rospy.ServiceException, e:
00109 rospy.logerr("error when asking gripper stereo to adjust grasp, %s"%e)
00110 return ([grasp_pose], 1)
00111 return (resp.adjusted_grasp_pose, resp.result)
00112
00113
00114
00115
00116 def gripper_stereo_reactive_approach(self, approach_dir, grasp_pose, stop_at_dist = 0.05):
00117
00118 left_touching = right_touching = palm_touching = 0
00119 new_goal = grasp_pose
00120 new_approach_dir = approach_dir.copy()
00121
00122 if self.stereo_adjusted == 1:
00123 rospy.loginfo("gripper stereo adjustment already done")
00124 return (0, 0, 0, grasp_pose, approach_dir, 1)
00125
00126
00127 (proj_vect, orthogonal_vect) = self.projected_vect_to_obj(new_goal, approach_dir)
00128 dist_to_obj = scipy.linalg.norm(proj_vect)
00129
00130
00131
00132
00133
00134 grasp_feasible = True
00135
00136
00137
00138 for i in range(2):
00139 do_global_search = 1
00140 if i > 0:
00141 print "Pausing 1.0 sec..."
00142 rospy.sleep(1.0)
00143 do_global_search = 0
00144 (left_touching, right_touching, palm_touching, new_goal, new_approach_dir, grasp_feasible) = self.adjust_using_gripper_stereo(new_goal, new_approach_dir, do_global_search)
00145
00146
00147 if left_touching or right_touching or palm_touching or not grasp_feasible:
00148 break
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 self.stereo_adjusted = 1
00171
00172 return (left_touching, right_touching, palm_touching, new_goal, new_approach_dir, grasp_feasible)
00173
00174
00175
00176 def adjust_using_gripper_stereo(self, grasp_goal, approach_dir, do_global_search = 1):
00177
00178 print "grasp_goal: "+str(grasp_goal)
00179
00180
00181 for tries in range(3):
00182 rospy.loginfo("asking gripper stereo to adjust the grasp, try number %d"%tries)
00183
00184
00185 (new_goal_list, result) = self.gripper_stereo_grasp_adjust(grasp_goal, do_global_search)
00186
00187
00188 if result == GripperStereoGraspAdjustResponse.IMPOSSIBLE:
00189 rospy.loginfo("gripper stereo adjustment returned grasp impossible!")
00190 continue
00191
00192 if result == GripperStereoGraspAdjustResponse.FAILURE:
00193 rospy.loginfo("gripper stereo adjustment returned failed!")
00194 continue
00195
00196
00197 for new_goal in new_goal_list:
00198
00199 print "new_goal: "+str(new_goal)
00200
00201
00202 current_angles = self.cm.get_current_arm_angles()
00203 current_pose = self.cm.get_current_wrist_pose_stamped()
00204 (trajectory, error_codes) = self.cm.ik_utilities.check_cartesian_path(current_pose, new_goal, current_angles, collision_aware = 0)
00205 print "error_codes:", error_codes
00206 if any(error_codes):
00207 rospy.loginfo("can't execute an interpolated IK trajectory to new_goal, moving onto the next")
00208 continue
00209 else:
00210 rospy.loginfo("found interpolated IK trajectory")
00211
00212
00213 new_goal = change_pose_stamped_frame(self.cm.tf_listener, new_goal, 'base_link')
00214
00215
00216 (proj_vect, orthogonal_vect) = self.projected_vect_to_obj(grasp_goal, approach_dir)
00217
00218
00219 dist_to_obj = scipy.linalg.norm(proj_vect)
00220
00221
00222 new_goal_mat = pose_to_mat(new_goal.pose)
00223 dist_to_obj_mat = new_goal_mat.copy()
00224 dist_to_obj_mat[0:3, 3] += new_goal_mat[0:3, 0] * -dist_to_obj
00225 dist_to_obj_pose = stamp_pose(mat_to_pose(dist_to_obj_mat), 'base_link')
00226
00227
00228 new_approach_dir = scipy.array(new_goal_mat[0:3, 0].T)[0]
00229
00230
00231
00232
00233 print "dist_to_obj: %0.3f"%dist_to_obj
00234 print "dist_to_obj_pose: "+str(dist_to_obj_pose)
00235
00236
00237 self.draw_functions.draw_rviz_axes(dist_to_obj_mat, 'base_link', id = 0, duration = 30)
00238
00239
00240 (left_touching, right_touching, palm_touching) = self.guarded_move_ik(dist_to_obj_pose, max_joint_vel = .15)
00241 break
00242
00243
00244 else:
00245 continue
00246
00247
00248 break
00249
00250
00251 else:
00252 return (0, 0, 0, grasp_goal, approach_dir, False)
00253
00254 return (left_touching, right_touching, palm_touching, new_goal, approach_dir, True)
00255
00256
00257
00258 def projected_vect_to_obj(self, goal, approach_dir):
00259
00260
00261 bl_goal_frame = change_pose_stamped_frame(self.cm.tf_listener, goal, 'base_link')
00262
00263
00264 current_pose = self.cm.get_current_wrist_pose_stamped()
00265 goal_frame_pos = scipy.array(get_xyz(bl_goal_frame.pose.position))
00266 current_frame_pos = scipy.array(get_xyz(current_pose.pose.position))
00267 toward_goal_frame = goal_frame_pos - current_frame_pos
00268
00269
00270 proj = self.vect_proj(toward_goal_frame, approach_dir)
00271
00272
00273 orthogonal_vect = toward_goal_frame - proj
00274
00275
00276
00277
00278
00279 return (proj, orthogonal_vect)
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 def reactive_approach(self, approach_dir, grasp_pose, joint_path = None,
00320 side_step = .015, back_step = .03, \
00321 num_tries = 10, goal_pos_thres = 0.01):
00322
00323 rospy.loginfo("running gripper stereo reactive approach")
00324 (left_touching, right_touching, palm_touching, grasp_pose, approach_dir, grasp_feasible) = self.gripper_stereo_reactive_approach(approach_dir, grasp_pose, stop_at_dist = 0.05)
00325
00326 if not grasp_feasible:
00327 rospy.loginfo("grasp infeasible!")
00328 return self.reactive_approach_result_dict["grasp infeasible"]
00329
00330 rospy.loginfo("running normal approach")
00331 rospy.loginfo("new grasp pose: "+str(grasp_pose))
00332 ReactiveGrasper.reactive_approach(self, approach_dir, grasp_pose, joint_path, side_step,
00333 back_step, num_tries, goal_pos_thres)
00334
00335
00336 def reactive_grasp(self, approach_pose, grasp_pose, joint_path = None, side_step = .015, back_step = .03, \
00337 approach_num_tries = 10, goal_pos_thres = 0.01, min_gripper_opening = 0.0021, max_gripper_opening = 0.1, \
00338 grasp_num_tries = 2, forward_step = 0.03, min_contact_row = 1, object_name = "points", table_name = "table", grasp_adjust_x_step = .02, grasp_adjust_z_step = .015, grasp_adjust_num_tries = 3):
00339
00340 self.stereo_adjusted = 0
00341
00342 result = ReactiveGrasper.reactive_grasp(self, approach_pose, grasp_pose, joint_path, side_step, back_step,
00343 approach_num_tries, goal_pos_thres, min_gripper_opening, max_gripper_opening,
00344 grasp_num_tries, forward_step, min_contact_row, object_name, table_name,
00345 grasp_adjust_x_step, grasp_adjust_z_step, grasp_adjust_num_tries)
00346
00347 return result