Can a topic be subscribed in two different function of the same program
I wanted to subscribe same topic in two different functions of the same code. so I just wanted to know, will the subscriber stops subscribing after it comes out of the function or it still keeps on subscribing.
just the format I have followed,
finction_1()
{
ros::subscriber sub= n.subscribe("topic", 1000, topic_callback);
}
finction_2()
{
ros::subscriber sub= n.subscribe("topic", 1000, topic_callback);
}
int main ()
{
function_1();
function_2();
}
will this format create any conflict?
It might be helpful to know what you are planning to do. As ahendrix said, it's possible to create the subscribers inside the function but they will be destroyed right away. If you just need some data inside your function block, maybe have a look at
ros::topic::waitForMessage
.I'm a little confused. Creating a subscriber sets up a callback function to be executed every time a message is received. Are you trying to subscribe two callbacks to the same topic or are you trying to subscribe and unsubscribe to the topic at different times within your node?
my query is, once the function scope is over the subscriber stops subscribing?
The way that you have written the code above, yes the subscriber stops subscribing when the function returns. However if the variable
sub
was global then it would keep subscribing after the function returns.yes, it is global variable. And it keep on subscribing after the function returns. but I have resolved the issue of multiple subscription.
thank you