# ROS TF programming -- broadcast and monitor coordinate transformation of robot

## TF programming: broadcast and monitor coordinate transformation of robot

Example: broadcast and monitor the coordinate transformation of robot, know the coordinate relationship between lidar and robot chassis, and solve the coordinate value of lidar data in the chassis coordinate system?

TF programming of ROS is an advanced learning for embedded system, which helps us to understand the way of robot motion and real-time position information acquisition. Learning TF programming, we will guide TF function package, what is TF function package of ROS?
TF function package of ROS:
TF function package can obtain the following coordinate transformation relations through broadcast TF transformation and monitor TF Transformation:
The relationship between local coordinate system and global coordinate system of robot.
The position of the object gripped by the robot relative to the center coordinate system of the robot
The position of the robot center coordinate system relative to the global coordinate system.
This blog, let's use the above examples to learn TF programming. Let's take a look together!

### 1, Set up TF broadcaster

#### 1. Enter project package

```cd ~/ros/src/comm/src
```

Note: ros is my ros workspace, and comm is my own ros project package. My friends can modify it according to their own!

```touch broadCaster.cpp
```

#### 3. Open TF broadcast file

```gedit broadCaster.cpp
```

#### 4. Write the following c + + code to the TF broadcast file

```/**
* This routine generates tf data and computes and issues the location instruction of base laser
*/
#include <ros/ros.h>
int main(int argc, char** argv){
// Initialize ROS node

// Subscribe to the location topic of base link
ros::NodeHandle node;

while(node.ok()){
// Initialize tf data
tf::Transform transform;
transform.setOrigin( tf::Vector3(0.1, 0.0, 0.2) );
transform.setRotation( tf::Quaternion(0,0,0,1) );

// Broadcast tf data between base link and base laser coordinate system
}
return 0;
};
```

Here, our TF broadcast file is created, as follows:

### 2, Set TF listener

#### 1. Create TF listener file robListener.cpp in current folder

To create a new terminal, we name it terminal 1, and enter the following command:

```touch robListener.cpp
```

#### 2. Open TF listening file robListener.cpp

```gedit robListener.cpp
```

#### 3. Write the following c + + code to TF listening file

```/**
* This routine listens for tf data, calculates and issues the location instruction of base laser
*/
#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/PointStamped.h>
int main(int argc, char** argv){

// Initialize ROS node
ros::init(argc, argv, "robot_tf_listener");

// Create node handle
ros::NodeHandle node;

// Create listener for tf
tf::TransformListener listener;
ros::Rate rate(10.0);
while (node.ok()){

//We will create a point in the base laser frame to convert to the base link frame
geometry_msgs::PointStamped laser_point;

//We will use the most recently available transformations in our simple example

//Acquisition of laser point
laser_point.point.x = 0.3;
laser_point.point.y = 0.0;
laser_point.point.z = 0.0;
try{
// Wait for base link and base laser
geometry_msgs::PointStamped base_point;

ROS_INFO("base_laser: (%.2f, %.2f. %.2f) -----> base_link: (%.2f, %.2f, %.2f) at time %.2f",
laser_point.point.x, laser_point.point.y, laser_point.point.z,
}
catch(tf::TransformException& ex){
ROS_ERROR("Received an exception trying to transform a point from \"base_laser\" to \"base_link\": %s", ex.what());
}

rate.sleep();
}
return 0;
};
```

Here, our TF listening file is created, as follows:

Next is to modify the CMakeLists.txt file!

### 3, Modify CMakeLists.txt file

#### 1. Enter project package comm

```cd ~/ros/src/comm/
```

#### 2. Open CMakeLists.txt file

```gedit CMakeLists.txt
```

#### 3. Add the following TF project compilation code at the end of CMakeLists.txt file:

```add_executable(broadCaster src/broadCaster.cpp)
```

As follows:

#### 4. Find an item in the CMakeLists.txt file in the find ﹣ package function, and add the following TF attribute:

```tf
```

As follows:

Here, our CMakeLists.txt file will be modified. Click Save to close the file!

### 4, Compiling project files

#### 1. Enter ros workspace

```cd ~/ros
```

#### 2. Compile file

```catkin_make
```

The following compilation progress is 100%. The code is compiled successfully. If it is not, you can modify it according to the error message prompted:

#### 3. Program registration

Let the code we write take effect

``` source ./devel/setup.bash
```

### 5, Test run results

#### 1. Start ros

1) . create a new terminal. We command terminal 2 and start it

```roscore
```

#### 3. Enter ros workspace at Terminal 3

```cd ~/ros
```

#### 4. Program registration at Terminal 3

``` source ./devel/setup.bash
```

#### 5. Run TF monitor on Terminal 3

Enter the following code in terminal 3 and press enter when not busy

```rosrun comm robListener
```

#### 6. Run TF broadcast program at terminal 1

1) Input the following command at terminal 1, then enter, then enter at Terminal 3, observe the change of terminals 1 and 3

```rosrun comm broadCaster
```

After running, we can observe the following situation:

As can be seen from the above figure, terminal 3 can obtain the coordinate relationship between base laser and base link at the monitor.
2) , Ctrl+C to close the running program, as shown in the following figure:

The above is the whole content of this blog. I hope this blog can help you to learn ROS TF programming! Thank you for reading, remember to like and pay attention! If you have problems, please leave a message in the comment area and discuss the problem together! Lin Jun is patient to solve this problem for you!
Another programming year in January^

Published 29 original articles, won praise 19, visited 4472

Tags: Programming Attribute

Posted on Tue, 10 Mar 2020 02:34:36 -0400 by elabuwa