ellipsoidal_ik.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 
00003 import sys
00004 import numpy as np
00005 import copy
00006 
00007 import roslib
00008 roslib.load_manifest("hrl_pr2_arms")
00009 roslib.load_manifest("kelsey_sandbox")
00010 import rospy
00011 from visualization_msgs.msg import Marker, MarkerArray
00012 from geometry_msgs.msg import Point, Quaternion, Vector3, PoseStamped
00013 from std_msgs.msg import ColorRGBA
00014 from sensor_msgs.msg import JointState
00015 import tf.transformations as tf_trans
00016 from equilibrium_point_control.pose_converter import PoseConverter
00017 
00018 import urdf_interface as urdf
00019 import kdl_parser as kdlp
00020 from hrl_pr2_arms.kdl_arm_kinematics import KDLArmKinematics
00021 from equilibrium_point_control.pose_converter import PoseConverter
00022 from hrl_pr2_arms.pr2_arm_kinematics import PR2ArmKinematics
00023 from hrl_pr2_arms.pr2_arm import PR2ArmJointTrajectory, PR2ArmJTranspose
00024 
00025 from spheroid_space import SpheroidSpace
00026 
00027 JOINT_STATE_INDS_R = [17, 18, 16, 20, 19, 21, 22]
00028 JOINT_STATE_INDS_L = [29, 30, 28, 32, 31, 33, 34]
00029 
00030 class SpheroidViz:
00031     def __init__(self):
00032         rot = np.mat([[1, 0, 0], [0, 1./np.sqrt(2), -1./np.sqrt(2)], [0, 1./np.sqrt(2), 1./np.sqrt(2)]])
00033         self.sspace = SpheroidSpace(0.2)#, np.mat([1.0, 0.5, 0.5]).T, rot)
00034         self.colors = [ColorRGBA(1., 0., 0., 1.), ColorRGBA(0., 1., 0., 1.)]
00035         self.vis_pub = rospy.Publisher("force_torque_markers_array", MarkerArray)
00036         self.pose_pub = rospy.Publisher("spheroid_poses", PoseStamped)
00037 
00038         m = Marker()
00039         m.header.frame_id = "torso_lift_link"
00040         m.header.stamp = rospy.Time()
00041         m.ns = "force_torque"
00042         m.id = 0
00043         m.type = Marker.ARROW
00044         m.action = Marker.ADD
00045         #m.points.append(Point(0, 0, 0))
00046         m.scale = Vector3(0.01, 0.01, 0.01)
00047         m.color = self.colors[0]
00048         #self.vis_pub.publish(m)
00049         self.m = m
00050         
00051         self.ma = MarkerArray()
00052 
00053     def publish_vector(self, m_id):
00054         new_m = copy.deepcopy(self.m)
00055         new_m.id = m_id
00056         u, v, p = 1, np.random.uniform(0, np.pi), np.random.uniform(0, 2 * np.pi)
00057         pos = self.sspace.spheroidal_to_cart((u, v, p))
00058         new_m.points.append(Point(*pos))
00059 
00060         df_du = self.sspace.partial_u((u, v, p))
00061         df_du *= 0.1 / np.linalg.norm(df_du)
00062         new_m.points.append(Point(*(pos+df_du)))
00063         
00064         self.ma.markers.append(new_m)
00065         self.vis_pub.publish(self.ma)
00066 
00067         rot_gripper = np.pi/4.
00068 
00069         nx, ny, nz = df_du.T.A[0] / np.linalg.norm(df_du)
00070         j = np.sqrt(1./(1.+ny*ny/(nz*nz)))
00071         k = -ny*j/nz
00072         norm_rot = np.mat([[-nx,  ny*k - nz*j,  0],      
00073                            [-ny,  -nx*k,        j],      
00074                            [-nz,  nx*j,         k]])
00075         _, norm_quat = PoseConverter.to_pos_quat(np.mat([0, 0, 0]).T, norm_rot)
00076         rot_angle = np.arctan(-norm_rot[2,1] / norm_rot[2,2])
00077         quat_ortho_rot = tf_trans.quaternion_from_euler(rot_angle + np.pi + rot_gripper, 0.0, 0.0)
00078         norm_quat_ortho = tf_trans.quaternion_multiply(norm_quat, quat_ortho_rot)
00079         ps_msg = PoseConverter.to_pose_stamped_msg("torso_lift_link", pos, norm_quat_ortho)
00080         self.pose_pub.publish(ps_msg)
00081 
00082 def fix_pr2_angs(q, min_lims, max_lims):
00083     q_mod = np.mod(q, 2 * np.pi)
00084     in_lims_a = np.clip(q_mod, min_lims, max_lims) == q_mod
00085     in_lims_b = np.clip(q_mod - 2 * np.pi, min_lims, max_lims) == q_mod
00086     if np.all(np.any([in_lims_a, in_lims_b])):
00087         return np.where(in_lims_a, q_mod, q_mod - 2 * np.pi)
00088     return None
00089 
00090 
00091 def main():
00092     rospy.init_node("ellipsoidal_ik")
00093     sspace = SpheroidSpace(0.2, np.mat([0.78, -0.28, 0.3]).T)
00094     kin = PR2ArmKinematics('r')
00095     jarm = PR2ArmJointTrajectory('r', kin)
00096 
00097     while not rospy.is_shutdown():
00098         u, v, p = 1, np.random.uniform(0, np.pi), np.random.uniform(0, 2 * np.pi)
00099         pos, rot = sspace.spheroidal_to_pose((u, v, p))
00100         q = kin.IK(pos, rot)
00101         if not q is None:
00102             print q
00103 #print np.mod(q, 2 * np.pi)
00104         rospy.sleep(0.1)
00105 #jarm.set_ep([0.1]*7, 15)
00106 
00107     return
00108 
00109     jfv = SpheroidViz()
00110     i = 0
00111     while not rospy.is_shutdown():
00112         jfv.publish_vector(i)
00113         i += 1
00114         rospy.sleep(0.5)
00115     return
00116 
00117 if __name__ == "__main__":
00118     main()


kelsey_sandbox
Author(s): kelsey
autogenerated on Wed Nov 27 2013 11:52:03