how to let a function wait for the completion of callback function
I have a class which needs to subscribe two topics, and I want to store the message into the class variable so that I can use it in another method (function) within that class. So I write c++ this: (for simplicity, I ignore some detail)
class A:
private:
msg1;
msg2;
public:
void callback1(msg1){
this->msg1 = msg1;
}
void callback2(msg2){
this-msg2 = msg2;
}
void myFunction(){
doSomeThing(msg1, msg2);
}
int main(int argc, char** argv){
INIT_NODE;
A.myFunction();
ROS::SPIN();
}
My problem is that when I call myFunction(), msg1 and msg2 have not been obtained by callback functions. So the doSomeThing(msg1, msg2) will get empty input. My question is that is there any way that myFunction can wait for completion of callback1 and callback2?
Quick comment: you probably want to use
message_filters
instead. Especially if these are two messages withHeader
s. If you really don't want to use that, look atmessage_filters/Cache
.I would also recommend to try and search some older Q&As about this topic, as there are very many already. Use Google, and add
site:answers.ros.org
to your search terms.Thank you very much!