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

return a message type from callback

asked 2017-02-15 03:25:13 -0500

ashwath1993 gravatar image

updated 2017-02-15 06:33:41 -0500

Is there a way to return anything other than void from a callback?
I want to return a message from the call back as I want to define the callback as .h so i can call it multiple times for different subscribers.
Is this possible in ros?

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}

since i can change the callback in general is a function. But where can I return the value to?

ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

Since the callback is called from the subscribe. image description I have a separate callback for each of those topics, even though they carry the same message (left side carries one type and right the other). Is there a way to use a single callback function to handle both types of messages and handle all the requests at the same time?

edit retag flag offensive close merge delete

Comments

I want to return a message from the call back as I want to define the callback as .h so i can call it multiple times for different subscribers.

Can you clarify why you think that would require the function to have a return type?

gvdhoorn gravatar image gvdhoorn  ( 2017-02-15 03:32:11 -0500 )edit

I want it to return a type so that I can call it through a header file and run multiple instances of the same header file. Reducing the size of my code as I currently have a callback for each subscriber.

ashwath1993 gravatar image ashwath1993  ( 2017-02-15 04:02:38 -0500 )edit

I don't see why simply using the callback by class wouldn't solve your issue : http://wiki.ros.org/roscpp/Overview/P... part 2.3.2 ?

TTDM gravatar image TTDM  ( 2017-02-15 04:09:08 -0500 )edit
1

I fail to see how returning a type and registering a callback multiple times have anything to do with each other.

Callbacks are just functions or methods. There is nothing special about them other than that they have a void return type and accept a single arg (the msg).

gvdhoorn gravatar image gvdhoorn  ( 2017-02-15 04:15:46 -0500 )edit

@gvdhoorn I wanted to know if their return type can be changed and if so, how. I want to run a single callback function to handle the messages based on the topic they listen to. I have built the system with multiple callbacks, wanted to know if there is an alternative.

ashwath1993 gravatar image ashwath1993  ( 2017-02-15 05:29:07 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-02-15 05:49:03 -0500

TTDM gravatar image

updated 2017-02-15 08:46:20 -0500

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

edit flag offensive delete link more

Comments

I tried to use message filters to aggregate the messages from multiple topics. But the system doesn't work for custom messages. I haven't been able to find a simple example to run it. The one on the wiki is not easy to understand.

ashwath1993 gravatar image ashwath1993  ( 2017-02-15 09:07:53 -0500 )edit
1

So your issue was about synchronisation, if you had clearly explained what was your aim and your issue, it would have been easier to help you... Message filter and time synchronizer should work for custom messages as long as you have a header in your custom message with a filled stamp.

TTDM gravatar image TTDM  ( 2017-02-15 09:47:50 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-15 03:25:13 -0500

Seen: 2,102 times

Last updated: Feb 15 '17