trajectory.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('zyonz_apps_base')
00004 import rospy
00005 import smach
00006 import smach_ros
00007 import tf
00008 import geometry_msgs
00009 import numpy
00010 import math
00011 
00012 from smach import CBState
00013 from iri_common_smach.utils_msg          import build_pose_stamped_msg
00014 from iri_wam_smach.sm_wam_move_from_pose import SM_WAM_MoveFromPose
00015 from geometry_msgs.msg import PoseStamped
00016 from tf.transformations import *
00017 from pprint import pprint
00018 
00019 def pose_st_from_matrix(matrix, frame_id):
00020     """ Returns geometry_msgs.msg.Pose() from transformation.matrix
00021 
00022     """
00023     quaternion = quaternion_from_matrix(matrix)
00024     position = translation_from_matrix(matrix)
00025 
00026     pose_st = PoseStamped() 
00027     pose_st.header.frame_id = frame_id 
00028     pose_st.pose.position.x = position[0]
00029     pose_st.pose.position.y = position[1] 
00030     pose_st.pose.position.z = position[2] 
00031     pose_st.pose.orientation.x = quaternion[0] 
00032     pose_st.pose.orientation.y = quaternion[1] 
00033     pose_st.pose.orientation.z = quaternion[2] 
00034     pose_st.pose.orientation.w = quaternion[3] 
00035     
00036     return pose_st
00037 
00038 def trajectory_around_view(pose_st, distance, number_of_poses):
00039     """ 
00040         Returns a list of poses around and arc. The arc is defined by
00041         the current pose, the distance to the center of the arc from the
00042         current pose and the number of next poses required
00043 
00044     """
00045     # This is the beginning of the FUNCTION 
00046     origin, xaxis, yaxis, zaxis = (0,0,0), (1,0,0), (0,1,0), (0,0,1)
00047     T = translation_matrix((pose_st.pose.position.x, pose_st.pose.position.y, pose_st.pose.position.z))
00048     R = quaternion_matrix((pose_st.pose.orientation.x, pose_st.pose.orientation.y, pose_st.pose.orientation.z, pose_st.pose.orientation.w))
00049     H = concatenate_matrices(T,R)
00050     frame_id = pose_st.header.frame_id 
00051 
00052     # Center of Rotation
00053     cr = concatenate_matrices(H, translation_matrix((0.0, 0.0, distance)))
00054     cr = concatenate_matrices(cr, rotation_matrix(-math.pi/2 ,zaxis), rotation_matrix(-math.pi, xaxis))
00055     print cr
00056     
00057     # Computing the new views around the arc
00058     lim_low = math.pi/4
00059     lim_high = -math.pi/4
00060 
00061     ang_y = 0
00062     pose_st_list = []
00063     """ 
00064         Uncomment if you want to have a sphere instead of an arc
00065     """
00066     #for ang_y in numpy.linspace(lim_low, lim_high, number_of_poses):
00067     for ang_x in numpy.linspace(lim_low, lim_high, number_of_poses):
00068         next_pose = concatenate_matrices(cr, concatenate_matrices(rotation_matrix(ang_y, yaxis), rotation_matrix(ang_x, xaxis), inverse_matrix(cr),H))
00069         pose_st_list.append(pose_st_from_matrix(next_pose, frame_id))
00070         
00071     return pose_st_list
00072  
00073 class ZYONZGetArcTrajectory(smach.State):
00074     """
00075          
00076     """
00077     def __init__(self):
00078         smach.State.__init__(self, outcomes = ['success','fail'],
00079                                    input_keys = ['pose_st', 'distance', 'num_views'],
00080                                    output_keys = ['pose_st_list'])
00081 
00082     def execute(self, userdata):
00083         try:
00084             response = trajectory_around_view(userdata.pose_st, userdata.distance, userdata.num_views)        
00085         except rospy.ServiceException, e:
00086             return 'fail'
00087         
00088         if (not response):
00089             return 'fail'
00090         
00091         pprint(response)
00092 
00093         userdata.pose_st_list = response
00094         return 'success'


zyonz_apps_base
Author(s): Jose Luis Rivero
autogenerated on Fri Dec 6 2013 22:53:18