Go to the documentation of this file.00001 import roslib; roslib.load_manifest('hrl_pr2_lib')
00002 import rospy
00003 import kinematics_msgs.srv as ks
00004 import hrl_lib.tf_utils as tfu
00005 import tf
00006 import numpy as np
00007 import pdb
00008
00009 class PR2ArmKinematics:
00010
00011 def __init__(self, arm, listener):
00012 self.tflistener = listener
00013 rospy.loginfo('PR2ArmKinematics: waiting for services for %s arm ' % arm)
00014 if arm == 'right':
00015 self.tflistener.waitForTransform('r_gripper_tool_frame', 'r_wrist_roll_link', rospy.Time(), rospy.Duration(10))
00016 else:
00017 self.tflistener.waitForTransform('l_gripper_tool_frame', 'l_wrist_roll_link', rospy.Time(), rospy.Duration(10))
00018
00019
00020 rospy.wait_for_service('pr2_' + arm + '_arm_kinematics/get_fk_solver_info')
00021 rospy.wait_for_service('pr2_' + arm + '_arm_kinematics/get_fk')
00022 rospy.loginfo('PR2ArmKinematics: forward kinematics services online.')
00023
00024 self._fk_info = rospy.ServiceProxy('pr2_' + arm + '_arm_kinematics/get_fk_solver_info', ks.GetKinematicSolverInfo )
00025 self._fk = rospy.ServiceProxy('pr2_' + arm + '_arm_kinematics/get_fk', ks.GetPositionFK, persistent=True)
00026 self.fk_info_resp = self._fk_info()
00027 self.joint_names = self.fk_info_resp.kinematic_solver_info.joint_names
00028 print 'PR2ArmKinematics: number of joints', len(self.joint_names)
00029
00030
00031 rospy.wait_for_service("pr2_" + arm + "_arm_kinematics/get_ik_solver_info")
00032 rospy.wait_for_service("pr2_" + arm + "_arm_kinematics/get_ik")
00033 rospy.loginfo('PR2ArmKinematics: inverse kinematics services online.')
00034 self._ik_info = rospy.ServiceProxy('pr2_' + arm +'_arm_kinematics/get_ik_solver_info', ks.GetKinematicSolverInfo)
00035 self._ik = rospy.ServiceProxy('pr2_' + arm +'_arm_kinematics/get_ik', ks.GetPositionIK, persistent=True)
00036 self.ik_info_resp = self._ik_info()
00037 self.ik_joint_names = self.ik_info_resp.kinematic_solver_info.joint_names
00038 if arm == 'left':
00039 self.ik_frame = 'l_wrist_roll_link'
00040 self.tool_frame = 'l_gripper_tool_frame'
00041 else:
00042 self.ik_frame = 'r_wrist_roll_link'
00043 self.tool_frame = 'r_gripper_tool_frame'
00044
00045
00046
00047
00048
00049
00050 def ik(self, cart_pose, frame_of_pose='torso_lift_link', seed=None):
00051
00052
00053 self.tflistener.waitForTransform(self.ik_frame, self.tool_frame, rospy.Time(), rospy.Duration(10))
00054
00055
00056
00057 toolframe_T_ikframe = tfu.transform(self.tool_frame, self.ik_frame, self.tflistener)
00058 cart_pose = cart_pose * toolframe_T_ikframe
00059
00060
00061 trans, rot = tfu.matrix_as_tf(cart_pose)
00062
00063 ik_req = ks.GetPositionIKRequest()
00064 ik_req.timeout = rospy.Duration(5.0)
00065 ik_req.ik_request.ik_link_name = self.ik_frame
00066
00067
00068 ik_req.ik_request.pose_stamped.header.frame_id = frame_of_pose
00069 ik_req.ik_request.pose_stamped.pose.position.x = trans[0]
00070 ik_req.ik_request.pose_stamped.pose.position.y = trans[1]
00071 ik_req.ik_request.pose_stamped.pose.position.z = trans[2]
00072
00073 ik_req.ik_request.pose_stamped.pose.orientation.x = rot[0]
00074 ik_req.ik_request.pose_stamped.pose.orientation.y = rot[1]
00075 ik_req.ik_request.pose_stamped.pose.orientation.z = rot[2]
00076 ik_req.ik_request.pose_stamped.pose.orientation.w = rot[3]
00077
00078
00079 ik_req.ik_request.ik_seed_state.joint_state.name = self.ik_joint_names
00080 if seed == None:
00081 p = []
00082 for i in range(len(self.ik_joint_names)):
00083 minp = self.ik_info_resp.kinematic_solver_info.limits[i].min_position
00084 maxp = self.ik_info_resp.kinematic_solver_info.limits[i].max_position
00085 p.append((minp + maxp) / 2.0)
00086 ik_req.ik_request.ik_seed_state.joint_state.position = p
00087 else:
00088 if seed.__class__ == np.matrix:
00089 seed = seed.T.A1.tolist()
00090 ik_req.ik_request.ik_seed_state.joint_state.position = seed
00091
00092 response = self._ik(ik_req)
00093 if response.error_code.val == response.error_code.SUCCESS:
00094
00095 return np.matrix(response.solution.joint_state.position).T
00096 else:
00097
00098 print response
00099 return None
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 def fk(self, joint_poses_mat, frame='torso_lift_link', sol_link=None, use_tool_frame=True):
00111 if sol_link == None:
00112 sol_link = self.ik_frame
00113
00114 fk_req = ks.GetPositionFKRequest()
00115 fk_req.header.frame_id = frame
00116 fk_req.fk_link_names = [sol_link]
00117 fk_req.robot_state.joint_state.name = self.fk_info_resp.kinematic_solver_info.joint_names
00118 fk_req.robot_state.joint_state.position = joint_poses_mat.T.A1.tolist()
00119 fk_resp = self._fk(fk_req)
00120
00121 solframe_T_wr = tfu.pose_as_matrix(fk_resp.pose_stamped[0].pose)
00122 if not use_tool_frame:
00123 return solframe_T_wr
00124 else:
00125
00126 self.tflistener.waitForTransform(self.tool_frame, sol_link, rospy.Time(), rospy.Duration(10))
00127 wr_T_toolframe = tfu.transform(sol_link, self.tool_frame, self.tflistener)
00128 return solframe_T_wr * wr_T_toolframe
00129
00130 class PR2Kinematics:
00131
00132 def __init__(self, tflistener=None):
00133 try:
00134 rospy.init_node('kinematics', anonymous=True)
00135 except rospy.exceptions.ROSException, e:
00136 pass
00137 if tflistener == None:
00138 self.tflistener = tf.TransformListener()
00139 else:
00140 self.tflistener = tflistener
00141
00142 self.left = PR2ArmKinematics('left', self.tflistener)
00143 self.right = PR2ArmKinematics('right', self.tflistener)
00144
00145
00146 if __name__ == '__main__':
00147 import pr2
00148 import pdb
00149 robot = pr2.PR2()
00150 pose = robot.left.pose()
00151
00152 k = PR2Kinematics(robot.tf_listener)
00153 cart = k.left.fk(pose)
00154 print 'cart pose\n', cart
00155 print 'real pose', pose.T
00156 ik_sol = k.left.ik(cart, 'torso_lift_link').T
00157 print 'ik pose', ik_sol
00158 print 'ik cart\n', k.left.fk(ik_sol)