00001
00002
00003 import argparse
00004 from geometry_msgs.msg import Quaternion
00005 from geometry_msgs.msg import Transform
00006 from geometry_msgs.msg import TransformStamped
00007 from geometry_msgs.msg import Vector3
00008 import rospy
00009 from std_msgs.msg import Header
00010 import tf
00011
00012
00013 class TFToTransform(object):
00014 def __init__(self, parent_frame_id, child_frame_id, duration, rate):
00015 self.duration = rospy.get_param('~duration', duration)
00016 self.parent_frame_id = rospy.get_param('~parent_frame_id',
00017 parent_frame_id)
00018 self.child_frame_id = rospy.get_param('~child_frame_id',
00019 child_frame_id)
00020 rate = rospy.get_param('~rate', rate)
00021 self.pub = rospy.Publisher('~output', TransformStamped, queue_size=1)
00022 self.listener = tf.TransformListener()
00023 rospy.Timer(rospy.Duration(1. / rate), self._publish_transform)
00024
00025 def _publish_transform(self, event):
00026 try:
00027 now = rospy.Time.now()
00028 self.listener.waitForTransform(
00029 self.parent_frame_id,
00030 self.child_frame_id,
00031 now,
00032 timeout=rospy.Duration(self.duration)
00033 )
00034 except Exception:
00035 rospy.logerr(
00036 "cannot get transform from '%s' to '%s' in '%.2f' [s]" %
00037 (self.parent_frame_id, self.child_frame_id, self.duration))
00038 return
00039
00040 trans, rot = self.listener.lookupTransform(
00041 self.parent_frame_id,
00042 self.child_frame_id,
00043 now
00044 )
00045 tf_msg = Transform(
00046 translation=Vector3(
00047 x=trans[0],
00048 y=trans[1],
00049 z=trans[2]
00050 ),
00051 rotation=Quaternion(
00052 x=rot[0],
00053 y=rot[1],
00054 z=rot[2],
00055 w=rot[3]
00056 )
00057 )
00058 tf_stamped_msg = TransformStamped(
00059 header=Header(
00060 stamp=now,
00061 frame_id=self.parent_frame_id
00062 ),
00063 child_frame_id=self.child_frame_id,
00064 transform=tf_msg
00065 )
00066 self.pub.publish(tf_stamped_msg)
00067
00068 if __name__ == '__main__':
00069 parser = argparse.ArgumentParser()
00070 parser.add_argument('parent_frame_id', nargs='?',
00071 help='parent frame id', default=None)
00072 parser.add_argument('child_frame_id', nargs='?',
00073 help='child frame id', default=None)
00074 parser.add_argument('--duration', '-d', type=float, default=1,
00075 help='Duration to resolve tf. default: 1 [s]')
00076 parser.add_argument('--rate', '-r', type=float, default=1,
00077 help='Rate of publication. default: 1 [hz]')
00078 args = parser.parse_args(rospy.myargv()[1:])
00079 rospy.init_node('tf_to_transform')
00080 tf_pub = TFToTransform(
00081 args.parent_frame_id,
00082 args.child_frame_id,
00083 args.duration,
00084 args.rate,
00085 )
00086 rospy.spin()