pr2_arms.py
Go to the documentation of this file.
00001 import PyKDL as kdl
00002 
00003 import numpy as np, math
00004 
00005 import roslib; roslib.load_manifest('pr2_arms_kdl')
00006 import rospy
00007 import hrl_lib.kdl_utils as ku
00008 
00009 
00010 class PR2_arm_kdl():
00011 
00012     def __init__(self):
00013         self.right_chain = self.create_right_chain()
00014         fk, ik_v, ik_p = self.create_solvers(self.right_chain)
00015         self.right_fk = fk
00016         self.right_ik_v = ik_v
00017         self.right_ik_p = ik_p
00018 
00019     def create_right_chain(self):
00020         ch = kdl.Chain()
00021         self.right_arm_base_offset_from_torso_lift_link = np.matrix([0., -0.188, 0.]).T
00022         # shoulder pan
00023         ch.addSegment(kdl.Segment(kdl.Joint(kdl.Joint.RotZ),kdl.Frame(kdl.Vector(0.1,0.,0.))))
00024         # shoulder lift
00025         ch.addSegment(kdl.Segment(kdl.Joint(kdl.Joint.RotY),kdl.Frame(kdl.Vector(0.,0.,0.))))
00026         # upper arm roll
00027         ch.addSegment(kdl.Segment(kdl.Joint(kdl.Joint.RotX),kdl.Frame(kdl.Vector(0.4,0.,0.))))
00028         # elbox flex
00029         ch.addSegment(kdl.Segment(kdl.Joint(kdl.Joint.RotY),kdl.Frame(kdl.Vector(0.0,0.,0.))))
00030         # forearm roll
00031         ch.addSegment(kdl.Segment(kdl.Joint(kdl.Joint.RotX),kdl.Frame(kdl.Vector(0.321,0.,0.))))
00032         # wrist flex
00033         ch.addSegment(kdl.Segment(kdl.Joint(kdl.Joint.RotY),kdl.Frame(kdl.Vector(0.,0.,0.))))
00034         # wrist roll
00035         ch.addSegment(kdl.Segment(kdl.Joint(kdl.Joint.RotX),kdl.Frame(kdl.Vector(0.,0.,0.))))
00036         return ch
00037 
00038     def create_solvers(self, ch):
00039          fk = kdl.ChainFkSolverPos_recursive(ch)
00040          ik_v = kdl.ChainIkSolverVel_pinv(ch)
00041          ik_p = kdl.ChainIkSolverPos_NR(ch, fk, ik_v)
00042          return fk, ik_v, ik_p
00043 
00044     def FK_kdl(self, arm, q, link_number):
00045         if arm == 'right_arm':
00046             fk = self.right_fk
00047             endeffec_frame = kdl.Frame()
00048             kinematics_status = fk.JntToCart(q, endeffec_frame,
00049                                              link_number)
00050             if kinematics_status >= 0:
00051                 return endeffec_frame
00052             else:
00053                 rospy.loginfo('Could not compute forward kinematics.')
00054                 return None
00055         else:
00056             msg = '%s arm not supported.'%(arm)
00057             rospy.logerr(msg)
00058             raise RuntimeError(msg)
00059 
00060     ## returns point in torso lift link.
00061     def FK_all(self, arm, q, link_number = 7):
00062         q = self.pr2_to_kdl(q)
00063         frame = self.FK_kdl(arm, q, link_number)
00064         pos = frame.p
00065         pos = ku.kdl_vec_to_np(pos)
00066         pos = pos + self.right_arm_base_offset_from_torso_lift_link
00067         m = frame.M
00068         rot = ku.kdl_rot_to_np(m)
00069         return pos, rot
00070 
00071     def kdl_to_pr2(self, q):
00072         if q == None:
00073             return None
00074 
00075         q_pr2 = [0] * 7
00076         q_pr2[0] = q[0]
00077         q_pr2[1] = q[1]
00078         q_pr2[2] = q[2]
00079         q_pr2[3] = q[3]
00080         q_pr2[4] = q[4]
00081         q_pr2[5] = q[5]
00082         q_pr2[6] = q[6]
00083         return q_pr2
00084 
00085     def pr2_to_kdl(self, q):
00086         if q == None:
00087             return None
00088         n = len(q)
00089         q_kdl = kdl.JntArray(n)
00090         for i in range(n):
00091             q_kdl[i] = q[i]
00092         return q_kdl
00093 
00094 
00095 if __name__ == '__main__':
00096     roslib.load_manifest('hrl_pr2_lib')
00097     import hrl_pr2_lib.pr2_arms as pa
00098     import hrl_lib.viz as hv
00099     from visualization_msgs.msg import Marker
00100     
00101     rospy.init_node('kdl_pr2_test')
00102     marker_pub = rospy.Publisher('/kdl_pr2_arms/viz_marker', Marker)
00103     pr2_arms = pa.PR2Arms(gripper_point=(0.,0.,0.))
00104     pr2_kdl = PR2_arm_kdl()
00105 
00106     r_arm, l_arm = 0, 1
00107 
00108     while not rospy.is_shutdown():
00109         q = pr2_arms.get_joint_angles(r_arm)
00110         p, r = pr2_kdl.FK_all('right_arm', q, 7)
00111         m = hv.create_frame_marker(p, r, 0.3, 'torso_lift_link')
00112         time_stamp = rospy.Time.now()
00113         m.header.stamp = time_stamp
00114         marker_pub.publish(m)
00115         rospy.sleep(0.1)
00116 
00117 
00118 


pr2_arms_kdl
Author(s): Advait Jain
autogenerated on Wed Nov 27 2013 12:12:57