subscribe.cpp
#include "ros/ros.h"
#include "std_msgs/Int32.h"#include <sstream>
void chatterCallback(const std_msgs::Int32::ConstPtr& msg)
{
ROS_INFO("I heard:[%d]",msg->data);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "example_talker_msg");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("third_pkg_topic", 1000,chatterCallback);
ros::Rate loop_rate(10);
ros::spin();
return 0;
}
third_pkg.cpp:
#include "ros/ros.h"
#include "std_msgs/Int32.h"
#include <sstream>
int main(int argc, char **argv)
{
ros::init(argc, argv, "example_talker_msg");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::Int32>("third_pkg_topic", 1000);
ros::Rate loop_rate(2);
int count=0;
while (ros::ok())
{
std_msgs::Int32 msg;
//std::stringstream ss;
//ss << "third_pkg" << count;
msg.data=count;
ROS_INFO("%d",msg.data);
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}