11.ROS2 action communication server

 

1. Introduction to action communication

Action communication is a communication model with continuous feedback. Between the communicating parties, the client sends request data to the server, and the server responds to the client.However, during the process from the server receiving the request to generating the final response, it will send continuous feedback information to the client.

Action Communication client/server model is as follows:

image8

 

2. Case introduction

The action client submits an integer data N, the action server receives the request data and accumulates all integers between 1 and N, and returns the final result to the action client.And each time it is accumulated, the current operation progress is calculated and fed back to the action client.

This case is located in the factory docker container. The source code location is:

 

3. Create a new function package

3.1. Create action communication interface function package

  1. Action communication requires creating an action communication interface first, and then creating an action communication interface.
  1. Then create an action folder under the pkg_interfaces function package, and create a new [Progress.action] file in the action folder. The file content is as follows:

image-20231030154108201

  1. Some dependency packages need to be added to package.xml. The specific contents are as follows:
  1. Add the following configuration to CMakeLists.txt:

image-20231030154828498

  1. Compile function package:
  1. After the compilation is completed, the C++ and Python files corresponding to the Progress.action file will be generated in the install directory under the workspace. We can also enter the workspace under the terminal and check the file definition and whether the compilation is normal through the following commands:

Under normal circumstances, the terminal will output content consistent with the Progress.action file

 

3.2. Create action communication function package

After executing the above command, the pkg_action function package will be created, and an action_server_demo node will be created, and the relevant configuration files have been configured.

image-20231030155311734

 

4. Server-side implementation

Next edit [action_server_demo.py] to implement the server-side functions and add the following code:

 

5. Edit configuration file

image-20231030164238858

 

6. Compile workspace

 

7. Run program

Open a terminal:

image-20231030164959631

Enter in another terminal:

image-20231030165029444

/get_sum is the action we need to call. Call it through the following command and enter it in the terminal:

Here we find the sum from 1 to 10:

image-20231030165504071

The top of the picture above is the server, and the bottom is the client. You can see that during the calculation of the sum of 1 to 10, the server has been feeding back the progress of the calculation. Finally, it shows that the task is completed, and the client has also received feedback that the sum is 55.