Help with tf2_ros

asked 2022-04-18 15:32:18 -0500

distro gravatar image

I need to use tf2_ros because tf doesnt work in python 3. I want to do what I did with tf below,but with tf2_ros. 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
edit retag flag offensive close merge delete

Comments

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

Akhil Kurup gravatar image Akhil Kurup  ( 2022-04-18 16:43:40 -0500 )edit

@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.

distro gravatar image distro  ( 2022-04-18 20:35:59 -0500 )edit

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())

Akhil Kurup gravatar image Akhil Kurup  ( 2022-04-18 20:42:39 -0500 )edit

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

Akhil Kurup gravatar image Akhil Kurup  ( 2022-04-18 20:46:45 -0500 )edit

@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)
distro gravatar image distro  ( 2022-04-18 21:40:17 -0500 )edit

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

It does work with Python3.

ravijoshi gravatar image ravijoshi  ( 2022-04-18 22:09:22 -0500 )edit

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

distro gravatar image distro  ( 2022-04-19 02:41:03 -0500 )edit

Please check your ROS installation.

ravijoshi gravatar image ravijoshi  ( 2022-04-19 07:24:09 -0500 )edit