title: Topic synchronization in ROS using timestaps
body: <p>I have 2 rosbags, one with camera data, the other with lidar data, and I want to synchronize the topics listed below to get something like a kitti dataset.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Topics</th>
  <th style="text-align: center;">Types</th>
  <th style="text-align: right;">Message Count</th>
  <th style="text-align: right;">Frequency</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">/os_cloud_node/points</td>
  <td style="text-align: center;">sensor_msgs/PointCloud2</td>
  <td style="text-align: right;">3111</td>
  <td style="text-align: right;">20.115842</td>
  </tr>
  <tr>
  <td style="text-align: left;">/cam/realsense_d435_1/color/image_raw</td>
  <td style="text-align: center;">sensor_msgs/Image</td>
  <td style="text-align: right;">1488</td>
  <td style="text-align: right;">12.216965</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I tried to sync data using frequency (scripts below). I know that my simplification greatly distorts the result of these operations and my data is very quickly mismatched.</p>
  <p>How should I do it correctly using timestamps? I don't really know how to compare them when they come in different frequencies and as very small numbers.</p>
  <p><em>camera_loader.py</em></p>
  <pre><code>import rosbag
  from helper import convert2image
  
  bag = rosbag.Bag('data/cam2.bag')
  print(&quot;# File content:&quot;)
  print(bag.get_type_and_topic_info())
  
  messages = bag.read_messages(topics=[&quot;/cam/realsense_d435_1/color/image_raw&quot;])
  
  name=0
  
  for msg in messages:
      tittle=&quot;raw_images/&quot;+f&quot;{name:06d}&quot; +&quot;.png&quot;
      name=name+1
      print(tittle)
      i =convert2image(msg.message)
      i.save(tittle)
  </code></pre>
  <p><em>lidar_loader.py</em></p>
  <pre><code>import rosbag
  import open3d as o3d
  from helper import convert2pointcloud
  
  bag = rosbag.Bag('data/lidar2.bag')
  
  print(&quot;# File content:&quot;)
  print(bag.get_type_and_topic_info())
  messages = bag.read_messages(topics=[&quot;/os_cloud_node/points&quot;])
  
  name=0
  counter=0
  
  for msg in messages:
      if counter % 2 == 0:
          tittle = &quot;lidar_points/&quot;+f&quot;{name:06d}&quot;+&quot;.pcd&quot;
          print(tittle)
          name = name+1
          pcd = convert2pointcloud(msg)
          o3d.io.write_point_cloud(tittle, pcd, write_ascii=False, compressed=False, print_progress=False)
      counter=counter+1
  </code></pre>