Transform a pose to another frame with tf2 in Python
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.
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
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.
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:
pose_stamped.header.frame_id,
# get the tf at the time the pose was valid
pose_stamped.header.stamp,
# wait for at most 1 second for transform, otherwise throw
rospy.Duration(1.0))
pose_transformed = tf2_geometry_msgs.do_transform_pose(pose_stamped, transform)
The lookup should better be wrapped inside a try-except block in case the transform is not found: http://wiki.ros.org/tf2/Tutorials/Wri...
Asked: 2015-12-10 07:10:08 -0500
Seen: 16,444 times
Last updated: Nov 18 '21
How do I test the ROS version in Python code?
How to specify port for topic?
JointTrajectory Python interface
Windows: [rosrun] Couldn't find executable named
image_geometry PinholeCameraModel Python not importing properly when installed using debian package
rospy subscriber delay, not giving the latest msg
Handling a delayed localization pose?