ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

You should be able to do the same thing below. The changes are

  • Initialize sub_msg to None, and while it is None, it is not received. This will remove is_subbed.
  • The judgment of whether it was received is now a separate loop. I feel this is a better outlook. Also, by executing r.sleep(), you can reduce the processing load while waiting for reception.

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.sub_msg = None

    def sub_callback(self, msg):
        self.sub_msg = msg

    def spin(self):
        r = rospy.Rate(self.rate)
        while self.sub_msg == None:
            r.sleep()
        while not rospy.is_shutdown():
            print(self.sub_msg) 
            r.sleep()

if __name__ == "__main__":
    SC = SomeClass()
    SC.spin()