8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【メモ】ROSで複数トピックからメッセージをSubscribe

Last updated at Posted at 2020-07-12

はじめに

ROSでは複数のトピックをSubscribeする場合が多いので、そのときのやり方をメモ

以下の記事がとても参考になりました。
ROSで複数のトピックから同期的にメッセージを受け取る

#方法
複数のメッセージをSubscribeするには、message_filtersを使う。
例えば、ROSのtutorialsのlistenerを拡張すると、

#/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()

message_filters.ApproximateTimeSynchronizer()で複数のsubscriberを同期している。

尚、python2の場合は、fps=100.のようにfloat型にしなければdelay=0と計算されてしまうので注意!

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?