您的位置:首页 > 其它

tf教程(四):Adding a frame

2017-07-01 15:19 381 查看


Adding a frame (C++)

Description: This
tutorial 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 care
of 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 previously
defined 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 frame
to 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++)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: