4. ROS2 Service Communication

Service communication is one of the ROS2 node communication methods, it is different from topic communication, service communication has a feedback mechanism, it will feedback the result of the service. Therefore, service communication is mostly used in programs that need feedback results, such as the small turtle routine, which calls the Spawn service to generate a turtle, and prints out the turtle's name after the service is completed (after the turtle is generated). The next step is to explain how to use the Python language to implement the service communication between nodes.

1. Create a new function package

Create a new function package in the src of the previously created workspace directory, and enter, in the terminal.

image-20230504104704846

3. Writing server-side python files

3.1 Programme source code

Create a new file named server_demo.py.

Copy the following section into the file.

Focus on the service callback function, Add2Ints_callback, here you need to pass in the parameters in addition to self, there is the request and response, request is the parameters needed by the service, response is the service feedback results. request.a and request.b is the request part of the content, response.sum is the response part of the content, here first look at the AddTwoInts this type of data is how, you can use the following command to see. content, response.sum is the response part of the content, here first look at the AddTwoInts this type of data is how, you can use the following command to view.

image-20230504110250103

-- part of the data of this type is divided into two parts, the upper part represents the request, the lower part represents the response. and then their respective fields and their respective variables, such as int64 a, int64 b, all in the then pass the parameter is, you need to specify the value of a, b is how much is it. Similarly, the result of the feedback also needs to specify the value of sum.

3.2 Modify the setup.py file.

Terminal input.

Find the location shown below.

image-20230504110847633

Add the following to 'console_scripts': [].

3.3. Compilation workspace

After compiling, refresh the workspace environment variables.

3.4. Run the programme

Terminal input.

After running, there is no feedback data because the service is not called, you can call the service from the command line, firstly query what services are currently available, terminal input, the

image-20230504111607795

/add_two_ints is the service we need to invoke, which is done by the following command, entered in the terminal.

Here we assign the value of a to 1 and the value of b to 4, that is, we call the service to compute the sum of 1 and 4.

image-20230504111848803

As you can see from the above figure, after calling the service, the result returned is 5, and the terminal running the server also prints the value returned.

4. Write the client-side python file

4.1 Programme source code

Create a new file named client_demo.py.

Copy the following into the inside.

4.2 Modifying the setup.py file

Terminal input.

Find the location shown below.

image-20230504143449133

Add the following to 'console_scripts': [].

image-20230504143713966

4.3. Compilation workspace

After compiling, refresh the workspace environment variables.

4.4. Run the programme

Terminal input.

image-20230504144109703

First run the server, then run the client, the client provides a=10, b=90, the server does the summing and gets the result as 100, the result is printed in both terminals.