How do you create multiple publishers in one node using the Python Multiprocessing package?

asked 2018-04-17 06:02:59 -0600

Matt31 gravatar image

I have multiple ultrasonic sensors which are connected to my PC via a set of USB ports. I can successfully read data from the sensors, process it and publish it to an ROS topic. However, I wish to read data from each sensor simultaneously. Rather than create a node for each sensor, I decided to create a set of publishers (one for each sensor) using the Python multiprocessing package.

Whilst I have managed to spawn multiple processes etc, the Rospy publisher refuses to publish any data from the child processes I have spawned. Specifically, the process I followed was:

  1. I declared the ultrasonic sensor parameters and publisher; one for each sensor. Each sensor is an object of a custom class, the publisher is a parameter within this class.
  2. I spawned a process for each sensor, and started them, as follows:

    procs = []
    for i, sonar in enumerate(self.sonars):
        sonar.open_port()
        proc = Process(target=sonar.measuring_loop)
        procs.append(proc)
        proc.start()
    
  3. Within each process, the sensor is sampled and the data obtained is published via a Rospy publisher.

The data is correct, and each process is looping correctly, so the issue is simply with the Rospy Publisher.

Do you know if there is something I have missed in my methodology?

Thanks

edit retag flag offensive close merge delete

Comments

The issue is most likely that forking is not properly supported with rospy. That is why you're not seeing any msgs being published.

I don't have any references handy right now, but if you search for rospy multiprocess you'll probably find some discussions about this.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-17 07:20:01 -0600 )edit

Is there any reason you can't just publish to the same topic, once for each sensor and then periodically? If you're using the standard msg for sonars (Range), it includes a Header with a frame_id field that ca be used to ID sensors.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-17 07:22:18 -0600 )edit

I've found some discussions on this issue, but none that properly address the way I'm tackling the problem. I guess I could publish to the same topic, but this would result in having to poll the USB port sequentially for each sensor, which would reduce the update rate of each sensor substantially

Matt31 gravatar image Matt31  ( 2018-04-17 07:51:00 -0600 )edit

but none that properly address the way I'm tackling the problem.

which problem are you referring to here? rospy + multiprocess has not been resolved afaik, so iiuc, it won't work right now.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-17 07:52:50 -0600 )edit

The rospy + multiprocess issue, which, as you have said, does not work. I'll have to create a node for each sensor or publish to the same topic as you described. Thanks for your help.

Matt31 gravatar image Matt31  ( 2018-04-17 07:58:15 -0600 )edit

I'll have to create a node for each sensor

Just making sure: you're talking about multiple instances of the same node, right?

publish to the same topic

the decision to (not) do this seems disconnected from whether you have multiple nodes or not?

gvdhoorn gravatar image gvdhoorn  ( 2018-04-17 08:10:12 -0600 )edit

I have a set (ring) of USB ultrasonic sensors. I need each of them to publish their data simultaneously. I originally intended to do this using one node and the Python multiprocessing package. Instead, I shall have to create multiple instances of the same node to achieve the same parallel system.

Matt31 gravatar image Matt31  ( 2018-04-17 08:17:02 -0600 )edit