tf_utils.py
Go to the documentation of this file.
00001 import numpy as np
00002 import tf.transformations as tr
00003 import geometry_msgs.msg as geo
00004 import rospy
00005 import tf
00006 
00007 #Copied (gasp!) from object_manipulator.convert_functions
00008 #(first premultiply by transform if given)
00009 def mat_to_pose(mat, transform = None):
00010     if transform != None:
00011         mat = transform * mat
00012     pose = geo.Pose()
00013     pose.position.x = mat[0,3]
00014     pose.position.y = mat[1,3]
00015     pose.position.z = mat[2,3]
00016     quat = tf.transformations.quaternion_from_matrix(mat)
00017     pose.orientation.x = quat[0]
00018     pose.orientation.y = quat[1]
00019     pose.orientation.z = quat[2]
00020     pose.orientation.w = quat[3]
00021     return pose
00022 
00023 ##make a PoseStamped out of a Pose
00024 def stamp_pose(pose, frame_id):
00025     pose_stamped = geo.PoseStamped()
00026     stamp_msg(pose_stamped, frame_id)
00027     pose_stamped.pose = pose
00028     return pose_stamped
00029 
00030 ##change the frame of a PoseStamped
00031 def change_pose_stamped_frame(tf_listener, pose, frame):
00032 
00033     #convert the PoseStamped to the desired frame, if necessary
00034     if pose.header.frame_id != frame:
00035         pose.header.stamp = rospy.Time(0)
00036         tf_listener.waitForTransform(frame, pose.header.frame_id, pose.header.stamp, rospy.Duration(5))
00037         try:
00038             trans_pose = tf_listener.transformPose(frame, pose)
00039         except rospy.ServiceException, e:
00040             print "pose:\n", pose
00041             print "frame:", frame
00042             rospy.logerr("change_pose_stamped_frame: error in transforming pose from " + pose.header.frame_id + " to " + frame + "error msg: %s"%e)
00043             return None
00044     else:
00045         trans_pose = pose
00046 
00047     return trans_pose
00048 
00049 
00050 def tf_as_matrix(tup):
00051     return np.matrix(tr.translation_matrix(tup[0])) * np.matrix(tr.quaternion_matrix(tup[1])) 
00052 
00053 def transform(to_frame, from_frame, tflistener, t=0):
00054     return tf_as_matrix(tflistener.lookupTransform(to_frame, from_frame, rospy.Time(t)))
00055 
00056 def tf_as_matrix(tup):
00057     return np.matrix(tr.translation_matrix(tup[0])) * np.matrix(tr.quaternion_matrix(tup[1])) 
00058 
00059 def matrix_as_tf(mat):
00060     return (tr.translation_from_matrix(mat), tr.quaternion_from_matrix(mat))
00061 
00062 def origin_to_frame(origin, supplement_frame, tf_listener, command_frame):
00063     m = origin
00064     if isinstance(m, geo.PointStamped):
00065         #point in some frame, needs orientation...
00066         #print 'command_frame', command_frame, 'supplement_frame', supplement_frame, 'header_frame', m.header.frame_id
00067         command_frame_T_supplement_frame = tf_as_matrix(tf_listener.lookupTransform(command_frame, supplement_frame, rospy.Time(0)))
00068         #print 'command_frame_T_supplement_frame\n', command_frame_T_supplement_frame
00069         command_frame_T_header_frame = tf_as_matrix(tf_listener.lookupTransform(command_frame, m.header.frame_id, rospy.Time(0)))
00070         #print 'command_frame_T_header_frame\n', command_frame_T_header_frame
00071         point_header = np.matrix([m.point.x, m.point.y, m.point.z, 1.]).T
00072         point_command_frame = command_frame_T_header_frame * point_header
00073         #print 'point header\n', point_header.T
00074         #print 'point_command_frame\n', point_command_frame.T
00075 
00076         command_frame_T_point_frame = command_frame_T_supplement_frame.copy()
00077         command_frame_T_point_frame[0:3,3] = point_command_frame[0:3,0]
00078         CMD_T_frame = command_frame_T_point_frame
00079         #print 'command_frame_T_point_frame\n', command_frame_T_point_frame
00080 
00081     #If it's a pose stamped then we turn the pose stamped into a frame?
00082     elif isinstance(m, geo.PoseStamped):
00083         fid_T_p = pose_to_mat(m.pose)
00084         print 'fid_T_p\n', fid_T_p
00085         print fid_T_p[0:3,3].T
00086         CMD_T_fid = tf_as_matrix(tf_listener.lookupTransform(command_frame, m.header.frame_id, rospy.Time(0)))
00087         print 'FRAMES', command_frame, m.header.frame_id
00088         print 'CMD_T_fid\n', CMD_T_fid
00089         print CMD_T_fid[0:3,3].T
00090         CMD_T_frame = CMD_T_fid * fid_T_p
00091         print 'CMD_T_frame\n', CMD_T_frame
00092         print CMD_T_frame[0:3,3].T
00093     else:
00094         raise RuntimeError('Got origin that is an instance of ' + str(m.__class__))
00095 
00096     return CMD_T_frame


rcommander
Author(s): Hai Nguyen (haidai@gmail.com)
autogenerated on Thu Nov 28 2013 11:46:34