ros_tutorials
  • 简介
  • 1 安装并配置ROS环境
    • 1.1 安装ROS
    • 1.2 管理环境变量
    • 1.3 创建ROS工作空间
  • 2 ROS文件系统介绍
    • 2.1 快速了解文件系统概念
    • 2.2 文件系统工具
    • 2.3 回顾
  • 3 创建ROS程序包(Packages)
    • 3.1 catkin程序包由什么组成?
    • 3.2 在catkin工作空间中的程序包
    • 3.3 创建一个catkin程序包
    • 3.4 编译程序包
    • 3.5 程序包依赖关系
    • 3.6 自定义你的程序包
  • 4 编译ROS程序包(packages)
    • 4.1 使用 catkin_make
    • 4.2 编译你的程序包Package
  • 5 理解ROS节点(nodes)
    • 5.1 图(Graph)概念
    • 5.2 节点(Nodes)
    • 5.3 客户端库(Client Libraries)
    • 5.4 roscore
    • 5.5 使用rosnode
    • 5.6 使用rosrun
    • 5.7 回顾
  • 6 理解ROS话题(topics)
    • 6.1 开始
    • 6.2 ros topics
    • 6.3 ROS Messages
    • 6.4 继续学习 rostopic
    • 6.5 使用 rqt_plot
  • 7 理解ROS服务和参数
    • 7.1 ROS Services
    • 7.2 使用rosservice
    • 7.3 使用 rosparam
  • 8 使用 rqt_console 和 roslaunch
    • 8.1 预先安装rqt和turtlesim程序包
    • 8.2 使用rqt_console和rqt_logger_level
  • 9 使用rosed编辑ROS中的文件
    • 9.1 使用 rosed
    • 9.2 使用Tab键补全文件名
    • 9.3编辑器
  • 10 创建ROS消息和ROS服务
    • 10.1 消息(msg)和服务(srv)介绍
    • 10.2 使用 msg
    • 10.3 使用 srv
    • 10.4 msg和srv都需要的步骤
    • 10.5 获得帮助
    • 10.6 回顾
  • 11 编写简单的Publisher和Subscriber(C++)
    • 11.1 编写Publisher Node
    • 11.2 编写Subscriber Node
    • 11.3 编译nodes
  • 12 编写简单的Publisher和Subscriber(Python)
    • 12.1 编写Publisher Node
    • 12.2 编写Subscriber Node
    • 12.3 编译nodes
  • 13 测试消息Publisher和Subscriber
    • 13.1 启动发布器(Publisher)
    • 13.2 启动订阅器(Subscriber)
  • 14 编写简单的Service和Client (C++)
    • 14.1 编写Service节点
    • 14.2 编写Client节点
    • 14.3 编译nodes
  • 15 编写简单的Service和Client (Python)
    • 15.1 编写Service Node
    • 15.2 编写Client节点
    • 15.3 编译nodes
    • 15.4 试试看
  • 16 测试简单的Service和Client
    • 16.1 运行Service
    • 16.2 运行Client
    • 16.3 关于Service和Client节点的更多例子
  • 17 录制与回放数据
    • 17.1 录制数据(通过创建一个bag文件)
    • 17.2 检查并回放bag文件
    • 17.3 录制数据子集
    • 17.4 rosbag record/play 命令的局限性
  • 18 roswtf入门
    • 18.1 安装检查
    • 18.2 运行时检查(在有ROS节点运行时)
    • 18.3 错误报告
  • 19 探索ROS维基
    • 19.1 基础
    • 19.2 高级
  • 20 接下来做什么
    • 20.1 使用模拟器
    • 20.2 使用 RViz
    • 20.3 理解 TF
    • 20.4 更进一步
Powered by GitBook
On this page
  • 11.2.1 源代码
  • 11.2.2 代码说明
  1. 11 编写简单的Publisher和Subscriber(C++)

11.2 编写Subscriber Node

11.2.1 源代码

在 beginner_tutorials package 目录下创建 src/listener.cpp 文件,并粘贴如下代码:

#include "ros/ros.h"
#include "std_msgs/String.h"

/**
 * This tutorial demonstrates simple receipt of messages over the ROS system.
 */
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
  /**
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line. For programmatic
   * remappings you can use a different version of init() which takes remappings
   * directly, but for most command-line programs, passing argc and argv is the easiest
   * way to do it.  The third argument to init() is the name of the node.
   *
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "listener");

  /**
   * 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;

  /**
   * The subscribe() call is how you tell ROS that you want to receive messages
   * on a given topic.  This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing.  Messages are passed to a callback function, here
   * called chatterCallback.  subscribe() returns a Subscriber object that you
   * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
   * object go out of scope, this callback will automatically be unsubscribed from
   * this topic.
   *
   * The second parameter to the subscribe() function is the size of the message
   * queue.  If messages are arriving faster than they are being processed, this
   * is the number of messages that will be buffered up before beginning to throw
   * away the oldest ones.
   */
  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  /**
   * ros::spin() will enter a loop, pumping callbacks.  With this version, all
   * callbacks will be called from within this thread (the main one).  ros::spin()
   * will exit when Ctrl-C is pressed, or the node is shutdown by the master.
   */
  ros::spin();

  return 0;
}

11.2.2 代码说明

下面我们将逐条解释代码,当然,之前解释过的代码就不再赘述了。

void chatterCallback(const std_msgs::String::ConstPtr& msg){  ROS_INFO("I heard: [%s]", msg->data.c_str());}
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

告诉 master 我们要订阅 chatter 话题上的消息。当有消息发布到这个话题时,ROS 就会调用 chatterCallback() 函数。第二个参数是队列大小,以防我们处理消息的速度不够快,当缓存达到 1000 条消息后,再有新的消息到来就将开始丢弃先前接收的消息。

NodeHandle::subscribe() 返回 ros::Subscriber对象,你必须让它处于活动状态直到你不再想订阅该消息。当这个对象销毁时,它将自动退订 chatter 话题的消息。

ros::spin();

ros::spin() 进入自循环,可以尽可能快的调用消息回调函数。如果没有消息到达,它不会占用很多 CPU,所以不用担心。一旦 ros::ok() 返回 false,ros::spin() 就会立刻跳出自循环。这有可能是 ros::shutdown() 被调用,或者是用户按下了 Ctrl-C,使得 master 告诉节点要终止运行。也有可能是节点被人为关闭的。

下边,我们来总结一下:

  • 初始化ROS系统

  • 订阅 chatter 话题

  • 进入自循环,等待消息的到达

  • 当消息到达,调用 chatterCallback() 函数

Previous11.1 编写Publisher NodeNext11.3 编译nodes

Last updated 7 years ago

这是一个回调函数,当接收到 chatter话题的时候就会被调用。消息是以 指针的形式传输,这就意味着你可以存储它而又不需要复制数据。

有各种不同的 NodeHandle::subscribe() 函数,允许你指定类的成员函数,甚至是 Boost.Function 对象可以调用的任何数据类型。 提供了更为详尽的信息。

还有其他的方法进行回调,但在这里我们不涉及。想要了解,可以参考 package 里的一些 demo 应用。需要更为详尽的信息,可以参考 。

boost shared_ptr
roscpp overview
roscpp_tutorials
roscpp overview