Go to the documentation of this file.00001 import roslib; roslib.load_manifest('hai_sandbox')
00002 import rospy
00003 import kinematics_msgs.srv as ks
00004 import hrl_lib.tf_utils as tfu
00005 import tf
00006 import hai_sandbox.pr2_kinematics as pr2k
00007 import sensor_msgs.msg as sm
00008 import hrl_lib.rutils as ru
00009 import numpy as np
00010 import hrl_lib.util as ut
00011
00012 def script():
00013 rospy.init_node('forward_kin')
00014 tflistener = tf.TransformListener()
00015 print 'waiting for transform'
00016 tflistener.waitForTransform('r_gripper_tool_frame', 'r_wrist_roll_link', rospy.Time(), rospy.Duration(10))
00017 print 'waiting for services'
00018 rospy.wait_for_service('pr2_right_arm_kinematics/get_fk_solver_info')
00019 rospy.wait_for_service('pr2_right_arm_kinematics/get_fk')
00020 print 'done init'
00021
00022 r_fk_info = rospy.ServiceProxy('pr2_right_arm_kinematics/get_fk_solver_info', ks.GetKinematicSolverInfo )
00023 r_fk = rospy.ServiceProxy('pr2_right_arm_kinematics/get_fk', ks.GetPositionFK)
00024
00025 resp = r_fk_info()
00026 print 'get_fk_solver_info returned', resp.kinematic_solver_info.joint_names
00027 print 'get_fk_solver_info returned', resp.kinematic_solver_info.limits
00028 print 'get_fk_solver_info returned', resp.kinematic_solver_info.link_names
00029
00030 fk_req = ks.GetPositionFKRequest()
00031 fk_req.header.frame_id = 'torso_lift_link'
00032 fk_req.fk_link_names = ['r_wrist_roll_link']
00033 fk_req.robot_state.joint_state.name = resp.kinematic_solver_info.joint_names
00034 fk_req.robot_state.joint_state.position = [.5 for i in range(len(resp.kinematic_solver_info.joint_names))]
00035 fk_resp = r_fk(fk_req)
00036
00037 rtip_T_wr = tfu.transform('r_gripper_tool_frame', 'r_wrist_roll_link', tflistener)
00038 right_wr = tfu.pose_as_matrix(fk_resp.pose_stamped[0].pose)
00039 rtip_pose = rtip_T_wr * right_wr
00040 print tfu.matrix_as_tf(rtip_pose)
00041
00042 class TestForwardKin:
00043 def __init__(self):
00044 self.name_dict = None
00045 self.msgs = []
00046 self.joint_idx = {}
00047
00048 rospy.init_node('forward_kin')
00049 self.tflistener = tf.TransformListener()
00050 self.fkright = pr2k.PR2ArmKinematics('right', self.tflistener)
00051 self.point_cloud_pub = rospy.Publisher('right_fk', sm.PointCloud)
00052
00053 rospy.Subscriber('/joint_states', sm.JointState, self.joint_state_cb)
00054 print 'done init'
00055
00056
00057 def joint_state_cb(self, msg):
00058 if self.name_dict == None:
00059 self.name_dict = {}
00060 for i, n in enumerate(msg.name):
00061 self.name_dict[n] = i
00062 self.joint_groups = ut.load_pickle('link_names.pkl')
00063 for group in self.joint_groups.keys():
00064 self.joint_idx[group] = [self.name_dict[name] for name in self.joint_groups[group]]
00065
00066 dmat = np.matrix(msg.position).T
00067 joint_angles = dmat[self.joint_idx['rarm'], 0].A1.tolist()
00068
00069
00070 rtip_pose = self.fkright.fk('base_footprint', 'r_wrist_roll_link', 'r_gripper_tool_frame', joint_angles)
00071 position = rtip_pose[0:3,3]
00072
00073
00074 pc = ru.np_to_pointcloud(position, 'base_footprint')
00075 pc.header.stamp = rospy.get_rostime()
00076 self.point_cloud_pub.publish(pc)
00077
00078 def run(self):
00079 r = rospy.Rate(10)
00080 while not rospy.is_shutdown():
00081 r.sleep()
00082
00083
00084 def kin_class():
00085 tflistener = tf.TransformListener()
00086 right = pr2k.PR2ArmKinematics('right', tflistener)
00087 rtip_pose = right.fk('torso_lift_link', 'r_wrist_roll_link', 'r_gripper_tool_frame', [.5 for i in range(len(right.joint_names))])
00088 print tfu.matrix_as_tf(rtip_pose)
00089
00090 left = pr2k.PR2ArmKinematics('left', tflistener)
00091 ltip_pose = left.fk('torso_lift_link', 'l_wrist_roll_link', 'l_gripper_tool_frame', [.5 for i in range(len(left.joint_names))])
00092 print tfu.matrix_as_tf(ltip_pose)
00093
00094 if __name__ == '__main__':
00095
00096 t = TestForwardKin()
00097 t.run()
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118