ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
It is not necessary to store a reference to a Subscriber (but you can!). It will keep working even after that the __init__
has been completed (of course as long as an instance of SomeClass
is defined and the node is running).
As for the Publisher, in general, yes you should store a reference to it. In this way, you can publish also from outside the function in which you have defined it.
I hope that this example can help you:
class SomeClass:
def __init__(self):
# Defined Subscriber without reference
rospy.Subscriber("/some/topic1", SomeMessageType, self.some_topic1_callback)
# Defined Publisher with reference
self.my_pub = rospy.Publisher("/some/topic2", SomeMessageType, queue_size=1)
def some_topic1_callback(self, msg):
# Do something with your message
new_msg = self.great_function_doing_stuff(msg)
# Publish the result
self.my_pub.publish(new_msg)