TF Programming: broadcasting and listening coordinate transformation robot
Example: broadcasting and listening coordinate transformation robot, known coordinates relations laser radar and robot chassis, solving the coordinate values of lidar data in the coordinate system chassis?
TF ROS programming is for embedded advanced learning, help us to understand the movement of the robot in real time and obtain location information, learn programming TF, TF our guidance of feature pack, what is the function of ROS TF package?
ROS of TF Feature Pack:
TF function package, may be obtained by converting the broadcast and listen TF TF conversion as coordinate conversion relationships:
Relationship between the global coordinate system with respect to the local coordinate system of the robot.
Robot gripping the object with respect to the position of the robot center coordinate system.
Robot center coordinate system position relative to the global coordinate system.
The blog, we come to learn with TF programming examples given above, the word does not say, look at it!
A set TF broadcaster
1, into the project package
cd ~/ros/src/comm/src
Note: ros ros is my work space, comm own ros engineering package, small partners according to their modify Oh!
2, create a file TF broadcastbroadCaster.cpp
touch broadCaster.cpp
3, open the file TF broadcast
gedit broadCaster.cpp
4, the following code is written in c ++ language broadcast files TF
/**
* 该例程产生tf数据,并计算、发布base_laser的位置指令
*/
#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
int main(int argc, char** argv){
// 初始化ROS节点
ros::init(argc, argv, "robot_tf_broadcaster");
// 订阅base_link的位置话题
ros::NodeHandle node;
// 创建tf的广播器
static tf::TransformBroadcaster br;
while(node.ok()){
// 初始化tf数据
tf::Transform transform;
transform.setOrigin( tf::Vector3(0.1, 0.0, 0.2) );
transform.setRotation( tf::Quaternion(0,0,0,1) );
// 广播base_link与base_laser坐标系之间的tf数据
br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "base_link", "base_laser"));
}
return 0;
};
Here, our TF broadcast file is created, as shown below:
Second, set the listener TF
1. Create a folder TF listeners files in the current folderrobListener.cpp
Create a new terminal, we namedTerminal 1, Enter the following command:
touch robListener.cpp
2, open the file TF monitorrobListener.cpp
gedit robListener.cpp
3, the following c ++ code into TF monitor file
/**
* 该例程监听tf数据,并计算、发布base_laser的位置指令
*/
#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/PointStamped.h>
int main(int argc, char** argv){
// 初始化ROS节点
ros::init(argc, argv, "robot_tf_listener");
// 创建节点句柄
ros::NodeHandle node;
// 创建tf的监听器
tf::TransformListener listener;
ros::Rate rate(10.0);
while (node.ok()){
//我们将在base_laser帧中创建一个要转换为base_link帧的点
geometry_msgs::PointStamped laser_point;
laser_point.header.frame_id = "base_laser";
//我们将在我们的简单示例中使用最近可用的转换
laser_point.header.stamp = ros::Time();
//laser_point检测点获取
laser_point.point.x = 0.3;
laser_point.point.y = 0.0;
laser_point.point.z = 0.0;
try{
// 等待获取监听信息base_link和base_laser
listener.waitForTransform("base_link", "base_laser", ros::Time(0), ros::Duration(3.0));
geometry_msgs::PointStamped base_point;
listener.transformPoint("base_link", laser_point, 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,
base_point.point.x, base_point.point.y, base_point.point.z, base_point.header.stamp.toSec());
}
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, we monitor the TF file has been created, as follows:
the next step is to modify the file CMakeLists.txt it!
Third, modifyCMakeLists.txtfile
1, into the engineering package comm
cd ~/ros/src/comm/
2, OpenCMakeLists.txtfile
gedit CMakeLists.txt
3, inCMakeLists.txtTF file is added at the end of the following items to compile the code:
add_executable(broadCaster src/broadCaster.cpp)
target_link_libraries(broadCaster ${catkin_LIBRARIES})
add_executable(robListener src/robListener.cpp)
target_link_libraries(robListener ${catkin_LIBRARIES})
As follows:
4, find the file CMakeLists.txtfind_packageA function, add the following in which TF attributes:
tf
As follows:
here, our CMakeLists.txt file on the amendment done, click Save and close the file!
Fourth, the compiled project file
1, into the work space ros
cd ~/ros
2, compiled files
catkin_make
Compile progress appears below 100%, which compile the code is successful, if unsuccessful, can be modified according to the error message prompts:
3, registration procedures
Let us write code to take effect
source ./devel/setup.bash
Fifth, test run results
1, start ros
1), a new terminal, we commandTerminal 2And then start
roscore
2, inTerminal 2Keeping time does not move, a new terminal once again, we namedTerminal 3
3, inTerminal 3Enter ros workspace
cd ~/ros
4, inTerminal 3Procedural register
source ./devel/setup.bash
5, inTerminal 3Run TF listener
In the following code input terminal 3, busy press Enter
rosrun comm robListener
6, inTerminal 1TF broadcast program run
1), inTerminal 1Enter the following command, and then press Enter, followed inTerminal 3Enter changes observed terminals 1 and 3
rosrun comm broadCaster
After running, it can be observed in FIG follows:
the figure can be seen, the terminal 3 can obtain the relationship between the coordinates of the listening base_laser and at the base_link.
2), Ctrl + C to close running programs, as follows:
The above is the entire contents of this blog, I hope this blog, for small partners learn ROS TF programming have some help Oh! Thank you for reading, remember to point praise, attention Oh! Problems encountered junior partner in the comments area message, to discuss the problem with it! Lin Jun seniors patience as we resolve Oh!
Chen programming years and one day in January ^ _ ^