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

Introduction

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

Method

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):
   print(msg1.data)
   print(msg2.data)

# Initialize Subscriber node
rospy.init_node('listener')

# 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)
mf.registerCallback(callback)

rospy.spin()

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