2.2.2 Service Communication Custom srv
Requirement:
In service communication, the client submits two integers to the server, the server sums them and responds with the result to the client. Please create the data carrier for server and client communication.
Process:
The available data types within an srv file are the same as those in a msg file, and the process for defining an srv is similar to the custom msg implementation process:
- Create the srv file according to the fixed format
- Edit the configuration files
- Compile to generate intermediate files
1. Define srv file
In service communication, the data is divided into two parts: the request and the response. In the srv file, the request and response are separated by ---. The specific implementation is as follows:
Create a new srv directory under the functional package, add an xxx.srv file with the content:
# The two numbers sent by the client when making a request
int32 num1
int32 num2
---
# The data sent by the server in response
int32 sum2. Edit configuration files
Add build dependencies and execution dependencies in package.xml
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
<!--
exce_depend was previously corresponding to run_depend, which is now invalid
-->Edit srv-related configurations in CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
# Need to include message_generation, must have std_msgsadd_service_files(
FILES
AddInts.srv
)generate_messages(
DEPENDENCIES
std_msgs
)Note: The official ROS documentation does not configure message_runtime in catkin_package, but testing shows that configuring it also works.
3. Compile
View the compiled intermediate files:
Intermediate files needed for Python calls (.../workspace/devel/lib/python3/dist-packages/package_name/srv)
.D3o22bXk.png)
Subsequent calls to related srv will be made from these intermediate files.