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

Revision history [back]

You should not sleep in subscriber callbacks. The sleeps will block all other callbacks if you have just one spin thread. And I guess that's exactly what's happening. What probably happens is the following:

  1. callbackInt is executed and sleeps for 5 secs.
  2. callbackString is executed and doesn't sleep at all (the 5 secs are over already).
  3. callbackString might be executed again depending on the implementation of spin, It publishes again and sleeps 5 seconds.

That means withing 5 seconds, you get two messages re-published for the string which would explain the rate of 2.5Hz.

Instead of sleeping in the callbacks, implement your re-publisher differently. The simplest solution would be to re-publish in a main loop if enough time since the last publish call passed. Don't forget to call spinOnce in your main loop to execute the subscriber callbacks.