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

How to return a particular response in ROS service?

asked 2019-08-09 05:47:31 -0500

spiritninja gravatar image

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 the service name - static_map, and service 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.


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) handle_add_two_ints function which represents the static_map_callback() in case of my code, to return the required response.

Thank you.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-08-09 10:42:10 -0500

ct2034 gravatar image

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 for other options. e. g.

return {'map': GetMap()}
edit flag offensive delete link more

answered 2019-08-09 10:44:59 -0500

rmv24 gravatar image

updated 2019-08-09 10:52:27 -0500

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.

edit flag offensive delete link more


that's right, if it is a class method.

ct2034 gravatar image ct2034  ( 2019-08-09 10:49:52 -0500 )edit

Yep, you're right. My bad, forgot to mention that. I'll edit it.

rmv24 gravatar image rmv24  ( 2019-08-09 10:52:01 -0500 )edit

Question Tools



Asked: 2019-08-09 05:47:31 -0500

Seen: 1,331 times

Last updated: Aug 09 '19