[PYTHON] [Memo] Subscribe messages from multiple topics with ROS


In ROS, you often subscribe to multiple topics, so make a note of how to do it at that time.

The following articles were very helpful. Receive messages synchronously from multiple topics in ROS


Use `` `message_filters``` to subscribe to multiple messages. For example, if you extend the listener of ROS tutorials,

#/usr/bin/env python

import message_filters
import rospy
from std_msgs.msg import Int32

# Define callback
def callback(msg1, msg2):

# Initialize Subscriber node

# Define Subscriber
sub1 = message_filters.Subscriber('listener1', Int32)
sub2 = message_filters.Subscriber('listener2', Int32)

queue_size = 10
fps = 100.
delay = 1 / fps * 0.5

mf = message_filters.ApproximateTimeSynchronizer([sub1, sub2], queue_size, delay)


Multiple subscribers are synced with message_filters.ApproximateTimeSynchronizer ().

In the case of python2, note that it will be calculated as delay = 0 unless it is made into a float type like fps = 100.!

Recommended Posts

[Memo] Subscribe messages from multiple topics with ROS
Cannot upload multiple images from form with FastAPI
Remove headings from multiple format CSV files with python