ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Transform a pose to another frame with tf2 in Python

asked 2015-12-10 07:10:08 -0600

fivef gravatar image

updated 2021-11-18 12:03:38 -0600

lucasw gravatar image

I have a pose stamped given in camera frame. Now I want to transform the pose to get its coordinates in the base_link frame.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-12-10 07:33:15 -0600

fivef gravatar image

updated 2021-11-18 12:02:41 -0600

lucasw gravatar image

Import the needed modules:

import tf2_ros
import tf2_geometry_msgs

Initialize the tf buffer and listener somewhere in your init function:

tf_buffer = tf2_ros.Buffer(rospy.Duration(100.0))  # tf buffer length
tf_listener = tf2_ros.TransformListener(self.tf_buffer)

Do the actual transformation:

transform = tf_buffer.lookup_transform(target_frame,
                                       # source frame:
                                       # get the tf at the time the pose was valid
                                       # wait for at most 1 second for transform, otherwise throw

pose_transformed = tf2_geometry_msgs.do_transform_pose(pose_stamped, transform)
edit flag offensive delete link more



The lookup should better be wrapped inside a try-except block in case the transform is not found:

bit-pirate gravatar image bit-pirate  ( 2016-03-22 03:03:46 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2015-12-10 07:10:08 -0600

Seen: 14,082 times

Last updated: Nov 18 '21