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
00008
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
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
00031 def change_pose_stamped_frame(tf_listener, pose, frame):
00032
00033
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
00066
00067 command_frame_T_supplement_frame = tf_as_matrix(tf_listener.lookupTransform(command_frame, supplement_frame, rospy.Time(0)))
00068
00069 command_frame_T_header_frame = tf_as_matrix(tf_listener.lookupTransform(command_frame, m.header.frame_id, rospy.Time(0)))
00070
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
00074
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
00080
00081
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