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

message_filters doesn't call the callback function

asked 2019-02-12 18:59:39 -0600

paul_shuvo gravatar image

updated 2020-11-01 13:59:42 -0600

I'm trying to use the message_filters in order to subscribe to two topics. Here's my code

class sync_listener:
    def __init__(self):
        self.image_sub = message_filters.Subscriber('camera/rgb/image_color', Image)
        self.info_sub = message_filters.Subscriber('camera/rgb/camera_info', CameraInfo)
        self.ts = message_filters.TimeSynchronizer([self.image_sub, self.info_sub], 10)
        self.ts.registerCallback(self.callback)

    def callback(self, image, camera_info):
        print("done")

def main(args):
    ls = sync_listener()
    rospy.init_node('sample_message_filters', anonymous=True)
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")

if __name__ == '__main__':
    main(sys.argv)

But it never goes to the callback function. It just freezes. I found a similar question here Rospy problem with time synchronizer message filter but there was no answer.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2019-02-28 17:37:40 -0600

paul_shuvo gravatar image

updated 2020-11-02 23:50:40 -0600

Rather than using TimeSynchronizer I used ApproximateTimeSynchronizer and it worked. So, I changed the code to-

class sync_listener:
    def __init__(self):
        self.image_sub = message_filters.Subscriber('camera/rgb/image_color', Image)
        self.info_sub = message_filters.Subscriber('camera/rgb/camera_info', CameraInfo)
        self.ts = message_filters.ApproximateTimeSynchronizer([self.image_sub, self.info_sub], 1, 1) # Changed code
        self.ts.registerCallback(self.callback)

    def callback(self, image, camera_info):
        print("done")

def main(args):
    ls = sync_listener()
    rospy.init_node('sample_message_filters', anonymous=True)
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")

if __name__ == '__main__':
    main(sys.argv)

Before finding this solution, I just used global variables to access the message of the first topic by assigning the message to the global variable in the callback and used it on the callback of the second, and that's how I was able to work with both. It's not clean but saves hours of frustration.

edit flag offensive delete link more
1

answered 2019-02-13 10:42:01 -0600

You appear to be subscribing to an image topic and a camera_info topic from different cameras, or the topics have been named in an unusual way.

The TimeSynchronizer requires that messages must have identical time stamps for the callback to be triggered. So if the time stamps are slightly different then the callback will never be executed.

In order to test this do you want to setup two normal subscribers which print out the topic name and time stamp for each topic, then you can debug the time stamps of both topics.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-02-12 18:59:39 -0600

Seen: 2,177 times

Last updated: Nov 02 '20