使用tf配置(slam)

1软件:ros_qtc_plugin

          ubuntu14.04

2运行原理及代码来自http://wiki.ros.org/navigation/Tutorials/RobotSetup/TF

3编写代码:

在利用ros_qtc_plugin建立新的工作空间时,首先要注意运行 gedit ~/.bashrc 进行编辑将启动文件地址加在末尾我的是source /home/adminer/w_test/devel/setup.bash.

进入工作空间的src文件下,否则使用下面代码时失败。

运行  roscreate-pkg robot_setup_tf roscpp tf geometry_msgs

意思是创建robot_setup_tf文件,并添加tf roscpp geometry_msgs 依赖。roscreate-pkg使用方法地址http://wiki.ros.org/roscreate

(1)利用ros_qtc_plugin创建文件tf_broadcaster.cpp

编辑代码:

#include 
//tf功能包提供tf:TransformBroadcaster,使得发布变换变得容易
#include 
 
  
int main(int argc, char** argv){
  ros::init(argc, argv, "robot_tf_publisher");
  /*
    * NodeHandle is the main access point to communications with the ROS system.
    * The first NodeHandle constructed will fully initialize this node, and the last
    * NodeHandle destructed will close down the node.
    */
  ros::NodeHandle n;
 
  
  ros::Rate r(100);
//创建对象,用于发布从哦呢base_link到base_laser的变换
  tf::TransformBroadcaster broadcaster;
//发送带有TransformBroadcaster变换所需要的四个变量
  while(n.ok()){
    broadcaster.sendTransform(
       //发送旋转矩阵,通过btQuaternion指定了两个坐标系的旋转关系,没有旋转,前三变量为零
       //随后是一个平移变量btVector3
      tf::StampedTransform(
       tf::Transform(tf::Quaternion(0, 0, 0, 1), tf::Vector3(0.1, 0.0, 0.2)),
       //给将要发布的变换指定时间戳ros::Time::now(),传送父节点base_link.子节点base_laser
       ros::Time::now(),"base_link", "base_laser"));
    r.sleep();
 }
}
(2)利用 ros_qtc_plugin创建文件tf_listener.cpp

添加代码

 #include 
 #include 
 #include 
 
  
 void transformPoint(const tf::TransformListener& listener){
   /*we'll create a point in the base_laser frame that we'd like to transform
    *to the base_link frame*/
   geometry_msgs::PointStamped laser_point;
   laser_point.header.frame_id = "base_laser";
 
  
   //we'll just use the most recent transform available for our simple example
   laser_point.header.stamp = ros::Time();
 
  
   //just an arbitrary point in space
   laser_point.point.x = 1.0;
   laser_point.point.y = 0.2;
   laser_point.point.z = 0.0;
 
  
   try{
     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());
   }
 }
 
  
 int main(int argc, char** argv){
   ros::init(argc, argv, "robot_tf_listener");
   ros::NodeHandle n;
 
  
   tf::TransformListener listener(ros::Duration(10));
 
  
   //we'll transform a point once every second
   ros::Timer timer = n.createTimer(ros::Duration(1.0), boost::bind(&transformPoint, boost::ref(listener)));
 
  
   ros::spin();
 
  
 }
 4编译代码 
  

打开CMakeList.txt文件,末尾添加

rosbuild_add_executable(tf_broadcaster src/tf_broadcaster.cpp)
rosbuild_add_executable(tf_listener src/tf_listener.cpp)
 保存文件 在终端下的 
  robot_setup_tf功能包下运行rosmake, 
  

5运行代码

(1)终端运行:roscore

(2)第二个终端运行rosrun robot_setup_tf tf_broadcaster

(2)第三个终端运行(2)第二个终端运行rosrun robot_setup_tf tf_listener

成功后显示:

使用tf配置(slam)_第1张图片

注意:qt下运行也可以

使用tf配置(slam)_第2张图片


你可能感兴趣的:(ros.qt)