How to return a particular response in ROS service?
I'm trying to return GetMap.srv through a python code.
The basic overlay of the process is as below,
- I have
service = rospy.Service('static_map',GetMap,static_map_callback)
an initialization as above, with theservice name
- static_map, andservice type
- GetMap. - The
static_map_callback
function is being used to process requests and finally return a response.
The callback function is defined as below.
def static_map_callback(req):
response = GetMap()
response = map_params.map_callback()
return response
The contents of GetMap.srv are,
# Get the map as a nav_msgs/OccupancyGrid
---
nav_msgs/OccupancyGrid map
- Using the
callback
, I want to return the Nav_msg/OccupancyGrid
It would be helpful if anyone could guide me on what changes the callback should undergo so that I can return the required response.
Or
How do I use the callback
to return the relative GetMap content.
I did go through the tutorials of services, I'm unable to comprehend what changes are to be made with the
example: s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
handleaddtwoints function which represents the `staticmap_callback()` in case of my code, to return the required response.
Thank you.
Asked by spiritninja on 2019-08-09 05:47:31 UTC
Answers
I assume your service calls are not returning, what you expect. Or what is your problem? - please clarify.
Genereally:
Your code has to return a nav_msgs/OccupancyGrid
. It seems to return, whatever map_params.map_callback()
returns. (which overwrites the GetMap()
, btw) Maybe you want to check that. See https://wiki.ros.org/rospy/Overview/Services#Providing_services for other options. e. g.
return {'map': GetMap()}
Asked by ct2034 on 2019-08-09 10:42:10 UTC
Comments
I'd done something similar recently, using class method, so you could try this:
Try passing the 'self' object as an argument in your callback function, so you could store your values in the self object. Within the function where you declare your service and related operations (pass the self and res object as arguments to the function), you can do something like
res.x = self.p; (this is an arbitrary example, but I hope you get the gist of what I'm trying to say).
Return the res object, and when you call the service using rosservice call, you'll get the desired response.
Asked by rmv24 on 2019-08-09 10:44:59 UTC
Comments
that's right, if it is a class method.
Asked by ct2034 on 2019-08-09 10:49:52 UTC
Yep, you're right. My bad, forgot to mention that. I'll edit it.
Asked by rmv24 on 2019-08-09 10:52:01 UTC
Comments