发布和接收消息

1.发布消息

1.发布消息ros::init(argc, argv, "talker"); //初始化节点ros::NodeHandle n; //定义句柄 //定义publisher pulisher = handle.advertise("topic",queue_size)ros::Publisher chatter_pub = n.advertise("chatter", 1000); std_msgs::String msg; std::stringstream ss; ss << "hello world " << count; //为msg赋值(填入内容)msg.data = https://www.it610.com/article/ss.str(); //发布消息chatter_pub.publish(msg);

2.接收消息
//定义回调函数void chatterCallback(const std_msgs::String::ConstPtr& msg){ROS_INFO("I heard: [%s]", msg->data.c_str()); }ros::init(argc, argv, "listener"); ros::NodeHandle n; //定义subscriber, subscriber = handle.subscribe("topic",queue_size,callback_function)ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); //保持同步ros::spin();

————————————————
版权声明:本文为CSDN博主「Leo_starr」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Leo_starr/article/details/102136763

【发布和接收消息】

    推荐阅读