22
33import rospy
44import geometry_msgs .msg
5- import tf
5+ import tf2_ros
66
77def callback (newPose ):
88 """Listens to a transform between from_frame and to_frame and publishes it
99 as a pose with a zero covariance."""
10- global publisher , tf_listener , from_frame , to_frame
10+ global publisher , tf_buffer , tf_listener , from_frame , to_frame
1111
1212 # Listen to transform and throw exception if the transform is not
1313 # available.
1414 try :
15- (trans , rot ) = tf_listener .lookupTransform (
16- from_frame , to_frame , rospy .Time (0 ))
17- except (tf .LookupException , tf .ConnectivityException ,
18- tf .ExtrapolationException ):
15+ trans = tf_buffer .lookup_transform (from_frame , to_frame , rospy .Time ())
16+ except (tf2_ros .LookupException , tf2_ros .ConnectivityException , tf2_ros .ExtrapolationException ):
1917 return
2018
2119 # Create and fill pose message for publishing
2220 pose = geometry_msgs .msg .PoseWithCovarianceStamped ()
23- pose .header .stamp = rospy .Time (0 )
24- pose .header .frame_id = from_frame
25- pose .pose .pose .position .x = trans [0 ]
26- pose .pose .pose .position .y = trans [1 ]
27- pose .pose .pose .position .z = trans [2 ]
28- pose .pose .pose .orientation .x = rot [0 ]
29- pose .pose .pose .orientation .y = rot [1 ]
30- pose .pose .pose .orientation .z = rot [2 ]
31- pose .pose .pose .orientation .w = rot [3 ]
21+ pose .header = trans .header
22+ pose .pose .pose .position = trans .transform .translation
23+ pose .pose .pose .orientation = trans .transform .rotation
3224
3325 # Since tf transforms do not have a covariance, pose is filled with
3426 # a zero covariance.
@@ -45,14 +37,15 @@ def callback(newPose):
4537def main_program ():
4638 """ Main function initializes node and subscribers and starts
4739 the ROS loop. """
48- global publisher , tf_listener , from_frame , to_frame
40+ global publisher , tf_buffer , tf_listener , from_frame , to_frame
4941 rospy .init_node ('tf_to_pose_publisher' )
5042 # Read frame id's for tf listener
5143 from_frame = rospy .get_param ("~from_frame" )
5244 to_frame = rospy .get_param ("~to_frame" )
5345 pose_name = str (to_frame ) + "_pose"
5446
55- tf_listener = tf .TransformListener ()
47+ tf_buffer = tf2_ros .Buffer ()
48+ tf_listener = tf2_ros .TransformListener (tf_buffer )
5649 publisher = rospy .Publisher (
5750 pose_name , geometry_msgs .msg .PoseWithCovarianceStamped , queue_size = 10 )
5851
0 commit comments