打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ROS/Tutorials/WritingPublisherSubscriber(c++)

Writing the Publisher Node

  27 #include "ros/ros.h"  28 #include "std_msgs/String.h"  29   30 #include <sstream>  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.  40    * For programmatic remappings you can use a different version of init() which takes  41    * remappings directly, but for most command-line programs, passing argc and argv is  42    * the easiest 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, "talker");  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<std_msgs::String>("chatter", 1000);  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 << "hello world " << count;  91     msg.data = ss.str();  92   93     ROS_INFO("%s", msg.data.c_str());  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 }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ROS:消息发布器和订阅器(c++)
NodeHandle节点
tf代码实例
ROS学习(四)发布者与订阅者
main函数执行前后 《程序员的自我修养》·笔记
ros 消息发布和订阅
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服