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

How to get value from a subscribed topic in ros in python

asked 2019-04-15 13:50:51 -0500

Tawfiq Chowdhury gravatar image

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()
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2019-04-15 14:17:21 -0500

jayess gravatar image

updated 2019-04-15 14:43:11 -0500

You can:

  1. use a global variable (not recommended for anything serious)
  2. use a custom class in which you save the data you a member variable
  3. use event-driven programming in which you call other functions from within the callback and pass the data to those functions

Update

my_data = None

def callback(data):
    # you have to let python know that this is a global otherwise
    # it thinks that it's a local variable
    global my_data 
    my_data = data.data
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", my_data)
edit flag offensive delete link more

Comments

How do I store the value of data.data in a global variable? I cannot directly do this: variable=data.data

Could you please hint or show?

Tawfiq Chowdhury gravatar image Tawfiq Chowdhury  ( 2019-04-15 14:23:57 -0500 )edit

Did you look at the links that I posted? The first item shows how to do that. I'll update with a quick example. But, remember that you really don't want to be using globals. Using them is a really good way to make buggy, hard to fix code.

jayess gravatar image jayess  ( 2019-04-15 14:40:50 -0500 )edit

I have not yet, I will look at it later, yes, please show the other way, it will help. Thanks a lot.

Tawfiq Chowdhury gravatar image Tawfiq Chowdhury  ( 2019-04-15 14:47:17 -0500 )edit

Just so you know, this is more of a Python question than a ROS one.

jayess gravatar image jayess  ( 2019-04-15 14:50:56 -0500 )edit
0

answered 2019-04-15 14:25:37 -0500

Nikodem gravatar image

Hey,

The code you provided doesn't work, because you miss the comma after first String. Other than that it's completely fine.

print("I am accessing data.data", data.data) # trying to access data.data

Simpliest solution to store the data for later would be to use global variables. It's probably not very elegant way to do things, but should work fine in less complicated applications.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-04-15 13:50:51 -0500

Seen: 4,783 times

Last updated: Apr 15 '19