tf教程(四):Adding a frame
2017-07-01 15:19
381 查看
Adding a frame (C++)
Description: Thistutorial teaches you how to add an extra fixed frame to tf.
Tutorial
Level: BEGINNER
Next
Tutorial: tf and time (C++)
目录
Why adding frames
Where to add frames
How to add a frame
The Code
The Code Explained
Running the frame broadcaster
Checking the results
Broadcasting a moving frame
catkin
rosbuild
In the previous tutorials we recreated the turtle demo by adding a tf broadcaster and a tf listener. This tutorial will teach you how to add an extra frame to the tf tree. This is very similar to creating the tf broadcaster, and will show some of the power
of tf.
Why adding frames
For many tasks it is easier to think inside a local frame, e.g. it is easier to reason about a laser scan in a frame at the center of the laser scanner. tf allows you to define a local frame for each sensor, link, etc in your system. And, tf will take careof all the extra frame transforms that are introduced.
Where to add frames
tf builds up a tree structure of frames; it does not allow a closed loop in the frame structure. This means that a frame only has one single parent, but it can have multiple children. Currently our tf tree contains three frames: world,turtle1 and turtle2. The two turtles are children of world. If we want to add a new frame to tf, one of the three existing frames needs to be the parent frame, and the new frame will become a child frame.
How to add a frame
In our turtle example, we'll add a new frame to the first turtle. This frame will be the "carrot" for the second turtle.Let's first create the source files. Go to the package we created for the previous tutorials:
$ roscd learning_tf
The Code
Fire up your favorite editor and paste the following code into a new file called src/frame_tf_broadcaster.cpp.切换行号显示
#include <ros/ros.h> #include <tf/transform_broadcaster.h> int main(int argc, char** argv){ ros::init(argc, argv, "my_tf_broadcaster"); ros::NodeHandle node; tf::TransformBroadcaster br; tf::Transform transform; ros::Rate rate(10.0); while (node.ok()){ transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) ); transform.setRotation( tf::Quaternion(0, 0, 0, 1) ); br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1")); rate.sleep(); } return 0; };
The code is very similar to the example in the tf broadcaster tutorial.
The Code Explained
Let's take a look at the key line in this piece of code:切换行号显示
13 transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) ); 14 transform.setRotation( tf::Quaternion(0, 0, 0, 1) ); 15 br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1"));
Here we create a new transform, from the parent turtle1 to the new child carrot1. The carrot1 frame is 2 meters offset to the left from the turtle1 frame.
Running the frame broadcaster
Now that we created the code, lets compile it first. Open the CMakeLists.txt file, and add the following line on the bottom:add_executable(frame_tf_broadcaster src/frame_tf_broadcaster.cpp) target_link_libraries(frame_tf_broadcaster ${catkin_LIBRARIES})
Build your package at the top folder of your catkin workspace:
$ catkin_make
If everything went well, you should have a binary file called frame_tf_broadcaster in your bin folder. If so, we're ready to edit the start_demo.launch launch file. Simply merge the node
block below inside the launch block::
<launch> ... <node pkg="learning_tf" type="frame_tf_broadcaster" name="broadcaster_frame" /> </launch>
First, make sure you stopped the launch file from the previous tutorial (use Ctrl-c). Now you're ready to start the turtle broadcaster demo:
$ roslaunch learning_tf start_demo.launch
Checking the results
So, if you drive the first turtle around, you notice that the behavior didn't change from the previous tutorial, even though we added a new frame. That's because adding an extra frame does not affect the other frames, and our listener is still using the previouslydefined frames. So, let's change the behavior of the listener.
Open the src/turtle_tf_listener.cpp file, and simple replace "/turtle1" with "/carrot1" in lines 26-27:
切换行号显示
1 listener.lookupTransform("/turtle2", "/carrot1", 2 ros::Time(0), transform);
And now the good part: just rebuild and restart the turtle demo, and you'll see the second turtle following the carrot instead of the first turtle! Remember that the carrot is 2 meters to the left of turtle1. There is no visual
representation for the carrot, but you should see the second turtle moving to that point.
$ catkin_make
$ roslaunch learning_tf start_demo.launch
Broadcasting a moving frame
The extra frame we published in this tutorial is a fixed frame that doesn't change over time in relation to the parent frame. However, if you want to publish a moving frame you can change the broadcaster to change over time. Let's modify the /carrot1 frameto change relative to /turtle1 over time.
切换行号显示
1 transform.setOrigin( tf::Vector3(2.0*sin(ros::Time::now().toSec()), 2.0*cos(ros::Time::now().toSec()), 0.0) ); 2 transform.setRotation( tf::Quaternion(0, 0, 0, 1) );
And now the good part: just rebuild and restart the turtle demo, and you'll see the second turtle following a moving carrot.
$ catkin_make
$ roslaunch learning_tf start_demo.launch
You're now ready to move to the next tutorial about tf and time (Python) (C++)
相关文章推荐
- ROS:关于tf的探索(3)Adding a frame(Python)
- ROS进二阶学习笔记(1) TF 学习笔记3 -- TF Listener 编写 (Python)and Adding frame(Python)
- Mac OS下安装配置Git-TF连接TFS2012教程
- 【tflearn系列教程】(二)如何安装tflearn
- Xcode GPU Frame Caputre 使用教程
- ROS学习(二十一)--tf/增加一个框架frame
- TreeSaver 使用教程整理——Step 2: Adding Basic UI
- Jeasyframe框架开发环境搭建详细教程
- iOSCoreAnimation动画系列教程(二):CAKeyFrameAnimation【包会】
- 阿里云机器学习平台PAI的视频介绍(其中tensorflow高级教程有tf的代码优化讲解)
- tf教程(一):Introduction to tf
- Spark DataFrame入门教程
- Flex官方快速入门教程翻译之19.Adding_Drag_and_Drop增加拖放功能
- 使用Xcode GPU Frame Caputre教程
- javascript教程 - 第三部分 window 和 frame 对象
- 使用Xcode GPU Frame Caputre教程
- tf教程(六):Time travel with tf
- OGRE基础教程四 Frame Listeners and Unbuffered Input