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

Revision history [back]

If you want to do this properly, there is no way around adding a Header to the messages. Otherwise you have no information at all about when the messages are created, only when they are received by your node. There might be different delays etc. on the two topics, so that is going to fail in a lot of cases.

If you have no way of modifying the publisher so that it includes a Header, the only thing you can do is wait until you've received one message on each topic and then pretend they are synchronized, like this:

msg1 = msg2 = null;

motor1_callback(msg) {
   msg1 = msg;
   check_and_process();
}

motor2_callback(msg) {
   msg2 = msg;
   check_and_process();
}

check_and_process() {
   if (msg1 != null && msg2 != null) {
       do_something;
       msg1 = msg2 = null;
   }
}

Take-home message: add a Header to your messages, modify your publisher and use message_filters if at all possible.

If you want to do this properly, there is no way around adding a Header to the messages. Otherwise you have no information at all about when the messages are created, only when they are received by your node. There might be different delays etc. on the two topics, so that is going to fail in a lot of cases.

If you have no way of modifying the publisher so that it includes a Header, the only thing you can do is wait until you've received one message on each topic and then pretend they are synchronized, like this:

msg1 = msg2 = null;

motor1_callback(msg) {
   msg1 = msg;
   check_and_process();
}

motor2_callback(msg) {
   msg2 = msg;
   check_and_process();
}

check_and_process() {
   if (msg1 != null && msg2 != null) {
       do_something;
do_something(msg1, msg2);
       msg1 = msg2 = null;
   }
}

Take-home message: add a Header to your messages, modify your publisher and use message_filters if at all possible.