9.ROS service client

Service service is also a common communication method between ros nodes. Different from the topic, the service has a server and a client. The client requests the server to provide services. After the server completes the service, it needs to return the service results. Also called a response. In this lesson, we will explain how to write a client node program.

9.1 Create a client

The general creation steps are as follows:

9.2 Create function package

In order to distinguish it from the learn_topic function package, we re-create a function package learn_service and enter it in the terminal.

Then compile,

9.3 C++ version

9.3.1 Writing source code

In the src folder of the function package learn_service, create a C++ file (the file suffix is .cpp), name it a_new_turtle.cpp, and paste the following content into a_new_turtle.cpp,

9.3.2 Modify CMakeList.txt file

Configure in CMakelist.txt, under the build area, add the following content,

add_executable shows that the generated executable program file is a_new_turtle, and the compiled source code is a_new_turtle.cpp in the src directory.

target_link_libraries specifies the libraries that need to be linked when compiling and generating an executable file.

9.3.3 Compile

Terminal input,

image-20231023173644143

After the compilation is passed, you need to re-source the current environment variables to find or update the program. Enter in the terminal.

9.3.4 Running the program

Open roscore,

Run the little turtle node program,

Run the client node program to generate a small turtle at the specified location.

image-20231023174232607

After starting the little turtle node, and then running the a_new_turtle program, you will find that another little turtle will appear on the screen. This is because the little turtle node provides service/spawn, which corresponds to ros::ServiceClient new_turtle in the code. = node.serviceClient<turtlesim::Spawn>("/spawn");//Creating this service will generate another little turtle turtle2. To view the services provided by the little turtle, you can view it through the rosservice list command, as shown in the figure below.

image-20231023174328925

You can view the parameters required by this service through rosservice info /spawn, as shown in the figure below.

image-20231023174404010

It can be seen that 4 parameters are required: x, y, theta, name. These four parameters are initialized in a_new_turtle.cpp.

9.3.5 Program flow chart

image-20231023174621881

9.4 Python version

9.4.1 Writing source code

Create a new scripts folder under the function package learn_service, then create a new python file (file suffix .py) in this scripts folder, name it a_new_turtle.py, copy and paste the following program code into the a_new_turtle.py file,

The python program does not need to be compiled, but it needs to add executable permissions and enter it in the terminal.

9.4.2 Run

Open roscore,

Run the little turtle node,

Run the publisher node program and continue to send speed to the little turtle.

image-20231023182843856

Similarly, after running, a little turtle will appear, and the terminal will print the returned content.

9.4.3 Program flow chart

image-20231023183128531