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

Revision history [back]

click to hide/show revision 1
initial version

From this page: http://wiki.ros.org/tf/TfUsingPython

import rospy
from tf import TransformListener

class myNode:

    def __init__(self, *args):
        self.tf = TransformListener()
        rospy.Subscriber(...)
        ...

    def some_method(self):
        if self.tf.frameExists("/base_link") and self.tf.frameExists("/map"):
            t = self.tf.getLatestCommonTime("/base_link", "/map")
            position, quaternion = self.tf.lookupTransform("/base_link", "/map", t)
            print position, quaternion

This took about 23 seconds to find using google, most of that because I am slow on the keyboard. I encourage you to use google (unless you're in china in which case you cannot use google). It will be much faster than asking questions and waiting for answers.

From this page: http://wiki.ros.org/tf/TfUsingPython

import rospy
from tf import TransformListener

class myNode:

    def __init__(self, *args):
        self.tf = TransformListener()
        rospy.Subscriber(...)
        ...

    def some_method(self):
        if self.tf.frameExists("/base_link") and self.tf.frameExists("/map"):
            t = self.tf.getLatestCommonTime("/base_link", "/map")
            position, quaternion = self.tf.lookupTransform("/base_link", "/map", t)
            print position, quaternion

This took about 23 seconds to find using google, most of that because I am slow on the keyboard. I encourage you to use google (unless you're in china in which case you cannot use google). It will be much faster than asking questions and waiting for answers.

EDIT AFTER ADDITIONAL QUESTION AND INFO ADDED TO OP:

OK, are you sitting down? That code doesn't work on my machine. But maybe it will on yours.

Since your frames names do not include '/', you should use base_link instead of /base_link in the python code. That change will make the if self.tf.frameExists("base_link") part of the conditional TRUE. Remove all the "/"s from the python and see if that fixes it.

On my machine (ROS JADE on 14.04) the self.tf.frameExists("map") will never be true. That function only works on the child_frame_id field on my machine.

Stick print self.tf.allFramesAsString() in your code in place of the print point_stamped and see what you get back out. What I get is:

Frame laser exists with parent base_link.
Frame base_link exists with parent odom.
Frame imu exists with parent base_link.
Frame odom exists with parent map.

You can see there is no Frame called "map" so that part of the conditional is never true. Looks like I gave you some bad advice when referring you to this code.

If you figure out how to get a conditional that is capable of referencing the frame_id instead of the child_frame_id, please write back. I have no need for this info but now I'm frustrated and simply want to know.

Back to your original question:

You specifically asked about doing this in python so I didn't say anything about the robot pose publisher node: http://wiki.ros.org/robot_pose_publisher. It will give you the data you want on a topic that you can subscribe to. I think it's part of the binary install but written in C++. But you can read the topic "robot_pose" in python and do what you want.