0
$\begingroup$

Rosanswers logo

I am currently facing a problem, that the Subscriber() is expecting 3 arguments, while in the docs it is always being done with 2: http://docs.ros.org/en/api/message_filters/html/python/

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):
    SimpleFilter.__init__(self)
    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):
    self.signalMessage(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.


Originally posted by KNTRL on ROS Answers with karma: 25 on 2021-06-15

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

Take a look at this example here from the message_filters tests: https://github.com/ros2/message_filters/blob/ffa56ac37da4261bf92156ed4186b915c1abe547/test/test_message_filters_cache.py#L71

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() https://docs.ros2.org/galactic/api/rclpy/api/init_shutdown.html#rclpy.create_node. Second argument is the type and third is the topic.


Originally posted by christophebedard with karma: 641 on 2021-06-15

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by KNTRL on 2021-06-15:
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.

Comment by christophebedard on 2021-06-15:
It did solve the question you asked, so can you mark the answer as correct?

Comment by KNTRL on 2021-06-16:
My bad, I'm still new to the site and didn't know this was a thing. Just did it. Thanks again!

Comment by christophebedard on 2021-06-16:
no worries, thank you! It just helps with tracking what's been answered and what hasn't!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.