9.Custom Service Messages and Usage

9.1 Customized Service Messages

Switch to~/catkin_ ws/src/learning_ Under the server function package directory, create a new folder named srv to store custom service messages.

9.1.1 Define SRV files

Switch to the srv directory and create a new blank srv file, with srv as the suffix to indicate that it is an srv file. Here we use IntPlus.srv as an example to copy the following code into the just created srv file.

Here is an explanation of the composition of the SRV file, which is divided into two parts by the symbol - - - - -. The upper side represents the request and the lower side is the response.

9.1.2 Add feature pack dependencies in package.xml

9.1.3. Add compilation options in CMakeLists.txt

9.1.5 C++Language Implementation

  1. Switch to~/catkin_ ws/src/learning_ Under server/src, create two new cpp files named IntPlus_ Server.cpp and IntPlus_ Client.cpp, copy the following code into it separately,

IntPlus_server.cpp

IntPlus_client.cpp

  1. Modify the CMakeLists.txt file
  1. Core part

The implementation process here is the same as before, with the main difference being the introduction of header files and the use of custom service files:The import header file is

Front learning_ Server is the name of the feature pack, followed by IntPlus. h, which is the header file name generated by the previously created srv fileUsing custom service files is

  1. run a program
  1. Run screenshot

  1. Program Description

Running IntPlus_ After the server, it will prompt to prepare for calculation; Running IntPlus_ After the client, the terminal inputs two integer numbers, followed by IntPlus_ The server accountant calculates the result and returns it to IntPlus_ Client, and then print out the results.

9.1.6 Python Language Implementation

  1. Switch to~/catkin_ ws/src/learning_ Under server/script, create two new py files and name them IntPlus_ Server.py and IntPlus_ Client.py, copy the following code into it separately,

IntPlus_server.py

IntPlus_client.py

  1. Core part

Here is mainly an explanation of how to import a custom service message module and use it:Import

use

  1. Run program

Before running the program, add executable permissions to the py file

run a program

  1. Program operation instructions

What is inconsistent with the C++version here is that the addend is set in the program (12 and 20), so once the service is started, the result can be returned immediately.