Return data from a callback function for use in a different function
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 getpose. I would like getpose to return the position (right now just the x position) from the topic so that it can be used within the movefeature class. So, in the movefeature, 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!
Asked by mscott on 2016-11-18 17:33:30 UTC
Comments
Callback functions do not return data. Instead just set self.x_pos in the callback function.
Asked by JoshMarino on 2016-11-18 22:24:28 UTC
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.
Asked by gvdhoorn on 2016-11-19 05:01:15 UTC
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?
Asked by mscott on 2016-11-19 19:43:22 UTC
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.
Asked by JoshMarino on 2016-11-20 12:15:58 UTC
Okay I think that makes sense, thank you!
Asked by mscott on 2016-11-21 12:41:07 UTC