4

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 at rospy.spin().

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
paul-shuvo
  • 1,874
  • 4
  • 33
  • 37
  • 1
    Have you confirmed that the messages you are listening for are actually being published? Maybe `rostopic echo` the ones you are interested in in an adjacent terminal while you run your test to make sure they are working as expected. – Hal Jarrett Feb 21 '19 at 14:41
  • Yes, I did. I also subscribed to `rgb` and `depth` image topics individually and `imshow`ed on the callback. It works. I'm just unable using them suing `message_filters` . – paul-shuvo Feb 23 '19 at 04:54
  • The code you've posted works well (at least at my device). For me it looks like there are no messages published a a defined topic. You should check the topics and their types again. – Fruchtzwerg Feb 26 '19 at 07:16
  • 1
    Did you fix this ? I'm facing the exact same problem ;( – lamhoangtung Feb 27 '19 at 13:21
  • @Iamhoangtung, I've posted my answer. Let me know if it helps. :) – paul-shuvo Feb 28 '19 at 23:31

1 Answers1

2

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.

paul-shuvo
  • 1,874
  • 4
  • 33
  • 37
  • 1
    Even after using "ApproximateTimeSynchronizer", the callback function is not being called. Can you suggest me something else? I requ. Thank you. – R June Dec 02 '19 at 10:18