机器人操作系统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 31 32 /** 33 * This tutorial demonstrates simple sending of messages over the ROS system. 34 */

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(\ 74

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次每秒钟

本文来源:https://www.bwwdw.com/article/2yet.html

Top