Tf2 lookup_transform problem

Hello everyone, I have a problem with my tf2 transform listener node, I want to get the transform between two frames. This is the code I’m using:

#!/usr/bin/env python

import rospy
import tf2_ros

class UR3TfListener(object):

    def __init__(self):

        self.tf_buffer = tf2_ros.Buffer()
        self.listener = tf2_ros.TransformListener(self.tf_buffer)
 
        rate = rospy.Rate(10)
 
        while not rospy.is_shutdown():
            if self.tf_buffer.can_transform('base_footprint','odom',rospy.Time(0)):
                trans = self.tf_buffer.lookup_transform('base_footprint','odom',rospy.Time(0))
            else:
                rospy.logdebug("Can't transform")
                rate.sleep()
                continue

            tx = trans.transform.translation.x
            ty = trans.transform.translation.y
            tz = trans.transform.translation.z
            rx = trans.transform.rotation.x
            ry = trans.transform.rotation.y
            rz = trans.transform.rotation.z

            rospy.logdebug("tx: %d, ty: %d, tz: %d, rx: %d, ry: %d, rz: %d", tx, ty, tz, rx, ry, rz)
 
            rate.sleep()

if __name__ == '__main__':
    rospy.init_node('tf_listener', anonymous=True, log_level=rospy.DEBUG)
    tf_listener = UR3TfListener()
    rospy.spin()

The problem is that the result I’m getting are like this:

[DEBUG] [1666494690.406768, 199.024000]: tx: 0, ty: 0, tz: 0, rx: 0, ry: 0, rz:0

And I know the transform shouldn’t be 0 because the frames look like this in Rviz
Image

Hi, maybe this is happening because of bad timing. You can try waiting for the transform instead of lookup_transform, something like

tf_buffer.waitForTransform("/base_footprint", “/odom”, rospy.Time.now(), rospy.Duration(4.0))

1 Like

It was much simpler than that hahaha, I was using %d in the rospy.logdebug() and that was rounding the output, and because the transform has numbers between 0 and 1 it was being rounded to 0. But thanks for your help. :slight_smile:

1 Like