Implementing ROS 2 services in C++
In this section, we will implement a ROS 2 service server and client using C++. The service server listens to a service called concat_strings
with a custom service type of master_ros2_interface::srv::ConcatStrings
. If it gets a request from the service client, the server will concatenate both strings it got and reply to the client.
You can find the server and client code in Chapter03
| master_ros2_pkg
| src
| simple_server.cpp
and simple_client.cpp
.
Adding a custom service interface
The first thing to do before writing server and client code is to write a custom service type.
Here is the custom service type we have defined:
string str1
string str2
---
string concatenated_str
We have to save this definition as master_ros2_interface/srv/ConcatStrings.srv
. The first two strings are for the request
field, and the third is for the reply
field. The request and reply are separated by ---
.
Editing CMakeLists.txt
After saving...