ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

The problem is not with the tf or ROS. You are using %d as a format string, which will round float values to integers. Coincidentally, our values are less than 1. Thus, they are rounded to 0.

I am surprised and confused at the same time after seeing the following statement:

    rospy.logdebug(
        "tx: %f, ty: %f, tz: %f, rx: %f, ry: %f, rz: %f", tx, ty, tz, rx, ry, rz
    )

I recommend, using the following statement instead:

    rospy.logdebug(trans)

Below is the code snippet:

#!/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)
        self.timer = rospy.Timer(rospy.Duration(1), self.tfListenerCallback)

    def tfListenerCallback(self, event):
        try:
            trans = self.tf_buffer.lookup_transform("tool0", "base_link", rospy.Time())
        except (
            tf2_ros.LookupException,
            tf2_ros.ConnectivityException,
            tf2_ros.ExtrapolationException,
        ):
            rospy.logdebug("Can't transform")
            return

        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(trans)
        rospy.logdebug(
            "tx: %f, ty: %f, tz: %f, rx: %f, ry: %f, rz: %f", tx, ty, tz, rx, ry, rz
        )


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

I used random values to publish a transformation as shown below:

ravi@dell:~/ros_ws$ rosrun tf2_ros static_transform_publisher 0.4 0.2 0.3 0.1 0.2 0.3 base_link tool0
[ INFO] [1666599679.602630460]: Spinning until killed publishing base_link to tool0

Then, I run your code to print the transformation. Please see below:

ravi@dell:~/ros_ws$ rosrun beginner_tutorials tf.py 
[DEBUG] [1666600470.164958]: init_node, name[/tf_listener], pid[71067]
[DEBUG] [1666600470.167868]: binding to 0.0.0.0 0
[DEBUG] [1666600470.170540]: bound to 0.0.0.0 38651
[DEBUG] [1666600470.173766]: ... service URL is rosrpc://dell:38651
[DEBUG] [1666600470.176383]: [/tf_listener/get_loggers]: new Service instance
[DEBUG] [1666600470.181235]: ... service URL is rosrpc://dell:38651
[DEBUG] [1666600470.183920]: [/tf_listener/set_logger_level]: new Service instance
[DEBUG] [1666600470.191242]: ... service URL is rosrpc://dell:38651
[DEBUG] [1666600470.195970]: [/tf_listener/tf2_frames]: new Service instance
[DEBUG] [1666600470.211223]: node[/tf_listener, http://dell:39269/] entering spin(), pid[71067]
[DEBUG] [1666600470.220975]: connecting to tm 58745
[DEBUG] [1666600470.312265]: header: 
  seq: 0
  stamp: 
    secs: 0
    nsecs:         0
  frame_id: "tool0"
child_frame_id: "base_link"
transform: 
  translation: 
    x: -0.3500360106436593
    y: -0.2633910550476512
    z: -0.31320910487014486
  rotation: 
    x: -0.14357217502739192
    y: -0.10602051106179562
    z: -0.03427079855048211
    w: 0.9833474432563559
[DEBUG] [1666600470.316059]: tx: -0.350036, ty: -0.263391, tz: -0.313209, rx: -0.143572, ry: -0.106021, rz: -0.034271