Calling a service with python script

I am currently in Map creation - Noetic and I am having trouble trying to solve exercise 2.7.

I am confused as to what I should write to print out the map for me.

I have checked the service info of static_map and it gives me this output:

Node: /map_server
URI: rosrpc://2_xterm:54709
Type: nav_msgs/GetMap
Args:

Then, I tried to check the srv file of GetMap and it gave me the following:

# Get the map as a nav_msgs/OccupancyGrid
---
nav_msgs/OccupancyGrid map

I notice that I just need to access the map from the GetMap file but I have no idea how I could do so, and my python script is as follow:

#! /usr/bin/env python

import rospy
from nav_msgs.srv import GetMap, GetMapResponse

def my_callback(request):

    my_response = request.OccupancyGrid.map

    return my_response

rospy.init_node('get_map_service')
map_service = rospy.Service('/static_map', GetMap, my_callback)
rospy.spin()

What am I missing if I may ask?

Hi @WhenOwlsHoot ,

So the exercise 2.7 asks for a service client, which has a syntax that goes like this
get_map_service = rospy.ServiceProxy('/static_map',GetMap)

What you have tried to implement seems to use rospy.Service('/static_map', GetMap, my_callback)
which is used to create a new service server (not service client).

I guess this should help you think in the right direction and finish the exercise.

Happy Learning…!!

Ahh it works now

get_map_service = rospy.ServiceProxy('/static_map', GetMap)

get_map_object = GetMapRequest()

result = get_map_service(get_map_object)

So, apparently GetMap.srv has no request, hence why this line of codes work?

This is the raw message definition of the GetMap.srv

# Get the map as a nav_msgs/OccupancyGrid
---
nav_msgs/OccupancyGrid map

Like you pointed, the request part is empty, so thats also the reason why when your calling the service via the terminal the command line syntax goes like this rosservice call /static_map "{}"
That’s because your sending in an empty request.

Hope this clears out your confusion.

1 Like