根据博文https://www.cnblogs.com/liu-fa/p/5925381.html,理解spin()和spinOnce():
先上根据博文修改为自己的测试代码,
消息发送端代码talker.cpp:
#include<ros/ros.h>
#include "std_msgs/String.h"
#include <sstream>
int main(int argc,char **argv)
{
ros::init(argc,argv,"hello_ros");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
//定义消息池最大容量为可以接收 1000个消息
ros::Rate loop_rate(10); //定义发送频率为10Hz(1秒发10次)
int count = 0;
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
/**
* 向 Topic: chatter 发送消息, 发送频率为10Hz(1秒发10次&#