Subscriber node not working for topic at 50Hz
Hello all,
So I have a topic published by a publisher node, at 50 Hz. The topic publishes a message 'loadMsg' which consists of the following data types
1) string unit
2) float64 load
Now I have my subscriber node, which is the following piece of code
#include <ros/ros.h>
#include <robot_comm/robot_comm.h>
#include "std_msgs/String.h"
#include "cell_node/loadMsg.h"
void loadSensor(const cell_node::loadMsg &d)
{
ROS_INFO("Load Unit = [%s] , Value = [%lf] ", d.unit.c_str(),d.load);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "demo");
//FILE * pFile;
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("loadSensor", 1000, loadSensor);
//ros::Rate loop_rate(50);
int i = 0;
while(i<10)
{
usleep(20000);
ros::spinOnce();
i++;
}
//pFile = fopen ( "myfile.txt" , "w+" );
//fwrite (buffer , 1 , sizeof(buffer) , pFile );
//fclose (pFile);
return 0;
}
So what this should do is basically sample from the stream of 'loadMsg' data being published at 50Hz. And the sampling should happen again at 50Hz (1/20000usec).
However, I am not getting any output! rosmake/make work perfectly, without any errors. When I run the node however, I get nothing.
What may be going wrong? In an earlier post, I asked a similar question. But it worked then, it refuses to work now. Here is the link to that:
http://answers.ros.org/question/38131/rosspinonce-not-functioning-properly/
Also, if I replace ros::spinOnce() by ros::spin() and remove the while loop, then it does sample from the topic, but seemingly at a lesser frequency.
All help is greatly appreciated!
Hey ipso, thanks for the hint to topic_tools. However, I don't get one thing. When I did sleep(N) in my while loop here
http://answers.ros.org/question/38131/rosspinonce-not-functioning-properly/
why did polling work here?
Also, what do you think about the ros::Timer library? Can I use that instead of this method where I poll every time I want data from the topic stream?
@Nishant: read my answer: you're not polling from the topic 'stream' (actually: events). You either receive the publications or you don't. AFAIK there is currently no support for polled topics in ROS. You could use
ros::Timer
in your publisher yes, your subscriber does not need it.@Nishant: the only thing you change by running your own while loop (as in the answer you linked) is the rate at which ROS looks at the queue of incoming events. @PerkinsJames also describes this in the accepted answer. As you publisher is @10Hz, so is your subscriber's callback.