Robotics StackExchange | Archived questions

Help with tf2_ros

I need to use tf2ros because tf doesnt work in python 3. I want to do what I did with tf below,but with tf2ros. I just want to transform points from one frame to another. DOes anyone know a simple script to use to make this happen?

   def transform(self,coord,from_frame='map',to_frame='base_scan',rtn=None):
        if rtn is None:
            rtn = rospy.Time(0)
        self.scope.header.frame_id = from_frame
        self.scope.header.stamp = rtn #rospy.get_rostime() # rospy.TIme(0)
        self.scope.point.x = coord[0]
        self.scope.point.y = coord[1]
        self.scope.point.z = 0
        try:
            self.listener.waitForTransform(from_frame, to_frame, rtn, rospy.Duration(1))
            p = self.listener.transformPoint(to_frame, self.scope)
            return [p.point.x,p.point.y]
        except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
            self.listener.waitForTransform(from_frame, to_frame, rospy.Time(0), rospy.Duration(1))
            p = self.listener.transformPoint(to_frame, self.scope)
            return [p.point.x, p.point.y]
            raise

Asked by distro on 2022-04-18 15:32:18 UTC

Comments

The tf2 wiki page has a simple example. Are you trying to accomplish something different?

Asked by Akhil Kurup on 2022-04-18 16:43:40 UTC

@Akhil Kurup No this doesnt answer my question, I'm looking to transform anypount anywhere on the map, not neccesarily where my robot is located.

Asked by distro on 2022-04-18 20:35:59 UTC

I don't understand your question. In the above example, you can lookup transform from and to any frame (not just the robot location). trans = tfBuffer.lookup_transform(frame1, frame2, rospy.Time())

Asked by Akhil Kurup on 2022-04-18 20:42:39 UTC

You can access the specific transforms using trans.transform.(translation/rotation) and use numpy to apply these transforms. numpy.dot(transform matrix, target pose)

Asked by Akhil Kurup on 2022-04-18 20:46:45 UTC

@Akhil Kurup I was able to get tf2_ros to do what I want now, but only in python2.7, when I try to import tf2_ros in python3 environment I get:

Traceback (most recent call last):
  File "/home/philip/server_prac/src/action_practice/src/tester.py", line 8, in <module>
    import tf2_ros
  File "/opt/ros/melodic/lib/python2.7/dist-packages/tf2_ros/__init__.py", line 38, in <module>
    from tf2_py import *
  File "/opt/ros/melodic/lib/python2.7/dist-packages/tf2_py/__init__.py", line 38, in <module>
    from ._tf2 import *
ImportError: dynamic module does not define module export function (PyInit__tf2)

Asked by distro on 2022-04-18 21:40:17 UTC

I need to use tf2_ros because tf doesnt work in python 3.

It does work with Python3.

Asked by ravijoshi on 2022-04-18 22:09:22 UTC

@Ravi Joshi well I just realized my tf and tf2_ros arent working in python3.

Asked by distro on 2022-04-19 02:41:03 UTC

Please check your ROS installation.

Asked by ravijoshi on 2022-04-19 07:24:09 UTC

Your errors above indicate that you are using melodic. ROS melodic does not work with Python3. You will need to upgrade to ROS noetic.

Asked by Akhil Kurup on 2022-04-19 10:34:31 UTC

Answers