7.Client

In ROS communication, in addition to topic communication, there is also a type of service communication. Services include both client and server, where the client requests the service and the server provides the service. This section focuses on the client and explains how C++and Python can implement the client.

7.1 Preparation work

7.1.1 Establishing a Function Package

  1. Switch to~/catkin_ In the ws/src directory,
  1. Switch to~/catkin_ Under the ws directory,

7.2. C++Language Implementation

7.2.1 Implementation steps

  1. Initialize ROS node
  2. Create handle
  3. Create a Client instance
  4. Initialize and publish service request data
  5. Wait for the response result after the server processes it

7.2.2. Switch to~/catkin_ ws/src/learning_ Create a new. cpp file under the server/src directory and name it a_ new_ Turtle, paste the following code inside

a_new_turtle.cpp

  1. Process Flow Chart

  1. Configure in CMakelist.txt, under the build area, add the following content
  1. Compiling code under workspace directory
  1. run a program
  1. Running effect screenshot

  1. Program Description

After starting the node of Little Turtle, run a_ new_ The Turtle program will find that there will be another small turtle appearing in the screen, because the turtle's node provides a service/spa wn, which will generate another small turtle Turtle2. To view the services provided by the turtle, you can use the rossservice list command, as shown in the following figure

You can view the parameters required for this service through rossservice info/spa, as shown in the following figure

It can be seen that there are four parameters required: x, y, theta, and name, which are in a_ new_ There is initialization in turtle.cpp

7.3. Python Language Implementation

7.3.1. Switch to~/catkin_ ws/src/learning_ Under the server directory, create a new script folder, cut it in, and create a new py file named a_ new_ Turtle, paste the following code inside

a_new_turtle.py

  1. Program flowchart

  1. run a program
  1. The program operation effect and program description are consistent with the implementation effect of C++. Here, we mainly discuss how Python provides the parameters required for the service,

The corresponding parameters are x, y, theta, and name.