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

Revision history [back]

click to hide/show revision 1
initial version

Okay, I got this. 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():
    listener()
    return OBJL
def callback(data):
    global OBJL
    header = data.header
    models = data.model
    bbox = data.model_bbox
    pose = data.pose

    objList = [] 
    try:
        obj1Index = models.index('myModelname')
        obj1 = DbItem(time=header.stamp, name=models[obj1Index], pose=pose[obj1Index], bbox=bbox[object1Index])  
        objList.append(obj1)
        #...
    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)
    rospy.spin()
    if __name__ == '__main__':
        listener()

Okay, I got this. 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():
    listener()
    return OBJL
def callback(data):
    global OBJL
    header = data.header
    models = data.model
    bbox = data.model_bbox
    pose = data.pose

    objList = [] 
    try:
        obj1Index = models.index('myModelname')
        obj1 = DbItem(time=header.stamp, name=models[obj1Index], pose=pose[obj1Index], bbox=bbox[object1Index])  
        objList.append(obj1)
        #...
    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)
    rospy.spin()
    if __name__ == '__main__':
        listener()