首页 > 代码库 > ROS TF——learning tf

ROS TF——learning tf

        在机器人的控制中,坐标系统是非常重要的,在ROS使用tf软件库进行坐标转换。

        相关链接:http://www.ros.org/wiki/tf/Tutorials#Learning_tf

一、tf简介

        我们通过一个小小的实例来介绍tf的作用。

1、安装turtle包

    $ rosdep install turtle_tf rviz      $ rosmake turtle_tf rviz  

 



2、运行demo

        运行简单的demo:
$ roslaunch turtle_tf turtle_tf_demo.launch  

 



        然后就会看到两只小乌龟了。
技术分享
        该例程中带有turtlesim仿真,可以在终端激活的情况下进行键盘控制。
技术分享

        可以发现,第二只乌龟会跟随你移动的乌龟进行移动。

3、demo分析

        接下来我们就来看一看到底ROS做了什么事情。
        这个例程使用tf建立了三个参考系:a world frame, a turtle1 frame, and a turtle2 frame。然后使用tf broadcaster发布乌龟的参考系,并且使用tf listener计算乌龟参考系之间的差异,使得第二只乌龟跟随第一只乌龟。
        我们可以使用tf工具来具体研究。
 
$ rosrun tf view_frames  

 



        然后会看到一些提示,并且生成了一个frames.pdf文件。
技术分享
        该文件描述了参考系之间的联系。三个节点分别是三个参考系,而/world是其他两个乌龟参考系的父参考系。还包含一些调试需要的发送频率、最近时间等信息。
        tf还提供了一个tf_echo工具来查看两个广播参考系之间的关系。我们可以看一下第二只得乌龟坐标是怎么根据第一只乌龟得出来的。
技术分享
[plain] view plain copy
  1. $ rosrun tf tf_echo turtle1 turtle2  


        控制一只乌龟,在终端中会看到第二只乌龟的坐标转换关系。
技术分享

        我们也可以通过rviz的图形界面更加形象的看到这三者之间的关系。

$ rosrun rviz rviz -d `rospack find turtle_tf`/rviz/tule_rviz.rviz

 


技术分享
        移动乌龟,可以看到在rviz中的坐标会跟随变化。其中左下角的是/world,其他两个是乌龟的参考系。
       下面我们就来详细分析这个实例。

二、Writing a tf broadcaster

1、创建包

 

cd catkin_ws/src/catkin_create_pkg learning_tf tf roscpp rospy turtlesim

 

 

 

建立你的新包roscd之前:

 $ cd ~/catkin_ws $ catkin_make $ source ./devel/setup.bash

 

我们首先创建源文件。 我们刚刚创建的包:

 $ roscd learning_tf

 

src / 文件夹并打开你最喜欢的编辑器,将下面的代码粘贴到一个新文件 src / turtle_tf_broadcaster.cpp

#include <ros/ros.h>#include <tf/transform_broadcaster.h>#include <turtlesim/Pose.h>std::string turtle_name;void poseCallback(const turtlesim::PoseConstPtr& msg){  static tf::TransformBroadcaster br;  tf::Transform transform;  transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );  tf::Quaternion q;  q.setRPY(0, 0, msg->theta);  transform.setRotation(q);  br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", turtle_name));}int main(int argc, char** argv){  ros::init(argc, argv, "my_tf_broadcaster");  if (argc != 2){ROS_ERROR("need turtle name as argument"); return -1;};  turtle_name = argv[1];  ros::NodeHandle node;  ros::Subscriber sub = node.subscribe(turtle_name+"/pose", 10, &poseCallback);  ros::spin();  return 0;};

现在我们创建代码,允许编译它。 打开 CMakeLists.txt 底部文件,并添加以下行:

add_executable(turtle_tf_broadcaster src/turtle_tf_broadcaster.cpp)target_link_libraries(turtle_tf_broadcaster ${catkin_LIBRARIES})

 

Build your package; at the top folder of your catkin workspace:

 $ catkin_make

 

    创建launch文件start_demo.launch:

  <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=http://www.mamicode.com/"2" type="double"/>    <param name="scale_angular" value=http://www.mamicode.com/"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" />  </launch>

 


        运行:

$ roslaunch learning_tf start_demo.launch  

 

 

      可以看到界面中只有移植乌龟了,打开tf_echo的信息窗口:

$ rosrun tf tf_echo /world /turtle1  

 

 

技术分享

    world参考系的原点在最下角,对于turtle1的转换关系,其实就是turtle1在world参考系中所在的坐标位置以及旋转角度。

 

三、Writing a tf listener

             这一步,我们将看到如何使用tf进行参考系转换。首先写一个tf listener(turtle_tf_listener.cpp)

. The turtlesim/Velocity.h header is not used anymore(再也不), it has been replaced by geometry_msgs/Twist.h. Furthermore(此外), the topic /turtle/command_velocity is now called /turtle/cmd_vel. In light of this, a few changes are necessary to make it work:

 

#include <ros/ros.h>#include <tf/transform_listener.h>#include <geometry_msgs/Twist.h>#include <turtlesim/Spawn.h>int main(int argc, char** argv){  ros::init(argc, argv, "my_tf_listener");  ros::NodeHandle node;  ros::service::waitForService("spawn");  ros::ServiceClient add_turtle =    node.serviceClient<turtlesim::Spawn>("spawn");  turtlesim::Spawn srv;  add_turtle.call(srv);  ros::Publisher turtle_vel =    node.advertise<geometry_msgs::Twist>("turtle2/cmd_vel", 10);  tf::TransformListener listener;  ros::Rate rate(10.0);  while (node.ok()){    tf::StampedTransform transform;    try{      listener.lookupTransform("/turtle2", "/turtle1",                               ros::Time(0), transform);    }    catch (tf::TransformException &ex) {      ROS_ERROR("%s",ex.what());      ros::Duration(1.0).sleep();      continue;    }    geometry_msgs::Twist vel_msg;    vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),                                    transform.getOrigin().x());    vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) +                                  pow(transform.getOrigin().y(), 2));    turtle_vel.publish(vel_msg);    rate.sleep();  }  return 0;};
 

 

 

 

修改 CMakeLists.txt

gedit CMakeLists.txt

 

 

 

add_executable(turtle_tf_listener src/turtle_tf_listener.cpp)target_link_libraries(turtle_tf_listener ${catkin_LIBRARIES})

 

 

Build your package; at the top folder of your catkin workspace:

 $ catkin_make

 

修改    launch文件start_demo.launch添加:

 

  <launch>    ...    <node pkg="learning_tf" type="turtle_tf_listener"          name="listener" />  </launch>

 

    然后在运行:

 

$ roslaunch learning_tf start_demo.launch  

 

 

就可以看到两只turtle了,也就是我们在最开始见到的那种跟随效果。

 

 

四、Adding a frame

 

        在很多应用中,添加一个参考系是很有必要的,比如在一个world参考系下,有很一个激光扫描节点,tf可以帮助我们将激光扫描的信息坐标装换成全局坐标。

 

1、tf消息结构

 

        tf中的信息是一个树状的结构,world参考系是最顶端的父参考系,其他的参考系都需要向下延伸。如果我们在上文的基础上添加一个参考系,就需要让这个新的参考系成为已有三个参考系中的一个的子参考系。

 

技术分享

 

 

2、建立固定参考系(fixed frame)

 

        我们以turtle1作为父参考系,建立一个新的参考系“carrot1”。
 
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;};

 

修改 CMakeLists.txt

gedit CMakeLists.txt
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
 
 

 

修改    launch文件start_demo.launch添加:

 
  <launch>    ...    <node pkg="learning_tf" type="frame_tf_broadcaster"          name="broadcaster_frame" />  </launch>

    然后在运行:

 

$ roslaunch learning_tf start_demo.launch  

 发现效果跟以前一样

     Open the src/turtle_tf_listener.cpp file, and simple replace "/turtle1" with "/carrot1" in lines 26-27:

 

  listener.lookupTransform("/turtle2", "/carrot1",                           ros::Time(0), transform);

 

 重新编译

 

 

Build your package; at the top folder of your catkin workspace:

 $ catkin_make
 
 技术分享
技术分享
 
 
ERROR: cannot launch node of type [learning_tf/turtle_tf_broadcaster]: can‘t locate node [turtle_tf_broadcaster] in package [learning_tf]
ERROR: cannot launch node of type [learning_tf/turtle_tf_broadcaster]: can‘t locate node [turtle_tf_broadcaster] in package [learning_tf]
ERROR: cannot launch node of type [learning_tf/turtle_tf_listener]: can‘t locate node [turtle_tf_listener] in package [learning_tf]
ERROR: cannot launch node of type [learning_tf/frame_tf_broadcaster]: can‘t locate node [frame_tf_broadcaster] in package [learning_tf]

解决办法:


executing the command "source devel/setup.sh" in the catkin workplace before running the launch file. (I always forget to run it....)
http://answers.ros.org/question/74608/tf-tutorialcannot-launch-node-of-type/

 
 

 

ROS TF——learning tf