Ask Your Question
0

rosservice geometry_msgs/Posearray example

asked 2019-04-12 07:38:43 -0500

ArunPrasanth gravatar image

So, I'm getting an array of poses from the robot(from a python script say script1.py). And I want this array of poses to be used in another python script(script2.py). I would like to use rosservice to do so. Whenever I request the service from script2.py it should give me the array of poses from script1.py. Is this possible? How it works? I have read the tutorials http://wiki.ros.org/ROS/Tutorials/Cre... and http://wiki.ros.org/ROS/Tutorials/Wri... , but I'm not sure to proceed for this particular message type. I cannot find any examples to look into. Also when I save the array of poses as a PoseArray in script1.py, Should I also save the header too? I'm bit confused here. Can someone give an hint here? Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-04-12 09:12:47 -0500

pavel92 gravatar image

updated 2019-04-12 09:15:25 -0500

This is definitely possible. You would need to create your custom service which will contain the pose array msg as a response. A service description file consists of a request and a response msg type. Since in your case you just need to receive the array on a service call you will not need a request (it will act as trigger srv) and the response should contain the pose array. For example this can be your bare bone service description:

---
geometry_msgs/PoseArray arr

You can name this srv file as getPoseArray.srv (check again the service creation tutorial for this).

The geometry_msgs/PoseArray msg contains a header and array of poses. When you create/save the array of poses in script1.py (I guess you are using a global variable for this, for example my_array) and you can also populate the header (especially the frame_id since you are dealing with poses). You will need to advertise the service where in the service callback you will just return the pose array (check again the service/client tutorials)). In python it will look something like this:

#!/usr/bin/env python

import rospy
from your_package.srv import getPoseArray
from geometry_msgs.msg import PoseArray

def handle_get_pose_array(req):
    return getPoseArrayResponse(my_array)

def get_pose_array_server():
    rospy.init_node('get_pose_array_server')
    s =rospy.Service('get_pose_array', getPoseArray, handle_get_pose_array)

   my_array = PoseArray()
   my_array.header.frame_id = some_frame
   my_array.poses = ... # assign/populate here

    rospy.spin()

if __name__ == "__main__":
    get_pose_array_server()

On the other side in script2.py you need to create the client and call the service:

#!/usr/bin/env python

import rospy
from your_package.srv import getPoseArray
from geometry_msgs.msg import PoseArray

def get_pose_array_client():
    rospy.wait_for_service('get_pose_array')
    try:
        client_handle = rospy.ServiceProxy('get_pose_array', getPoseArray)
        resp1 = client_handle()
        return resp1.arr
    except rospy.ServiceException, e:
        print "Service call failed: %s"%e

if __name__ == "__main__":
    get_pose_array_client()
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-04-12 07:38:43 -0500

Seen: 263 times

Last updated: Apr 12