ROS2 service communications

Service communication is one of the communication methods of ROS2 nodes, which is different from topic communication in that service communication has a feedback mechanism, which will feedback the results of the service. Therefore, service communication is mostly used in programs that need feedback results, such as the routine of a baby turtle, calling the Spawn service to generate a turtle, and after the service is completed (after the turtle is generated), the turtle's name will be printed. Next, it is explained how to use the Python language to achieve service communication between nodes.

1、Create a new feature pack

src new function package in the previously created workspace directory, terminal input,

image-20230504104704846

2、Write server-side python files

2.1、Program source code

Create a new file, named server_demo.py,

Copy the following sections into the file,

Focus on the service callback function, Add2Ints_callback, in addition to self, there are also request and response, request is the parameter required by the service, and response is the feedback result of the service. request.a and request.b are the content of the request part, response.sum is the content of the response part, here first look at the AddTwoInts type of data is how, you can use the following command to view,

image-20230504110250103

--- The section divides this type of data into two parts, the upper side represents the request and the lower side represents the response. Then the variables in each domain, such as int64 a and int64 b, all the parameters passed in need to specify what the value of a and b is. Similarly, the result of the feedback also needs to specify what the value of sum is.

2.2、Modify the setup.py file

terminal input,

Locate the location as shown in the image below,

image-20230504110847633

Add the following to 'console_scripts': [].

2.3、Compile the workspace

After the compilation is completed, refresh the environment variables of the workspace.

2.4、Run the program

terminal input,

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

image-20230504111607795

/add_two_ints is the service we need to call, call it through the following command, terminal input,

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

image-20230504111848803

As can be seen from the above figure, after calling the service, the result of the feedback is 5, and the terminal running the server also prints the value of the feedback.

3、Write a client python file

3.1、Program source code

Create a new file, named client_demo.py,

Copy the following into it,

3.2、Modify the setup.py file

terminal input,

Locate the location as shown in the image below,

image-20230504143449133

Add the following to 'console_scripts': [],

image-20230504143713966

3.3、Compile the workspace

After the compilation is completed, refresh the environment variables of the workspace.

3.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 summes, the result is 100, and the result is printed at the two terminals.