Return data from a callback function for use in a different function

asked 2016-11-18 16:33:30 -0500

mscott gravatar image

updated 2016-11-18 16:42:46 -0500

I am trying to use ROS to control many different robots in a swarm simulation. I need to have access to all of the robots position and velocity. I am using the MORSE simulator, which publishes the position and velocity of each of the robots. What I need my function to do (Python) is to take all of those pieces of data and organizes it so that I can use it for my controller. What I have so far:

##Create robot class##
class robot():

    def __init__(self):
        self.robot_number = robot_number
        self.x_data = 0
        self.y_data = 0
        self.z = 0
        self.x_vel = 0
        self.y_vel = 0
        self.position = []
        print('init1')
    def get_pose(self, pose_topic):
        self.x_pos = rospy.Subscriber(pose_topic, PoseStamped,self.pose_callback)
        return self.x_pos
    def pose_callback(self,data):
        self.x_data = data.pose.position.x
        return self.x_data

class move_feature():
    def __init__(self):
        self.x_position = 0
    def pose(self,pose_topic):
        main_robot = robot()
        self.x_position = main_robot.get_pose(pose_topic)  
        print(self.x_position)


def main(args):
    '''Initializes and cleanup ros node'''
    robot_dictionary = {}

    for i in range(0,robot_number+1):
        robot_name = 'robot%s' % (i)
        robot_dictionary[robot_name] = move_feature()
        pose_topic = '/robot/pose%s' % (i+1)
        vel_topic = '/velocity%s' % (i+1)
        robot_dictionary[robot_name].pose(pose_topic)
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down ROS Image feature detector module")


if __name__ == '__main__':
    rospy.init_node('robot', anonymous=True)
    main(sys.argv)

So I created a robot class which has the function get_pose. I would like get_pose to return the position (right now just the x position) from the topic so that it can be used within the move_feature class. So, in the move_feature, I should just be able to call pose with the position topic and have it return the current position.

I will eventually need it to expand to 100 robots, so organization is very important. Right now, the function does not return the x_value at all... it just returns information about the subscriber. Is this the right way to try to solve this problem?

Thanks in advance!

edit retag flag offensive close merge delete

Comments

Callback functions do not return data. Instead just set self.x_pos in the callback function.

JoshMarino gravatar image JoshMarino  ( 2016-11-18 21:24:28 -0500 )edit

ROS pub/sub is a dataflow system, so you should model dataflows. Your code seems to be based on a synchonous approach (ie: RPC). ROS supports that as well, but then you'd use services. Note that components != objects+RPC.

gvdhoorn gravatar image gvdhoorn  ( 2016-11-19 04:01:15 -0500 )edit

Thanks for the comments.
JoshMarino: Sure, I generally just set self.x_pos and then use it. The problem in this case is with organization. Since I need to many robots, I need to have them organized properly, not just specify self.x_pos. gvdhoorn: I'm not sure I understand. Could you elaborate?

mscott gravatar image mscott  ( 2016-11-19 18:43:22 -0500 )edit

You are wanting to get the position from a subscriber, but only whenever you want it (synchronous). Pub/sub in ROS do not work like this, they are continuously sending data at a specified frequency. If you want it when called from a function, a ROS service would be better implemented.

JoshMarino gravatar image JoshMarino  ( 2016-11-20 11:15:58 -0500 )edit

Okay I think that makes sense, thank you!

mscott gravatar image mscott  ( 2016-11-21 11:41:07 -0500 )edit