pythonpython-3.xrosrospy

Is it possible to time synchronize two topics in ROS of same message type?


I try to map robot gripper position to the resistance exerted by the object held by the gripper. I subscribed the gripper position from one topic and resistance value from another topic, since I want to ensure that the gripper position corresponds to the exact resistance value at that position. Given that both are float messages, how can I synchronize them?

self.sub1 = rospy.Subscriber("resistance", Float64, self.ard_callback)
self.sub2 = rospy.Subscriber("gripperpos", Float64, self.grip_callback)

Solution

  • You could use TimeSynchronizer in rospy.

    This is an example of subscribing multiple topics to get data at the same time:

    import message_filters
    from sensor_msgs.msg import Image, CameraInfo
    
    def callback(image, camera_info):
      # Solve all of perceptions here...
    
    image_sub = message_filters.Subscriber('image', Image)
    info_sub = message_filters.Subscriber('camera_info', CameraInfo)
    
    ts = message_filters.TimeSynchronizer([image_sub, info_sub], 10)
    ts.registerCallback(callback)
    rospy.spin()
    

    If your problem was not resolved, use ApproximateTimeSynchronizer rather than TimeSynchronizer:

    ts = message_filters.ApproximateTimeSynchronizer([image_sub, info_sub], 1, 1)  
    

    Read More