Ask Your Question

TimeSynchronizer while subscribing to two camera topics using ROS2 - Foxy

asked 2021-08-13 00:10:04 -0600

NiranjanRavi gravatar image

updated 2021-08-13 16:37:18 -0600

jayess gravatar image

Hi, I am working with ROS 2- foxy distribution. I have two USB cameras simultaneously running and publishing images in topics - /usb_cam_0/image_raw and /usb_cam_1/image_raw . I want to subscribe the topics and concatenate the images and save them in a folder.

I went through this and tried to adapt the same logic as,

subscription_1 = message_filters.Subscriber('/usb_cam_0/image_raw', Image)

subscription_2 = message_filters.Subscriber('/usb_cam_1/image_raw', Image)

message_filters.ApproximateTimeSynchronizer([subscription_1, subscription_2],10,1)

But this script throws me an error in the subscriber class of message filter. The error was

File "/home/device/messageFilter/src/install/message_filters/lib/python3.6/site-packages/message_filters/", line 78, in __init__
    self.topic = args[2]
IndexError: tuple index out of range

Unable to clear this error. Other approach which worked was

subscription_1 = self.create_subscription(Image, '/usb_cam_0/image_raw', imageClass.camera_0_callback, 1)

But here i would have to use two separate callback functions which I don't want to.

The first logic works was from the dicsussion with ROS. But does it have a big difference with ROS2 - foxy? I am new to this platform. Any guidance would be helpful.

Regards Niranjan

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-08-14 15:18:56 -0600

Fetullah Atas gravatar image

updated 2021-08-14 15:20:07 -0600

Refer to code snippet below, this was quite troublesome to get it working but this code is known to be working under Foxy

typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::msg::Image,
            typedef message_filters::Synchronizer<RGBDApprxTimeSyncPolicy> RGBDApprxTimeSyncer;

            // subscriber for color cam in case rgbd camera model localization
            message_filters::Subscriber<sensor_msgs::msg::Image> rgbd_color_image_subscriber_;
            // subscriber for depth cam in case rgbd camera model localization
            message_filters::Subscriber<sensor_msgs::msg::Image> rgbd_depth_image_subscriber_;

        void rgbdCallback(
              const sensor_msgs::msg::Image::ConstSharedPtr & color,
              const sensor_msgs::msg::Image::ConstSharedPtr & depth);

                this, "camera/color/image_raw",
                this, "camera/depth/image_raw",
                new RGBDApprxTimeSyncer(
                  &YourClass::rgbdCallback, this, std::placeholders::_1,

    void YourClass::rgbdCallback(
        const sensor_msgs::msg::Image::ConstSharedPtr & color,
        const sensor_msgs::msg::Image::ConstSharedPtr & depth)

        auto colorcv = cv_bridge::toCvShare(color)->image;
        auto depthcv = cv_bridge::toCvShare(depth)->image;
        if (colorcv.empty() || depthcv.empty()) {
       // do something with recieved messages

edit flag offensive delete link more


Hi fetullah, thank you for the inputs. I am trying to utilize ApproximateTimeSynchronizer library in python. I have my code base in python and i want to integrate this code as well. I tried to re-work the above logic in c++ but the message filter error keeps re occurring.

NiranjanRavi gravatar image NiranjanRavi  ( 2021-08-18 10:35:30 -0600 )edit

I see, have you tried to look into message_filters test files, I see that here they have a python example which you may follow and try to adopt it ;

Fetullah Atas gravatar image Fetullah Atas  ( 2021-08-18 11:06:14 -0600 )edit

Hi thank you, yes, I tried the approach. But i had to increase the number of cams, so performed the synchronization operation at later stages.

NiranjanRavi gravatar image NiranjanRavi  ( 2021-09-20 17:00:55 -0600 )edit

Your Answer

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

Add Answer

Question Tools



Asked: 2021-08-12 23:54:11 -0600

Seen: 90 times

Last updated: Aug 14