Rosanswers logo

Platform: ubuntu 20.02 ROS2: Galactic, installed through 'apt'



Currently, I have a static tf2 transform between "map" and "planner" frame. The relationship is shown below.

$ ros2 run tf2_ros tf2_echo map planner
[INFO] [1688158439.517036056] [tf2_echo]: Waiting for transform map ->  planner: Invalid frame ID "map" passed to canTransform argument target_frame - frame does not exist
At time 0.0
- Translation: [0.000, -1.000, 0.000]
- Rotation: in Quaternion [0.000, 0.000, -0.383, 0.924]

Basically, the "planner" frame is offset from the "map" frame by 1 meter in the y-axis and -45 degree in yaw ( all in ros2 unit standard).

However, when I tried to use the tf_buffer to transform (-0.645, -0.705) in "map" frame to "planner" frame, I got

x -0.6646803743153546 y -0.2474873734152917

, which is not the answer I expected. Originally, I was expecting both x should be less than 0.5 meters from the origin in "planner" frame. image description

Because base on the x-y graph below (I have transformed the point from ROS standard to normal cartesian coordinate), I was expecting the |x-value| of "planner" frame is < 0.5 meters. , but it turned out to be -0.66468

code for doing the transform

def on_timer(self):
    # my_pose = Pose()

    # my_pose.position.x =  -0.745000
    # my_pose.position.y = -0.705000
    # my_pose.position.z = 0.0
    # my_pose.orientation.x = 0.0
    # my_pose.orientation.y = 0.0
    # my_pose.orientation.z = 0.0
    # my_pose.orientation.w = 1.0
    # testPose = PoseStamped()
    # testPose.pose = my_pose
    # testPose.header.frame_id = "map"
    # testPose.header.stamp = self.get_clock().now().to_msg()
    # resultPose = self.tf_buffer.transform(PoseStamped, "planner", rclpy.time.Time()  )
    # self.get_logger().info(" x " + str(resultPose.pose.position.x) + "y " + str(resultPose.pose.position.y))
    # Define the input pose in the source frame
    source_pose = PoseStamped()
    source_pose.header.frame_id = 'map'
    source_pose.pose.position.x = -0.645
    source_pose.pose.position.y = -0.705
    source_pose.pose.position.z = 0.0
    source_pose.pose.orientation.x = 0.0
    source_pose.pose.orientation.y = 0.0
    source_pose.pose.orientation.z = 0.0
    source_pose.pose.orientation.w = 1.0
    # q = quaternion_from_euler(0, 0, -45* (3.14/180))
    # source_pose.pose.orientation.x = q[0]
    # source_pose.pose.orientation.y = q[1]
    # source_pose.pose.orientation.z = q[2]
    # source_pose.pose.orientation.w = q[3]

    # Transform the pose to the target frame
    d = Duration()
        target_pose = self.tf_buffer.transform(source_pose, 'planner', d  )
        self.get_logger().info("Transformed pose: x  "+ str( target_pose.pose.position.x) +  " y " + str( target_pose.pose.position.y))
    except tf2_ros.TransformException as e:
        self.get_logger().error("Transform failed: %s", str(e))

Thus, I am wondering if this is a bug in the tf2? Or could I have misunderstood something?

Originally posted by sdu568 on ROS Answers with karma: 45 on 2023-06-30

Post score: 0


1 Answer 1


Rosanswers logo

The results look to be consistent with what you're stating as the problem.

I'm not sure why you expect the point of interest to be "just about 0.5m away from the origin of the planner frame" For that simple statement if you look at your lower plot it shows it being at a similar distance as (0.5,0,5) if you remember that the distance on the diagonal is going to be .707 m to that point from both the origin of the map and the origin of the planner. Your result is very similar but rotated slightly.

I'd suggest that you try doing the math out yourself by hand fully with a calculator or in python to verify. You can probably take out the rotation to simplify the manual math too at first.

The math in tf2 is very heavily used and well tested making a computational error very unlikely.

Originally posted by tfoote with karma: 58457 on 2023-06-30

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by sdu568 on 2023-06-30:
First of all, thank you for your quick response. I am sorry. I think I might need to rephrase my question. Indeed, the distance is about 0.707 meters from the origin of the "planner" frame.

However, my real expectation is that the |x value| (in ros standard) of the "planner" frame should be < 0.5 meters, as refect in the updated graph.

I have updated the question description.

Comment by sdu568 on 2023-07-01:
After reading your answer and doing some calculations, I realize that it is my fault. The length of the square should be 0.707m , not 0.5 meters. Thank you very much for your help!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.