ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Subscriber callbacks run on their own thread, so I usually update a variable in the subscriber callback and then use that data in a main loop running in the startup thread. Something like this:

import rospy

class MyNode:
    def main(self):
        rospy.subscriber('sometopic', SomeMsgType, self.msgCallback)
        self.value = None
        self.rate = rospy.getParam('~rate', 10.0)
        while not rospy.is_shutdown():
            ... do something with self.value ...
            rate.sleep()

    def msgCallback(self, newValue):
        self.value = newValue

if __name__ == '__main__':
    try:
        MyNode().main()
    except rospy.ROSInterruptException:
        pass