Issue with subscriber callback as publihser
Using the code prototype shown here
I wrote the following code:
#include <ros/ros.h>
#include <std_msgs/Int16.h>
class SubscribeAndPublish
{
public:
SubscribeAndPublish()
{
pub = nh.advertise<std_msgs::Int16>("same_topic",1);
sub = nh.subscribe<std_msgs::Int16>("same_topic",1,&SubscribeAndPublish::callback,this);
}
void callback(const std_msgs::Int16::ConstPtr& msg)
{
ROS_INFO("I heard this data: [%d]", msg->data);
std_msgs::Int16 msg_p;
msg_p.data = msg->data;
ROS_INFO_STREAM(" I got some data");
pub.publish(msg_p);
}
private:
ros::NodeHandle nh;
ros::Publisher pub;
ros::Subscriber sub;
};
int main(int argc, char **argv){
ros::init(argc,argv,"node");
SubscribeAndPublish ps;
ros::spin();
return 0;
}
When I executes the code, it is waiting (for input) and I gave some integers as follows
12
123324
43534
23452345
342
153
5234
The program is not printing anything and still waits for input, but when I kill the node, (only) numbers are printing on command lines.
Why the program is not working as expected? where I went wrong?