Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 PKG = 'approach_table_tools' 
00038 NAME = 'find_approach_pose'
00039 
00040 import roslib; roslib.load_manifest(PKG) 
00041 import rospy 
00042 import tf
00043 import numpy
00044 import math
00045 
00046 from geometry_msgs.msg import Quaternion, Pose
00047 from approach_table_tools.srv import GetApproachPose, GetApproachPoseResponse
00048 
00049 def msg_to_quaternion(msg):
00050   return [msg.x, msg.y, msg.z, msg.w]
00051 
00052 def quaternion_to_msg(q):
00053   msg = Quaternion()
00054   msg.x = q[0]
00055   msg.y = q[1]
00056   msg.z = q[2]
00057   msg.w = q[3]
00058   return msg
00059 
00060 def msg_to_pose(msg):
00061   trans = tf.transformations.quaternion_matrix(msg_to_quaternion(msg.orientation))
00062   trans[0, 3] = msg.position.x
00063   trans[1, 3] = msg.position.y
00064   trans[2, 3] = msg.position.z
00065   trans[3, 3] = 1.0
00066   return trans
00067 
00068 def pose_to_msg(pose):
00069   msg = Pose()
00070   msg.position.x = pose[0, 3]
00071   msg.position.y = pose[1, 3]
00072   msg.position.z = pose[2, 3]
00073   q = tf.transformations.quaternion_from_matrix(pose)
00074   msg.orientation.x = q[0]
00075   msg.orientation.y = q[1]
00076   msg.orientation.z = q[2]
00077   msg.orientation.w = q[3]
00078   return msg
00079 
00080 class ApproachPoseFinder:
00081   def __init__(self):
00082     self.listener = tf.TransformListener()
00083     self.server = rospy.Service('get_approach_pose', GetApproachPose, self.get_approach_pose)
00084     self.fixed_frame = rospy.get_param('~fixed_frame', 'odom_combined')
00085 
00086   def get_approach_pose(self, req):
00087     board_pose = req.board_pose
00088     
00089     trans = msg_to_pose(board_pose.pose)
00090     origin = tf.transformations.translation_matrix([2 * 0.08, 0, -1.0])
00091     pose_mat =  numpy.dot(trans, origin)
00092     board_pose.pose = pose_to_msg(pose_mat)
00093   
00094     
00095     
00096     self.listener.waitForTransform(self.fixed_frame, board_pose.header.frame_id, board_pose.header.stamp, rospy.Duration(2.0))
00097   
00098     
00099     odom_pose = self.listener.transformPose(self.fixed_frame, board_pose)
00100   
00101     
00102     odom_pose.pose.position.z = 0.0
00103   
00104     
00105     rot_q = tf.transformations.quaternion_from_euler(0.0, 0.0, math.pi / 2)
00106     new_q = tf.transformations.quaternion_multiply(rot_q, msg_to_quaternion(odom_pose.pose.orientation))
00107   
00108     
00109     roll, pitch, yaw = tf.transformations.euler_from_quaternion(new_q)
00110   
00111     
00112     odom_pose.pose.orientation = quaternion_to_msg(tf.transformations.quaternion_from_euler(0.0, 0.0, yaw))
00113   
00114     return GetApproachPoseResponse(odom_pose)
00115 
00116 def approach_pose_finder():
00117   rospy.init_node(NAME)
00118   af = ApproachPoseFinder()
00119   rospy.spin()
00120 
00121 if __name__ == "__main__":
00122   approach_pose_finder()