pr2_kinematics.py
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         # Forward kinematics
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         # Inverse kinematics
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     # Inverse kinematics
00047     # @param cart_pose a 4x4 SE(3) pose
00048     # @param frame_of_pose frame cart_pose is given in, if None we assume that self.tool_frame is being used
00049     # @param seed starting solution for IK solver (list of floats or column np.matrix of floats)
00050     def ik(self, cart_pose, frame_of_pose='torso_lift_link', seed=None):
00051         #if frame_of_pose == self.tool_frame or frame_of_pose == None:
00052 
00053         self.tflistener.waitForTransform(self.ik_frame, self.tool_frame, rospy.Time(), rospy.Duration(10))
00054         #wr_T_toolframe = tfu.transform(sol_link, self.tool_frame, self.tflistener)
00055         #solframe_T_wr * wr_T_toolframe
00056         #print 'undoing'
00057         toolframe_T_ikframe = tfu.transform(self.tool_frame, self.ik_frame, self.tflistener)
00058         cart_pose = cart_pose * toolframe_T_ikframe 
00059         #frame_of_pose = self.tool_frame
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         #set pose
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]#cart_pose[0][0,0]
00070         ik_req.ik_request.pose_stamped.pose.position.y = trans[1]#cart_pose[0][1,0]
00071         ik_req.ik_request.pose_stamped.pose.position.z = trans[2]#cart_pose[0][2,0]
00072 
00073         ik_req.ik_request.pose_stamped.pose.orientation.x = rot[0]#cart_pose[1][0,0];
00074         ik_req.ik_request.pose_stamped.pose.orientation.y = rot[1]#cart_pose[1][1,0];
00075         ik_req.ik_request.pose_stamped.pose.orientation.z = rot[2]#cart_pose[1][2,0];
00076         ik_req.ik_request.pose_stamped.pose.orientation.w = rot[3]#cart_pose[1][3,0];
00077 
00078         #seed solver
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             #print 'success'
00095             return np.matrix(response.solution.joint_state.position).T
00096         else:
00097             #print 'fail', response.__class__, response
00098             print response
00099             return None
00100 
00101     ##
00102     # Forward Kinematics
00103     # @param joint_poses_mat nx1 matrix of joint positions
00104     # @param frame frame to give solution in
00105     # @param sol_link link to solve FK for
00106     # @param use_tool_frame FK doesn't account for length of tool frame (PR2 gripper), 
00107     #                       if sol_link is the wrist link then will return the
00108     #                       gripper's FK.
00109     # @return a 4x4 SE(3) matrix
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             #print 'redoing'
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)


hrl_pr2_lib
Author(s): haidai
autogenerated on Wed Nov 27 2013 11:40:34