Welcome to JetCobot repository
23.ROS2 TF2 coordinate transformation
23. ROS2 TF2 Coordinate Transformation
1. Introduction to TF2
Coordinate systems are a very familiar concept and a key foundation in robotics. In a complete robotic system, there are many coordinate systems. How do we manage the positional relationships between these coordinate systems? ROS provides a powerful coordinate system management tool: TF2.
TF System Reference: tf: The transform library | IEEE Conference Publication | IEEE Xplore
2. Coordinate Systems in Robotics
Coordinate systems are also crucial in mobile robot systems. For example, the center point of a mobile robot is the base coordinate system (Base Link), and the position of the radar is called the laser link. As the robot moves, the odometry accumulates its position. The reference system for this position is called the odom coordinate system (odom). The odometry itself is subject to accumulated errors and drift. The reference system for absolute position is called the map coordinate system (map).
The relationships between layers of coordinate systems are complex. Some are relatively fixed, while others are constantly changing. Even seemingly simple coordinate systems can become complex within a spatial context, making a good coordinate system management system extremely important.

The basic theory of coordinate system transformations is explained in every robotics textbook. It can be broken down into two components: translation and rotation. These components are described by a four-by-four matrix. When plotting a coordinate system in space, the transformation between these two components is essentially a mathematical description of vectors.
The underlying principles of the TF functionality in ROS encapsulate these mathematical transformations. For detailed theoretical knowledge, please refer to robotics textbooks. We will primarily explain how to use the TF coordinate management system.
3. TF Command Line Operations
Let's first use an example with two turtles to understand a robot following algorithm based on coordinate systems. For ease of demonstration, this lesson is best conducted in a virtual machine
3.1 Installing Related Tools
This example requires installing the corresponding function packages, the TF turtle simulator example, and the TF tree visualization tool.
xxxxxxxxxxsudo apt install ros-${ROS_DISTRO}-turtle-tf2-py ros-humble-tf2-toolssudo pip3 install transforms3dsudo apt install ros-${ROS_DISTRO}-rqt-tf-tree
3.2 Starting Up
Then you can start the system using a launch file. You can then control one of the turtles, and the other will automatically follow.
xxxxxxxxxxros2 launch turtle_tf2_py turtle_tf2_demo.launch.pyros2 run turtlesim turtle_teleop_keyWhen we control the movement of one turtle, the other turtle will follow.

3.3 Viewing the TF Tree
xxxxxxxxxxros2 run rqt_tf_tree rqt_tf_tree
You can view the TF transformation tree in the rqt window.

3.4 Querying Coordinate Transformation Information
Just viewing the coordinate system structure is not enough. If we want to know the specific relationship between two coordinate systems, we can use the tf2_echo tool:
xxxxxxxxxxros2 run tf2_ros tf2_echo turtle2 turtle1After running successfully, the terminal will repeatedly print the coordinate system transformation values.

3.5 Coordinate System Visualization
- Run rviz2 and add the TF display plugin.
xxxxxxxxxxrviz2
Set the reference coordinate system in rviz2 to "world," add the TF display plugin, and then animate the turtle. The coordinate axes in rviz will begin to move. Isn't this more intuitive?
