ROS学习笔记3-基础概念

2017-11-08  本文已影响0人  scott_yu779

命名空间

引用
http://blog.csdn.net/u014587147/article/details/75647002

#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
 
int main(int argc, char** argv){
  ros::init(argc, argv, "laser_scan_publisher");
 
  ros::NodeHandle n;
  ros::Publisher scan_pub = n.advertise<sensor_msgs::LaserScan>("scan", 50);
 
  unsigned int num_readings = 100;
  double laser_frequency = 40;
  double ranges[num_readings];
  double intensities[num_readings];
 
  int count = 0;
  ros::Rate r(1.0);
  while(n.ok()){
    //generate some fake data for our laser scan
    for(unsigned int i = 0; i < num_readings; ++i){
      ranges[i] = count;
      intensities[i] = 100 + count;
    }
    ros::Time scan_time = ros::Time::now();
 
    //populate the LaserScan message
    sensor_msgs::LaserScan scan;
    scan.header.stamp = scan_time;
    scan.header.frame_id = "laser_frame";
    scan.angle_min = -1.57;
    scan.angle_max = 1.57;
    scan.angle_increment = 3.14 / num_readings;
    scan.time_increment = (1 / laser_frequency) / (num_readings);
    scan.range_min = 0.0;
    scan.range_max = 100.0;
 
    scan.ranges.resize(num_readings);
    scan.intensities.resize(num_readings);
    for(unsigned int i = 0; i < num_readings; ++i){
      scan.ranges[i] = ranges[i];
      scan.intensities[i] = intensities[i];
    }
 
    scan_pub.publish(scan);
    ++count;
    r.sleep();
  }
}

引用博客
NodeHandle问题

问题如下:

ros::init(argc, argv, "my_node_name");  
ros::NodeHandle nh1("~");  //"~"代表 node_name
ros::NodeHandle nh2("~foo");  

nh1的命名空间(namespace)是 /my_node_name,而nh2的namespace是 /my_node_name/foo
这里只是一个例子,一个ros的node只有一个NodeHandle,它提供这个node的对于topic的收发功能

程序包的概念剖析

最简单的程序包也许看起来就像这样:

my_package/
CMakeLists.txt
package.xml
<export>
   <metapackage />
 </export>

2、metapackage的cmakelist样式
Additionally a metapackage has a required, boilerplate CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.3)
project(<PACKAGE_NAME>)
find_package(catkin REQUIRED)
catkin_metapackage()

NodeHandle句柄

引用
ROS Nodehandle句柄

私有命名空间~
~是nodename
nodename/name1/name2/...

默认是nodenamespace
node_namespace和node_name是两回事!
ros::NodeHandle nh("~my_private_namespace");
ros::Subscriber sub = nh.subscribe("my_private_topic",....);
以上例子会订阅<node_name>/my_private_namespace/my_private_topic

ros::NodeHandle nh("/my_global_namespace");
这种做法并不推荐,因为这样会使得节点无法被放入别的命名空间。只是有时在代码中使用全局名字有用。

上一篇下一篇

猜你喜欢

热点阅读