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

tf not found using python in ros noetic

asked 2022-11-03 09:09:06 -0500

robra gravatar image

updated 2022-11-03 09:13:17 -0500

Hi

I have followed the tutorial here to find a transformation between 2 frames using Python in ROS Noetic, but I can't get it even though I'm sure the tf exists because I can find it in the terminal.

Here is what I get in the terminal:

$ rosrun tf tf_echo /base_link /tool0
    At time 1667484434.367
    - Translation: [-0.197, 0.704, 0.486]
    - Rotation: in Quaternion [1.000, 0.000, -0.000, -0.000]
                in RPY (radian) [-3.141, 0.001, 0.000]
                in RPY (degree) [-179.967, 0.041, 0.028]
    At time 1667484434.967
    - Translation: [-0.197, 0.704, 0.486]
    - Rotation: in Quaternion [1.000, 0.000, -0.000, -0.000]
                in RPY (radian) [-3.141, 0.001, 0.000]
                in RPY (degree) [-179.967, 0.041, 0.028]
    At time 1667484435.967
    - Translation: [-0.197, 0.704, 0.486]
    - Rotation: in Quaternion [1.000, 0.000, -0.000, -0.000]
                in RPY (radian) [-3.141, 0.001, 0.000]
                in RPY (degree) [-179.967, 0.041, 0.028]

Here is a minimal code:

#!/usr/bin/env python3
import sys
import rospy
import moveit_commander
import tf2_ros

class ourbot(object):
    def __init__(self):
        super(ourbot, self).__init__()

        moveit_commander.roscpp_initialize(sys.argv)
        rospy.init_node("get_tf", anonymous=True)

def main():
    try:    
        demorobot = ourbot()

        tfBuffer = tf2_ros.Buffer()
        listener = tf2_ros.TransformListener(tfBuffer)

        try:
            trans = tfBuffer.lookup_transform('base_link', 'tool0', rospy.Duration(3.0))
            pose_transformed = tf2_geometry_msgs.do_transform_pose(pose_robot, trans)
            print(pose_transformed)
        except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):
            print('not found')     
    except rospy.ROSInterruptException:
        return
    except KeyboardInterrupt:
        return


if __name__ == "__main__":
    main()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-11-03 16:49:09 -0500

Mike Scheutzow gravatar image

I think you have two problems with this code:

  1. The third argument to lookup_transform() is a rospy.Time object, as in time-of-day, not a Duration object. Try using rospy.Time(0).

  2. Your tfBuffer needs some time to actually receive the messages from the /tf topic. You must either 1) explicitly specify timeout=Duration(3.0), or 2) insert a big-enough delay before the call, or 3) loop calling the tfBuffer.can_transform() method until the tfBuffer says it is ready to provide the information.

edit flag offensive delete link more

Comments

Thank you, I actually forgot to update the question, but I had already changed this line: trans = tfBuffer.lookup_transform('base_link', 'tool0', rospy.Duration(3.0)) to trans = tfBuffer.lookup_transform('base_link', 'tool0', rospy.Time.now(), rospy.Duration(3.0)) as in this tutorial but that didn't work either. However, using the rospy.Time(0) instead of rospy.Time.now(), works!

robra gravatar image robra  ( 2022-11-04 05:35:24 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-11-03 09:09:06 -0500

Seen: 138 times

Last updated: Nov 03 '22