Ask Your Question

Topic reading inside a callback

asked 2014-12-10 10:46:23 -0500

updated 2014-12-10 11:22:07 -0500


I have changed my question because I think what I am trying to do is bit different from what was explained here before.

I have a Callback and I need to read a different topic inside this callback. I don't know if it is possible, but I have to ensure that some part of the code is only executed if the other topic has a particular value.

Any case, I don't know if I can read a topic or execute a particular Callback while I am just executing one callback

Thank you.

edit retag flag offensive close merge delete


What is addCallback? Do you want to make another subscription or just "do the same thing as a callback would have" - in the latter case: It's just a function that you can call.

dornhege gravatar image dornhege  ( 2014-12-10 11:04:56 -0500 )edit

I have changed the question because It was totally confusing.

arenillas gravatar image arenillas  ( 2014-12-10 11:22:32 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2014-12-10 13:08:50 -0500

ajain gravatar image

updated 2014-12-11 11:47:36 -0500

I would suggest having 2 separate callbacks. And the code that requires certain variable to be set is executed only if respective flags are true. Also, I'd suggest using mutex on flags to avoid any race conditions. For eg.

class A 
    bool flag;
    std::mutex m;
    void callback1(...)
         //Set the flag
         flag = true;
    void callback2(...)
        //Use flag to check if you wanna execute the code
       bool isFlagTrue = flag;
       if (isFlagTrue)
             //Put your code here ...

I hope that's what you're looking for.


As Kramer suggested in his comment below, only one callback happens at a time. So you don't have to use mutex unless you have multi-threaded node process.

edit flag offensive delete link more



FYI: unless the node has multi-threaded spinning, there isn't a need for the mutex. When a node uses single-threaded spinning, only one callback executes at any time (see: Callbacks Overview).

kramer gravatar image kramer  ( 2014-12-10 18:28:10 -0500 )edit

You are correct. I'll add that as an edit to the answer. Thanks for correcting me.

ajain gravatar image ajain  ( 2014-12-11 11:46:10 -0500 )edit

answered 2014-12-10 12:06:17 -0500

Erwan R. gravatar image


You can't know the order in which messages will be received, so callbacks should be robust to information from other topics. From my experience of ROS, the best way of doing that consist in having callbacks that store data in node's class attributes, so they are accessible from other callbacks. You should also check if the data you need is available (the attribute has a consistent value) or skip processing if not. At most, callbacks should be independent from each other.

edit flag offensive delete link more

answered 2014-12-10 12:06:44 -0500

dornhege gravatar image

I won't work like that.

A callback is called back, i.e. it will be triggered iff there is data available from a publisher. You cannot request data from a callback.

The correct way to solve the problem is to synchronize the callbacks, i.e. collect data from both and only trigger your specific code if data from both is OK. For some common use cases, there already exist ROS methods, e.g. Stamped data from one callback and a transformation from TF.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-12-10 10:46:23 -0500

Seen: 185 times

Last updated: Dec 11 '14