Ask Your Question

Problem with Subscriber class from message_filter in Python

asked 2021-06-15 03:29:14 -0600

KNTRL gravatar image

I am currently facing a problem, that the Subscriber() is expecting 3 arguments, while in the docs it is always being done with 2:

Therefore I am getting an IndexError: tuple index out of range. Looking at the source code of Subscriber:

class Subscriber(SimpleFilter):

ROS2 subscription filter,Identical arguments as :class:`rclpy.Subscriber`.

This class acts as a highest-level filter, simply passing messages
from a ROS2 subscription through to the filters which have connected
to it.

def __init__(self, *args, **kwargs):
    self.node = args[0]
    self.topic = args[2]
    kwargs.setdefault('qos_profile', 10)
    self.sub = self.node.create_subscription(*args[1:], self.callback, **kwargs)

def callback(self, msg):

def getTopic(self):
    return self.topic

def __getattr__(self, key):
    """Serve same API as rospy.Subscriber"""
    return self.sub.__getattribute__(key)

you can see that let's say

imu_sub = Subscriber("imu/", Imu)

won't work, as it expects 3 arguments. As I consider myself still as a novice in Python and ROS2, I'd appreciate any help on this topic.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-06-15 08:03:33 -0600

updated 2021-06-15 08:04:33 -0600

I think the documentation for the Subscriber class is just confusing.

Take a look at this example here from the message_filterstests:

sub = Subscriber(self.node, String, "/empty")

so the first argument is a node you've created beforehand. You can do that with rclpy.create_node() Second argument is the type and third is the topic.

edit flag offensive delete link more


Thank you! I wish I could say it solved my problem in the grand scheme of things, but I just found out that making use of message_filter did nothing for me as it seems. I just posted another question regarding the core problem.

KNTRL gravatar image KNTRL  ( 2021-06-15 09:19:05 -0600 )edit

It did solve the question you asked, so can you mark the answer as correct?

christophebedard gravatar image christophebedard  ( 2021-06-15 10:08:51 -0600 )edit

My bad, I'm still new to the site and didn't know this was a thing. Just did it. Thanks again!

KNTRL gravatar image KNTRL  ( 2021-06-16 04:56:31 -0600 )edit

no worries, thank you! It just helps with tracking what's been answered and what hasn't!

christophebedard gravatar image christophebedard  ( 2021-06-16 08:00:06 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-06-15 03:29:14 -0600

Seen: 363 times

Last updated: Jun 15 '21