How can we wait until subscriber has received a massage

asked 2020-04-25 05:09:42 -0500

azerila gravatar image

updated 2020-04-25 09:26:28 -0500

My goal is something like the following due to need for synchronization.

_sub = nh_.subscribe("topic1", 1000, &func1);

void ROBOT::func1() {
}


void ROBOT::func2(){
    _sub.wait_until_recieves_a_msg();
}

a hack would for example be:

_sub = nh_.subscribe("topic1", 1000, &func1);
void ROBOT::func1() {
        called = True;
}

void ROBOT::func2(){
     while(called==False){
                      }
     called = False
}

I know my c++ syntax from above is not correct most likely, but you get the idea.

edit retag flag offensive close merge delete

Comments

Yes you can.

Orhan gravatar image Orhan  ( 2020-04-25 07:44:29 -0500 )edit

@Orhan how

azerila gravatar image azerila  ( 2020-04-25 09:26:50 -0500 )edit

If I understand your question, and I may not, you would spin. http://wiki.ros.org/roscpp/Overview/C...

billy gravatar image billy  ( 2020-04-26 02:14:56 -0500 )edit

If I'm getting it correctly, you need to wait for the first message of something to arrive to start running a code block (...) in another subscriber. void ROBOT::myAnotherSubscriber(MyMessage &msg) { if (!this->my_received_condition) {return;} ... } and the other one, void ROBOT::myStarterSubscriber(MyMessage &msg) { this->my_received_condition = true; } Or if the another one isn't also a subscriber, just make it a timer and start the timer when the condition(s) are met: http://wiki.ros.org/roscpp/Overview/T...

Orhan gravatar image Orhan  ( 2020-04-26 08:53:56 -0500 )edit