Ask Your Question
0

message_filters doesn't call the callback function

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

paul_shuvo gravatar image

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/projector/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
1

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

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
0

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

paul_shuvo gravatar image

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/projector/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

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

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

Seen: 152 times

Last updated: Feb 28