1.发布消息
1.发布消息
ros::init(argc, argv, "talker"); //初始化节点
ros::NodeHandle n; //定义句柄
//定义publisher pulisher = handle.advertise<msgType>("topic",queue_size)
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
//为msg赋值(填入内容)
msg.data = 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