Hello, I am wondering what is the best practice in python to wait for a subscribed topic to start publishing, before using its messages in the spin method. An simplified example of what I am doing right now is provided below, but I was thinking if there was a more elegant solution.
If remove the is_subbed flag I get the AttributeError, that the class has no attribute 'sub_msg'. I guess this could be fixed by initializing self.sub_msg in the _init_ method with some arbitrary value, which will then be used for the first few runs of the spin method. But I need to get the correct messages right from the start. I hope this hasn't been asked yet, at least I haven't found a good answer yet.
class SomeClass(object):
def __init__(self):
rospy.init_node('node_name', anonymous=True)
self.sub = rospy.Subscriber('sub_name', sub_type, self.sub_callback, queue_size=10)
self.rate = 100
self.is_subbed = False
def sub_callback(self, msg):
self.sub_msg = msg
self.is_subbed = True
def spin(self):
r = rospy.Rate(self.rate)
while not rospy.is_shutdown():
# Skip method if callback has not been called yet
if not self.is_subbed:
continue
print(self.sub_msg)
r.sleep()
if __name__ == "__main__":
SC = SomeClass()
SC.spin()
Originally posted by flix17 on ROS Answers with karma: 1 on 2021-12-11
Post score: 0