1.3.3 ROS Services code illustration: Service server part 2

Course subject(s) Module 1. ROS Essentials

Welcome to part 2 of the tutorial on ROS services. In the last video, we created our own service type, and took a look at an example of a service server. In this part, we will look at the example service client, and actually run the two examples. We will finish by modifying the examples a bit to examine the blocking behaviour of ROS services.

Inspecting our service client code

We will now be looking at the service client example: metres_to_feet_client.py.

Lines 6 through 8 contain all the required imports, and the client definition on #L10. We first wait for the required service to become available. Then, we create a service proxy to be able to call the service (#L16). Then, we actually call the service on #L19, and return the service response from the service call (#L22).

In the main function, we have the ROS node for the service client (#L30). We send a test value to the server via the client (#L38). Once we have a response, we check if the conversion was successful, and report the return value accordingly. We are using the logerr function for error messages (#L42).

Using the service server & client

First, make sure you are in the hrwros_msgs folder. Start roscore in a separate terminal. Then, source our setup files and run our server, and :
$ $HOME/hrwros_ws/devel/setup.bash
$ rosrun hrwros_week1 metres_to_feet_server.py

The service is now available. Time to also start the ROS client in a separate terminal:
$ $HOME/hrwros_ws/devel/setup.bash
$ rosrun hrwros_week1 metres_to_feet_client.py

The terminal will both display the request and response messages.

We learned, that services will block execution. Let’s look at that as well. Since the processing, which is blocking, is happening in the server, let’s go to the server file and add a delaying test loop. In metres_to_feet_server.py, navigate to #L27 and add the following block of code:
for test_idx in range(0,10):
    rospy.sleep(1)

On the client side, let’s write a log message after the response has been received. In metres_to_feet_client.py, navigate to #L21 and add the following block of code:
print('I only got here AFTER the service call was completed!')

First, terminate the server in the terminal (CTRL+C), and restart it. Restart the client the same way. You will now see the client will only print out every 10 seconds, because the processing is not complete yet on the server side.

Creative Commons License
Hello (Real) World with ROS - Robot Operating System by TU Delft OpenCourseWare is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at https://online-learning.tudelft.nl/courses/hello-real-world-with-ros-robot-operating-systems//.
Back to top