ROS 2 Topics synchronizes

74 Views Asked by At

I have a research project, using 2 Basler cameras (both a2A1920-16ucPRO) to create a disparity map. I extract the images via ROS. For this I have subscribed to 2 Topics, where I regularly get images at 2 hertz. The problem is that these 2 images have a time difference of 5-10 milliseconds, but I need to get synchronously the 2 images. The cameras are connected to my PC via USB. Is there any way to set something in ROS that I get 2 images in the same time? Or is there another solution? Here is my code:

import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
import time
import message_filters
Instantiate CvBridge

bridge = CvBridge()

def callback1(b1, b2):
    print("Aufruf beide Kameras!")
    try:
   
       cv2_img1 = bridge.imgmsg_to_cv2(b1, "bgr8")
       cv2_img2 = bridge.imgmsg_to_cv2(b2, "bgr8")

    except CvBridgeError, e:
           print(e)
    else:
  
       img_gray_left=cv2.cvtColor(cv2_img1, cv2.COLOR_BGR2GRAY) 
       img_gray_right=cv2.cvtColor(cv2_img2, cv2.COLOR_BGR2GRAY)
       dimension=(1024,440)
       resized1=cv2.resize(img_gray_left, dimension, interpolation=cv2.INTER_AREA)
       resized2=cv2.resize(img_gray_right, dimension, interpolation=cv2.INTER_AREA)
       cv2.imwrite('multilayer-stixel-world/build/myimages_ros/camera_image_left.pgm', resized1)
       cv2.imwrite('multilayer-stixel-world/build/myimages_ros/camera_image_right.pgm', resized2)
    



def main():
    rospy.init_node('image_listener', anonymous=True)
    image_topic1 = message_filters.Subscriber('/ace_RGB_Left/pylon_camera_node/image_raw', Image)
    image_topic2 = message_filters.Subscriber('/ace_RGB_Right/pylon_camera_node/image_raw',Image)
    ts = message_filters.ApproximateTimeSynchronizer([image_topic1, image_topic2],2, 0.005)
    ts.registerCallback(callback1)
    rospy.spin()

if _name_ == '_main_':
    main()

The message function "TimeSynchronizer" dont work, because the timestamps do not fit perfectly. So i use the function "ApproximateTimeSynchronizer" with a tolerance of 5 milliseconds. But that is not enough.

0

There are 0 best solutions below