rclpy - call publisher's publish method when data in queue is available

asked 2019-12-25 18:05:08 -0500

Fevi gravatar image

updated 2019-12-26 23:11:07 -0500

Hi,

I am wrapping a publisher to make it accept data from a python queue (q). Basically, the idea is to use a subscriber with an output q that is read by a "processor" block. In it, some transformation occurs before the data finally gets republished.

In order to allow for multiple "processors" in succession before data gets published, my thinking is that the "processors" need to have an input q and an output q. Therefore, my publisher will need to have its data input from the last "processors" output queue and publish data as soon as there is some available from the queue.

From looking at rclpy, it seems that this can be achieved by using a guard condition that is when the last "processors" output q is not empty. Presumably, one would use the same strategy to make the "processor" process data when available.

  • Is the above approach what one should use? I'm in no way married to this idea if there's easier ways to get it done.
  • Is anyone aware of an example on how one can add a custom callback function to a ROS2 node in python?

Thank you very much for taking the time to answer!

Edit 1: So far, the concept suggested here https://answers.ros.org/question/302037/ros2-how-to-call-a-service-from-the-callback-function-of-a-subscriber/ appears to be the most straightforward one. Basically, one can check on the q status every time after spin_once() is called. This seems much more straightforward than what I was initially thinking - but does this simple approach come with problems? It seems like this should be OK for I/O heavy tasks like catching data from a queue - presumably the abovementioned "processors" should run in a separate MultiThreadedExecutor.

Best, Felix

edit retag flag offensive close merge delete