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 realize the service communication between nodes.

1. Create a new function package

Create a new feature package in the src of the previously created workspace directory by typing, in the terminal.

image-20230504104704846

3. Write server-side python file

3.1, Program 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 commands to see.

image-20230504110250103

A part of the data of this type is divided into two parts, the upper part represents the request, the lower part represents the response. then the 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. Similarly, the result of the feedback also needs to specify what the value of sum is.

3.2 Modifying the setup.py file

Terminal input.

Find the location shown below.

image-20230504110847633

Add the following to 'console_scripts': [].

3.3. Compile workspace

After the compilation is complete, refresh the environment variables in the workspace.

3.4. Run the program

Terminal input,

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

image-20230504111607795

/add_two_ints is the service we need to invoke, which is done with the following command, terminal input, the

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, Program 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 the compilation is complete, refresh the environment variables in the workspace.

4.4. Run the program

Terminal input

image-20230504144109703

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