ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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: http://docs.ros.org/en/api/message_fi...

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

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: https://github.com/ros2/message_filte...

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/rc.... Second argument is the type and third is the topic.

edit flag offensive delete link more

Comments

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
1

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

Question Tools

1 follower

Stats

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

Seen: 1,877 times

Last updated: Jun 15 '21