用C++写一个简单的订阅者
打开一个终端,进入到beginner_tutorials包下面:
cd ~/catkin_ws/src/beginner_tutorials
建立文件src/listener.cpp:
vim 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", , 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 ;
}
保存退出。
下面看一下代码的解释:
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
当一个消息到达chatter话题时,这个回调函数将会被调用。
ros::Subscriber sub = n.subscribe("chatter", , chatterCallback);
订阅chatter话题,当一个新的消息到达时,ROS将会调用chatterCallback()函数。第二个参数是对列的长度,会将收到的消息缓冲下来,一共可以缓冲1000条消息,满1000之后,后面的到达的消息将会覆盖前面的消息。
NodeHandle::subscribe()将会返回一个ros::Subscriber类型的对象,当订阅对象被销毁以后,它将会自动从chatter话题上撤销。
ros::spin();
ros::spin()进入了一个循环,非常快的调用消息的回调函数。不要担心,如果它没有什么事情可做时,它也不会浪费太多的CPU。
当ros::ok()返回false时,ros::spin()将会退出。
这就意味着,当ros::shutdown()被调用,或按下CTRL+C等情况,都可以退出。
下面总结一下写一个订阅者的步骤:(1)初始化ROS系统(2)订阅chatter话题(3)Spin,等待消息的到来(4)当一个消息到达时,chatterCallback()函数被调用。
下面看一下如何构建节点。我们之前用过catkin_create_pkg创建过package.xml和CMakeLists.txt(目录:catkin_ws/src/beginner_tutorials/CMakeLists.txt),这时候你的CMakeLists.txt看起来应该是下面这个样子,包括前面所做的修改,注释部分可以除去:
cmake_minimum_required(VERSION 2.8.)
project(beginner_tutorials) ## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg) ## Declare ROS messages and services
add_message_files(DIRECTORY msg FILES Num.msg)
add_service_files(DIRECTORY srv FILES AddTwoInts.srv) ## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs) ## Declare a catkin package
catkin_package()
将下面几行代码添加到CMakeLists.txt的最后。最终你的CMakeLists.txt文件看起来样该是下面这个样子:
cmake_minimum_required(VERSION 2.8.)
project(beginner_tutorials) ## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg) ## Declare ROS messages and services
add_message_files(FILES Num.msg)
add_service_files(FILES AddTwoInts.srv) ## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs) ## Declare a catkin package
catkin_package() ## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS}) add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp) add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)
构建完之后,这将会创建两个可执行文件,talker和listener。它们将会产生在~/catkin_ws/devel/lib/share/<package name>目录下,这也就是说构建好的东西,会放在devel的目录中,原本的东西会留在src文件中。
下面开始构建,在你的工作空间根目录下输入:
catkin_make
这样我们就完成了,在某个话题上的消息的发布者和订阅者的可执行模块的创建。
用C++写一个简单的订阅者的更多相关文章
- [Vue]写一个简单的文件上传控件
这篇将介绍如何写一个简单的基于Vue+Element的文件上传控件. 控件将具有 1. 上传队列的列表,显示文件名称,大小等信息,可以显示上传进度实时刷新 2. 取消上传 使用Element的u ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 如何写一个简单的http服务器
最近几天用C++写了一个简单的HTTP服务器,作为学习网络编程和Linux环境编程的练手项目,这篇文章记录我在写一个HTTP服务器过程中遇到的问题和学习到的知识. 服务器的源代码放在Github. H ...
- 如何写一个简单的shell
如何写一个简单的shell 看完<UNIX环境高级编程>后我就一直想写一个简单的shell来作为练习,因为有事断断续续的写了好几个月,如今写了差不多来总结一下. 源代码放在了Github: ...
- 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”
这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...
- 一步一步写一个简单通用的makefile(三)
上一篇一步一步写一个简单通用的makefile(二) 里面的makefile 实现对通用的代码进行编译,这一章我将会对上一次的makefile 进行进一步的优化. 优化后的makefile: #Hel ...
- Java写一个简单学生管理系统
其实作为一名Java的程序猿,无论你是初学也好,大神也罢,学生管理系统一直都是一个非常好的例子,初学者主要是用数组.List等等来写出一个简易的学生管理系统,二.牛逼一点的大神则用数据库+swing来 ...
- (2)自己写一个简单的servle容器
自己写一个简单的servlet,能够跑一个简单的servlet,说明一下逻辑. 首先是写一个简单的servlet,这就关联到javax.servlet和javax.servlet.http这两个包的类 ...
- express 写一个简单的web app
之前写过一个简单的web app, 能够完成注册登录,展示列表,CURD 但是版本好像旧了,今天想写一个简单的API 供移动端调用 1.下载最新的node https://nodejs.org/zh- ...
随机推荐
- android 混淆配置
proguard 原理Java代码编译成二进制class 文件,这个class 文件也可以反编译成源代码 ,除了注释外,原来的code 基本都可以看到.为了防止重要code 被泄露,我们往往需要混淆( ...
- 简单的Ajax
ajax获取时间的html代码 <head> <meta http-equiv="Content-Type" content="text/html; c ...
- SQL Server 2008如何导出带数据的脚本文件
第一步,选中需要导出脚本的数据库,右键选中 第二步,选取弹出菜单中的任务-生成脚本选项(会弹出一SQL生成脚本的向导) 第三步,在向导中点击下一步,弹出选择数据库界面(默认是自己之前选中的数据库),把 ...
- iOS8的新特性
iOS8的几个重要变化: 家庭分享.用户可以创建家庭分享,除创建者之外最多可以加入6个家庭成员.通过该功能,用户可以和家人分享位置.照片.日历.应用程序.音乐和视频等. 键盘.苹果在iOS8之后开放了 ...
- 我也能上google
之所以起个这么隐晦的标题就是因为怕被黑.但是不能上谷歌,对于我们搞技术的人来说真的很残忍. 目前只找到几个镜像网址: https://xie.lu/ https://www.gotosearch.in ...
- Linux学习之sed命令详解
概述 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区 ...
- Linux学习awk命令
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...
- struts2笔记03-ActionContext
1.概念 ActionContext是action的上下文,它包括action执行所需要的对象.struts2对每一个action都会创建一个新的ActionContext实例,同Action一样,是 ...
- spring常量值注入
<context:property-placeholder location="classpath:resources/*.properties" /> @Value( ...
- SQL Server FileStream优点与不足
LOB优点: 1.保证大对象的事务一致性. 2.备份与还原包括大数据对象,可以对它进行时点恢复. 3.所有数据都可以使用一种存储与查询环境. LOB不足: 1.大型对象在缓存中占非常大的缓存区. 2. ...