2
$\begingroup$

Rosanswers logo

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.


Originally posted by paul_shuvo on ROS Answers with karma: 45 on 2019-02-12

Post score: 0

$\endgroup$

3 Answers 3

2
$\begingroup$

Rosanswers logo

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.


Originally posted by paul_shuvo with karma: 45 on 2019-02-28

This answer was NOT ACCEPTED on the original site

Post score: 2

$\endgroup$
1
$\begingroup$

Doing a rostopic hz on each topic is a good starting point to make sure they are publishing at all.

This could help debug further:

https://github.com/lucasw/topic_state/blob/master/scripts/multi_echo.py

rosrun topic_state multi_echo.py /camera/rgb/image_color /camera/rgb/camera_info _use_sync:=true

If that doesn't result in anything try disabling the time sync:

rosrun topic_state multi_echo.py /camera/rgb/image_color /camera/rgb/camera_info _use_sync:=false

It doesn't yet support approximate time sync, it's making individual subscribers for each topic and will print the timestamps so you can manually inspect to see if they exactly match or if not how much they differ from each other- similar to the advice in the PeteBlackerThe3rd imported answer but the multi_echo script saves the trouble of temporarily modifying your code for debug.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by PeteBlackerThe3rd with karma: 9529 on 2019-02-13

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

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