本教程将演示怎样对tf树添加额外的坐标系。这与以前讲的tf broadcaster十分相似,而且会显示tf的优点。
很多任务很容易想到内部坐标系,如在激光扫描的中心更容易推理坐标系中的激光。tf允许你定义每个激光坐标系的在你系统中的坐标。tf 将将采取所有额外的坐标系转换。
Where to add frames
修改 start_demo.launch 文件。tf建立了frames的树形结构,在这个结构中不允许闭环,意思是一个frame只能有一个父母,但是它可以有很多孩子,这个例子中包含了三个frame:world,turtle1和turtle2,其中turtle1和turtle2是world的孩子,如果我们想要建立一个新的frame,这个新的frame需要成为这三个frame的其中一个的孩子
在turtle教程中,我们将在第一个turtle中添加一个新的坐标系,这个坐标系将作为第二个turtle的“carrot”坐标系。roscd learning_tf
新建文件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) ); br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1")); rate.sleep(); } return 0; };
代码解释transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) ); transform.setRotation( tf::Quaternion(0, 0, 0) ); br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1"));
一个新的frame名称为carrot1,它的父母是turtle1,它距离父母的距离是在y侧即在左侧2米
编辑CMakeLists.txt
<pre name="code" class="html">add_executable(frame_tf_broadcaster src/frame_tf_broadcaster.cpp) target_link_libraries(frame_tf_broadcaster ${catkin_LIBRARIES})
<launch> <!-- Turtlesim Node--> <node pkg="turtlesim" type="turtlesim_node" name="sim"/> <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/> <!-- Axes --> <param name="scale_linear" value="2" type="double"/> <param name="scale_angular" value="2" type="double"/> <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle1" name="turtle1_tf_broadcaster" /> <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle2" name="turtle2_tf_broadcaster" /> <node pkg="learning_tf" type="turtle_tf_listener" name="listener" /> <node pkg="learning_tf" type="frame_tf_broadcaster" name="broadcaster_frame" /> </launch>
运行launch文件检查结果,将turtle_tf_listener.cpp进行修改roslaunch learning_tf start_demo.launch
listener.lookupTransform("/turtle2", "/carrot1", ros::Time(0), transform); roslaunch learning_tf start_demo.launch
将frame_tf_broadcaster.cpp文件进行修改,可以使turtle2在turlte1旁旋转。
transform.setOrigin( tf::Vector3(2.0*sin(ros::Time::now().toSec()), 2.0*cos(ros::Time::now().toSec()), 0.0) );
transform.setRotation( tf::Quaternion(0, 0, 0) );