How to get value from a subscribed topic in ros in python
I have the following code from the ros subscriber tutorial:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
def listener_new():
rospy.init_node('listener_new', anonymous=True)
rospy.Subscriber("locations", String, callback)
rospy.spin()
if __name__ == '__main__':
listener_new()
I need to know how I may store the value of data.data in callback function so that I may use it for further purposes, I tried the following code but did not work:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
print("I am accessing data.data," data.data) # trying to access data.data
def listener_new():
rospy.init_node('listener_new', anonymous=True)
rospy.Subscriber("locations", String, callback)
rospy.spin()
if __name__ == '__main__':
listener_new()