1.3.2 ROS Services code illustration: Service server part 1

Course subject(s) Module 1. ROS Essentials

Welcome to a two part section about creating a ROS service client and server. We will start with creating our own service type, and take a look at an example service server created in Python.

Setting up the service definition files

Let’s begin by sourcing our setup files, and navigate to our workspace msgs folder:
$ source $HOME/hrwros_ws/devel/setup.bash
$ roscd hrwros_msgs/

We can see the msg/ folder, but we just learned services are defined in the srv/ folder. So let’s create that, and create our service file:
$ mkdir srv
$ cd srv
$ touch ConvertMetresToFeet.srv

Now our service file is created, we can start editing it. Remember, we first want the request message type, then a demarcation of three dashes, and then a response message type:
float64 distance_metres     # Request message: Distance in (m) to be converted to (ft)
---                         # Demarcation
float64 distance_feet       # Response message: Distance in (ft) after conversion
bool success                # Response message: Success or failure of conversion

Now, just like we did for messages, we add our new service to the CMakeLists.txt file in the hrwros_msgs/ folder. Note that services have their own section, after messages. Add the name of our new service definition file after the FILES entry. Don’t forget the .srv file extension.

Finish by building our service by running the catkin b command. We can check if it worked correctly by using the rossrv command:
$ rossrv show hrwros_msgs/ConvertMetresToFeet
$ rossrv show hrwros_msgs/ConvertMetresToFeet -r

The terminal should show the request message field, and the response message field.

Inspecting our service server code

Please note that there is an error in the code illustration of service server in the video. The conversion factor is inverted. The correct value is _CONVERSION_FACTOR_METRES_TO_FEET = 3.28.

Now we’ve created a new service, we can use it in our code. Let’s go to the week 1 script folder, where we already have two template implementations. Let’s begin by inspecting the server: metres_to_feet_server.py.

We are importing the newly created service definitions from hrwros_msgs.srv. Note that we not only have to import the service definition, but also explicitly the request and response message definitions (#L6).

The service request is processed in the service callback function (#L13).

  • We first create an object for the response message type, and in the example, do a check in the distance input to check if it is a positive real number.
  • If the sanity check fails, we return a default error value in the response.
  • Otherwise, we do the conversion and return both the converted value and a flag that indicates the conversion was successful.
  • In either case, we return the response.

Then, we have the next function for the administrative settings of the service server node (#L38).

  • Here, we create a ROS node for our service (#L32)
  • We create the actual service by giving it a name, a type, and a callback function for processing a service request. (#L35)
  • Finally, in the main function (#L41) we start the service server node.
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