How to subscribe to a topic at will/on demand?
Hello all.
As indicated by the quick response of catagay and Lorenz to my previous question here
http://answers.ros.org/question/38088/how-do-i-create-a-ros-topic-publishersubscriber/
I started reading up on how to create publishing and subscribing agents, on the ros tutorials page.
http://www.ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
Here is what I understood from the tutorial:
1) The publisher in the example keeps on publishing forever.
2) advertise<std_msgs::string>() is the harbinger of the topic to be published. So this step seems necessary.
3) chatter_pub.publish(msg) is the guy who does the talking. Since he is in a while(ros::ok()), this will publish forever.
4) The subscriber also keeps on subscribing forever.
5) ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); tells the master to subscribe to the topic and call chatterCallback whenever there is a new message. Because of ros::spin(), however, this subscriber will continue listening to the topic and printing to the screen forever (till node dies, of course).
Based on these points, here are my related questions:
1) Is my understanding enumerated in the points above right? Or am I missing something?
2) What do you mean by "..call chatterCallback whenever there is a new message."? What does new message mean here? Does it mean that the function is called whenever the message on the topic is updated?
3) Suppose the publisher wants to keep on publishing the topic forever, but the subscriber wants to get messages from the topic only at select locations in my code. Say I want the subscriber to listen to a topic, extract a string from the message, convert it to an integer using atoi() and then print to screen. But I want it to do this in a for loop, while a particular condition in it holds. Is this possible?
4) Can I pass more arguments to the chatterCallback function? say instead of
void chatterCallback(const std_msgs::String::ConstPtr& msg);
I want it to be
void chatterCallback(const std_msgs::String::ConstPtr& msg, int &i, char c);
Is this possible? If yes, what changes do I need to reflect in the argument for the callback function for subscribe()?
5)I ask the above question (4) because I want some way, in which I can bring the result of all that I do in my callback function, into my main() function. Is there a way in which I can do this?
6)In the ROS tutorial I described above, there are the following few lines which escape my understanding. Any idea as to what they mean?
The message is passed in a boost shared_ptr, which means you can store it off if you want, without worrying about it getting deleted underneath you, and without copying the underlying data.
I somehow feel the above lines are related to my questions (4) and (5) above.
7) What happens if I don't use ros::spin() at all, in my subscriber code? Does that mean that the ...
Thanks dornhege. In your response to (3), do you mean that being in the callback is like being in main() itself? Which means variables created and assigned values in the callback continue to exist in main()..?
No, the callback is just a function where you react on a new message and do whatever you want - including ignoring it by doing nothing.
It might help to split up multiple questions in different "question threads", otherwise there will be multiple mixed answers.
Your response to (5) is what I was really looking for. Also, I shall keep in mind to post multiple questions on different threads. I posted all in one, because I thought they were closely related. Thanks! :)
Thanks joq! Is that what Thomas D's comment means in the question I referred to above?
Yes, that is related.