publish after processing data from 2 synced subscribers (rospy)
I want to make one node to subscribe 2 topics synchronously, process those data, and publish it. I know that publishing and subscribing data with single node is possible, but have never seen any example with time synchronization in rospy. This is what I wrote so far (std_msgs in the code will be replaced with sensor_msgs later on to subscribe RGB image and Point cloud):
#!/usr/bin/env python
import rospy
import message_filters
import cv2
from cv_bridge import CvBridge
from std_msgs.msg import String
def callback(data1, data2):
rospy.loginfo(rospy.get_caller_id())
def listener():
sub_1 = message_filters.Subscriber("chatter", String)
sub_2 = message_filters.Subscriber("sub_chatter", String)
ts = message_filters.ApproximateTimeSynchronizer([sub_1, sub_2], 10, 0.1, allow_headerless=True)
ts.registerCallback(callback)
def talker():
pub = rospy.Publisher('Publisher', String, queue_size=10)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
pub.publish()
rate.sleep()
if __name__ == '__main__':
rospy.init_node('SubPub', anonymous=True)
listener()
talker()
rospy.spin()
I'm currently getting stuck on how to pass sub_1 and sub_2 to the publisher. I think that I need to define class and put everything into that class and use flag to publish data whenever callback is called. I'm kind of unsure how to integrate all these features or my idea is even valid. Does anyone have idea or example?