ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Technically, I don't think the callbacks are actually random; rather, they occur in the order in which messages are received...even though that may appear to be random. I mention that because it indicates a fundamental misunderstanding of the mechanism: you do not call the callbacks, the callbacks are event-driven, ordered by message reception.
With that said, you want to impose some kind of message reception synchronization. Perhaps something in the message_filters package will do? Specifically, it seems to me that ApproximateTime would work for you.
If not, off the top of my head, a simple solution would be to use the callbacks to store message values then call a common method. The common method short-circuits unless both message values are filled, otherwise processing the stored values in the order you specify.
It's important to note that the callback mechanism already imposes per-message synchronization (unless you're multi-threading); that is, only one callback executes at any time, and does so to completion. So implementation is straightforward -- you can simply have a boolean flag for each message type, only performing full processing when both are true.
One final (unrelated) point: you do not need two NodeHandle
objects, as they are reference-counted copies of a global object. See NodeHandle Overview or, for more detail, the NodeHandle API (specifically the default constructor).