问题
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)
回答1:
You could use TimeSynchronizer
in rospy
.
This is an example to subscribe to 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 perception 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 not resolved, there is ApproximateTimeSynchronizer
rather than TimeSynchronizer
:
ts = message_filters.ApproximateTimeSynchronizer([image_sub, info_sub], 1, 1)
Reading More
来源:https://stackoverflow.com/questions/55458218/is-it-possible-to-time-synchronize-two-topics-in-ros-of-same-message-type