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

Ask for current topic-content

asked 2012-08-11 06:47:18 -0500

termed gravatar image

updated 2012-08-11 22:08:25 -0500

Hello Guys,

I'm new to ros and although there are tutorials for using rostopic, I don't know how to integrate it in my project.

What I'm trying to do: I'm writing a python script* which spawns objects in a gazebo world. The positions and number of items is changing.

After every ne positioning, I'd want to get the current data with all (or certain) fields of one topic.

How can I do that? Is there a way, that I have like an object which subscribes to the topic, and I'll ask it hey, give me your current state? I really don't now how to incorporate those Subscriber-tutorial-pys in my basically independent py-script (I use plumbum for rosparam calls).

I would appreciate if you could help me to get a better impression on how to solve my problem.

I'm using ros-electric.


To make it a little bit more clear: Every time I launch an object, I want to check a certain topic. And from that topic I want to read the boundingbox, pos, rot, etc of model a, b, c. Those values are all written out to the topic I want to read. So I just need to now how to access them in a script (and just one iterarion, e.g. the current state).

*(the script is autonomous from the ros-projects. it basically calculates the position of those objects, writes thos on the paramserver and launches them)

edit retag flag offensive close merge delete


Thanks for your answer! The problem I have is not to have a topic which gives informations I need, but on how to read those informations in code. So, I mean, when I call echo in the console I get a shitload of data, but how do I get just one load, or how can I choose one?

termed gravatar image termed  ( 2012-08-11 22:03:13 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2012-08-11 10:57:49 -0500

prasanna.kumar gravatar image

I think you can get what you are looking for here. You can use gazebo/GetModelState for that purpose. You can call this service programatically instead of subscribing to a topic for object state. Hope this helps!

edit flag offensive delete link more

answered 2012-08-12 02:57:02 -0500

termed gravatar image

updated 2012-08-12 07:49:22 -0500

It would be awesome if someone could tell me why this is alway returning the same values after the first call (see comment). It's quite simple actually. If anyone else ever has this problem hier is the script. If you call listener(), the callback is invoked just once, so you get back just one object. So from my other script I just import this script and call getObj() which calls listener() and therefore callback() (once) and then stores the variable globally so that I can get access and return it. It may not be pretty, but it works, and I found no other solution.

#!/usr/bin/env python
import roslib; roslib.load_manifest('myPackage')
import rospy
from std_msgs.msg import String
from my_msgs.msg import MyMessageType
from helpers import DbItem
OBJL = []
def getObj():
    return OBJL
def callback(data):
    global OBJL
    header = data.header
    models = data.model
    bbox = data.model_bbox
    pose = data.pose

    objList = [] 
        obj1Index = models.index('myModelname')
        obj1 = DbItem(time=header.stamp, name=models[obj1Index], pose=pose[obj1Index], bbox=bbox[object1Index])  
    except ValueError:
        print "Object not in List..."
    OBJL = objList
    rospy.signal_shutdown("response message")

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("myTopic", MyMessageType, callback)
    if __name__ == '__main__':
edit flag offensive delete link more


Actual this has a major issue. After I called this once, the next calls will always have the exact same values. Why that?

termed gravatar image termed  ( 2012-08-12 07:48:15 -0500 )edit

Question Tools

1 follower


Asked: 2012-08-11 06:47:18 -0500

Seen: 331 times

Last updated: Aug 12 '12