ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

1/ I wanted to know if their return type can be changed and if so, how. => I don't think so.

2/ I want to run a single callback function to handle the messages based on the topic they listen to. => well nothing stops you to call the same callback on differents topics, if their message types are the same. If the message types differ,you can probably template your argument "msg_type::ConstPtr& msg" and then test inside your callback which message type you have received and execute different things depending on this type. I don't see why you would do so instead of creating multiple callbacks but it seems possible to me. ( I believe that the multiple callbaks would be created anyway, it's just that you wouldn't see it since your compiler would create thoses multiple callbacks for you )

3/ I have built the system with multiple callbacks, wanted to know if there is an alternative. => this looks like the exact same question as 2/

The real question is why do you want to have a single callback ? The way you ask your questions, it seems it is because you need to aggregate information from multiple topics and you don't know how to do it ? If that's really what you need to do, one way to do so is to use class method.

void Foo::callback(const std_msgs::StringConstPtr& message)
{
}

...
Foo foo_object;
ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);

Answering to your last comment:

1/ I wanted to know if their return type can be changed and if so, how. => I don't think so.

2/ I want to run a single callback function to handle the messages based on the topic they listen to. => well nothing stops you to call the same callback on differents topics, if their message types are the same. If the message types differ,you can probably template your argument "msg_type::ConstPtr& msg" and then test inside your callback which message type you have received and execute different things depending on this type. I don't see why you would do so instead of creating multiple callbacks but it seems possible to me. ( I believe that the multiple callbaks would be created anyway, it's just that you wouldn't see it since your compiler would create thoses multiple callbacks for you )

3/ I have built the system with multiple callbacks, wanted to know if there is an alternative. => this looks like the exact same question as 2/

The real question is why do you want to have a single callback ? The way you ask your questions, it seems it is because you need to aggregate information from multiple topics and you don't know how to do it ? If that's really what you need to do, one way to do so is to use class method.

void Foo::callback(const std_msgs::StringConstPtr& message)
{
}

...
Foo foo_object;
ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);

Answering to your last comment:

1/ I wanted to know if their return type can be changed and if so, how. => I don't think so.

2/ I want to run a single callback function to handle the messages based on the topic they listen to. => well nothing stops you to call the same callback on differents topics, if their message types are the same. If the message types differ,you can probably template your argument "msg_type::ConstPtr& msg" and then test inside your callback which message type you have received and execute different things depending on this type. I don't see why you would do so instead of creating multiple callbacks but it seems possible to me. ( I believe that the multiple callbaks would be created anyway, it's just that you wouldn't see it since your compiler would create thoses multiple callbacks for you )

3/ I have built the system with multiple callbacks, wanted to know if there is an alternative. => this looks like the exact same question as 2/

The real question is why do you want to have a single callback ? The way you ask your questions, it seems it is because you need to aggregate information from multiple topics and you don't know how to do it ? If that's really what you need to do, one way to do so is to use class method.

void Foo::callback(const std_msgs::StringConstPtr& message)
{
}

...
Foo foo_object;
ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);
//information available in foo_object

Answering to your last comment:

1/ I wanted to know if their return type can be changed and if so, how. => I don't think so.

2/ I want to run a single callback function to handle the messages based on the topic they listen to. => well nothing stops you to call the same callback on differents topics, if their message types are the same. If the message types differ,you can probably template your argument "msg_type::ConstPtr& msg" and then test inside your callback which message type you have received and execute different things depending on this type. I don't see why you would do so instead of creating multiple callbacks but it seems possible to me. ( I believe that the multiple callbaks would be created anyway, it's just that you wouldn't see it since your compiler would create thoses multiple callbacks for you )

3/ I have built the system with multiple callbacks, wanted to know if there is an alternative. => this looks like the exact same question as 2/

The real question is why do you want to have a single callback ? The way you ask your questions, it seems it is because you need to aggregate information from multiple topics and you don't know how to do it ? If that's really what you need to do, one way to do so is to use class method.

void Foo::callback(const std_msgs::StringConstPtr& message)
{
}

...
Foo foo_object;
ros::Subscriber sub = nh.subscribe("my_topic", 1, &Foo::callback, &foo_object);
//information available in foo_object

Edit with your added image :

What you want is a callback(msg1, msg2, msg3 ....) ?? I don't think that's possible. If you really want to have a single callback in your fb1 node, then you could create a new node "agregator" which create a new unique message from all other messages and then create only one callback in your main since you would have only one topic to receive

The question why you want a unique callback still remain.... If it's in order to share information, the answer is above, if it's to have a simpler/clearer code, just create all your nodehandler/subscriber inside a function "all_callbacks"..... if it's to synchronize your messages you can use a message filter http://wiki.ros.org/message_filters to synchronize the topics