Re-publishing transformed topics in ROS
There are situations when we need to republish the messages from the tf
-topic
to a separate node. This guide provides a sample python script showing how to create a new
node, subscribe to the tf
-topic and republish the data to the created node.
The code
import rospy
import tf
from geometry_msgs.msg import Transform, Vector3, Quaternion
rospy.init_node('republished_node')
listener = tf.TransformListener()
publisher = rospy.Publisher('republished_topic', Transform, queue_size=1)
rate = rospy.Rate(10.0)
while not rospy.is_shutdown():
try:
(trans,rot) = listener.lookupTransform('/topic_to_republish', '/parent_topic', rospy.Time(0))
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
continue
publisher.publish(Transform(Vector3(*trans), Quaternion(*rot)))
rate.sleep()
Explanation
We first initialize a new node
rospy.init_node('republished_node')
and create a publisher to a new topic republished_topic
listener = tf.TransformListener()
publisher = rospy.Publisher('republished_topic', Transform, queue_size=1)
We take the transformation from topic_to_republish
to parent_topic
(trans,rot) = listener.lookupTransform('/topic_to_republish', '/parent_topic', rospy.Time(0))
Finally, we publish the transformation to the node republished_node
publisher.publish(Transform(Vector3(*trans), Quaternion(*rot)))