机器人操作系统ROS Indigo 入门学习(12) - 用C++语言写一个简单的发布者和订阅者
更新时间:2023-11-27 10:56:01 阅读量: 教育文库 文档下载
- 机器人控制系统有哪些推荐度:
- 相关推荐
这个教程将会包含怎样用C++去写一个发布者和订阅者.
1.1写一个发布者Node
“Node”是连接在ROS网络中一个可执行单元的术语.这里我们创建一个会不断广播messages的发布者(“talker”)node.
改变目录到你之前创建的工作空间的beginner_tutorials package中: cd ~/catkin_ws/src/beginner_tutorials
1.1.1代码
在beginner_tutorials packag目录中创建一个srv目录: mkdir -p ~/catkin_ws/src/beginner_tutorials/src
这个目录会包含所有beginner_tutorials package中的源文件.
在beginner_tutorials package中创建一个src/talker.cpp文件.并且把下面的代码粘贴上去":
https://raw.github.com/ros/ros_tutorials/groovy-devel/roscpp_tutorials/talker/talker.cpp
27 #include \
28 #include \ 29
30 #include
35 int main(int argc, char **argv) 36 {
37 /**
38 * The ros::init() function needs to see argc and argv so that it can perform
39 * any ROS arguments and name remapping that were provided at the command line. For programmatic 40 * remappings you can use a different version of init() which takes remappings
41 * directly, but for most command-line programs, passing argc and argv is the easiest
42 * way to do it. The third argument to init() is the name of the node. 43 *
44 * You must call one of the versions of ros::init() before using any other
45 * part of the ROS system. 46 */
47 ros::init(argc, argv, \ 48
49 /** 50 * NodeHandle is the main access point to communications with the ROS system.
51 * The first NodeHandle constructed will fully initialize this node, and the last
52 * NodeHandle destructed will close down the node. 53 */
54 ros::NodeHandle n; 55
56 /**
57 * The advertise() function is how you tell ROS that you want to
58 * publish on a given topic name. This invokes a call to the ROS
59 * master node, which keeps a registry of who is publishing and who 60 * is subscribing. After this advertise() call is made, the master
61 * node will notify anyone who is trying to subscribe to this topic name,
62 * and they will in turn negotiate a peer-to-peer connection with this 63 * node. advertise() returns a Publisher object which allows you to
64 * publish messages on that topic through a call to publish(). Once
65 * all copies of the returned Publisher object are destroyed, the topic
66 * will be automatically unadvertised. 67 *
68 * The second parameter to advertise() is the size of the message queue
69 * used for publishing messages. If messages are published more quickly 70 * than we can send them, the number here specifies how many messages to
71 * buffer up before throwing some away. 72 */
73 ros::Publisher chatter_pub =
n.advertise
75 ros::Rate loop_rate(10); 76
77 /**
78 * A count of how many messages we have sent. This is used to create
79 * a unique string for each message. 80 */
81 int count = 0; 82 while (ros::ok()) 83 {
84 /**
85 * This is a message object. You stuff it with data, and then publish it. 86 */
87 std_msgs::String msg; 88
89 std::stringstream ss;
90 ss << \ 91 msg.data = ss.str(); 92
93 ROS_INFO(\ 94
95 /**
96 * The publish() function is how you send messages. The parameter 97 * is the message object. The type of this object must agree with the type 98 * given as a template parameter to the advertise<>() call, as was done
99 * in the constructor above. 100 */
101 chatter_pub.publish(msg); 102
103 ros::spinOnce(); 104
105 loop_rate.sleep(); 106 ++count; 107 } 108 109
110 return 0; 111 }
1.1.2代码解释
现在我们分解代码. #include “ros/ros.h”
ros/ros.h是一个非常方便的头文件它包含了最常用的ROS系统部分所必须的一些头文件.
#include “std_msgs/String.h”
这里包含了std_msgs package中的std_msgs/String message .这个头文件自动的从String.msg.file中产生.更多关于message的信息,请查看msg page.
Ros::init(argc,argv,”talker”);
初始化ROS.这个允许ROS通过命令行--(现在不重要)重新映射名字.同样可以用来制定node的名字.在系统中Nodes的名字必须是唯一的. 名字必须是一个基础的名字,比如,不能有/在里面.
Ros::NodesHandle n;
为这个node创建一个handle.创建的第一个NodeHandle用来初始化node,最后一个销毁的会清除所有node占有的资源.
ros::Publisher chatter_pub = n.advertise
告诉master我们将要在chatter topic中要发布一个std_ksgs/String类型的message,这就会让master告诉nodes听取chatter,在这个tpoic上在我们将要发布数据.第二个参数是发布队列的大小.这样的话如果我们发布的太快,在开始丢弃之前的message前,它会最大缓冲是1000个messages.
NodeHandle::advertise()返回一个ros::Publisher的对象,它有两个作用:(1)它包含一个允许你发布message到它创建的topic上的publish()方法(2)当它程序跑飞时,会自动避开危险.
ros::Rate loop_rate(10);
ros::Rate对象会指定一个你想循环的频率.它会跟踪距离上次调用Rate::sleep(),有多长时间了,并且休眠正确长度的时间. 这里我们设置为10hz:
int count =0; while(ros::ok()) {}
默认roscpp会安装一个SIGINT信号处理函数提供对ctrl+c的处理,ctrl+c会导致ros::ok()返回错误.
Ros::ok()会返回错误如果: 接受到SIGINT(ctrl+c)
我们通过用另一个有同样名字的node网络。 ros::shutdown()被应用的另一部分调用。 所有的 ros::NodeHandles都被摧毁了。
一旦ros::ok()返回错误,所有的ROS调用都会失败. 87 std_msgs::String msg; 88
89 std::stringstream ss;
90 ss << \ 91 msg.data = ss.str();
我们使用适应message的类在ROS上广播了一个message,通常从一个msg文件产生出来。其他复杂的数据类型也是是可以的,但是现在我们准备用标准的String message,它有一个成员:”data”。 chatter_pub.publish(msg);
现在实际上我们在向任何一个连接上的人广播这个message.
ROS_INFO(\
ROS_INFO和它的友元类都是用来替代printf/cout的。更多信息请看rosconsole documentation
ros::spinOnce();
对于这个程序调用ros::splinOnce()不是必要的,因为我们不会接受到任何回叫信号。然而,如果你打算为这个应用添加一个订阅,并且没有调用ros::splinOnce(),你绝不会得到回叫信号,所以还是添加的好。 loop_rate.sleep();
现在使用ros::Rate对象去空耗掉剩下的时间以满足10hz的发布速度。
这里是步骤的简要描叙: 初始化ROS系统
广告给master我们将要发布std_msgs/Stringmessage到chatter topic上 在发布message的时候循环以满足10次每秒钟
正在阅读:
机器人操作系统ROS Indigo 入门学习(12) - 用C++语言写一个简单的发布者和订阅者11-27
广东省惠州市惠东中学2018-2019学年高一年级化学期末模拟复习训03-24
2011年中考数学复习高分冲刺经典习题 - (14)10-06
螺纹的画法及标注08-30
自控原理2实验考试安排 (1)05-30
CUBLAS04-20
培训心得体会,2022年培训心得体会范文_培训心得体会怎么写08-01
瑞士洛桑酒店管理学院硕士04-23
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 订阅者
- C++
- 机器人
- 发布者
- 入门
- 操作系统
- 语言
- 简单
- Indigo
- 学习
- 一个
- ROS
- 12
- 毛概 6
- 计量经济学03 多元线性回归模型
- 2011届高考英语一轮复习讲解:主谓一致
- 2015年注册测绘师考试培训习题(单选题和多选题)
- 维生素,矿物质
- CSharp调用COM组件
- 育婴师中级理论知识试卷及答案(2)
- 成语大全
- grbl controller和artcam 使用简介 - 图文
- crts-ii型轨道板预制工艺难点解析
- 惯标版003费县调试措施(冷态风量标定)
- 5小刺猬运果子案例分析
- 药品零售连锁企业筹建申请表
- 苏教版2017版六年级下册劳动与技术教学计划
- 感应加热设备 - 图文
- 第九章热电偶传感器习题及答案
- 高级财务管理学课程设计案例 答案
- 2010VB本科期末上机考试练习题答案
- 媒体介入中国司法公正利大于弊 一辩稿
- 开闭所规范要求