ROS Series: Service Communication

Hint: Study Records


Learning Service Communication Mechanisms

1. Service Communication

Service communication: based on the request-response mode, it is used for occasional data transmission scenarios that require real-time performance and certain logic processing requirements;

2. Process steps

0. server registration
1. client registration
2. rosmaster realizes information matching
3. The client sends a request request
4. The server sends a response respond

1. Define the srv file

The code is as follows (example):
Use --- to separate the request and the response;

2. Edit the configuration file

The secret: add two lines to the xml file, add two lines and two paragraphs to the txt file

find_package(catkin REQUIRED COMPONENTS




#  INCLUDE_DIRS include
#  LIBRARIES plumbing_server_client
 CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
#  DEPENDS system_lib

3. The intermediate file path generated after compilation can be added into the relevant configuration file to configure vscode

,"xxx/xxx Workspace/devel/include/***"

4. Programming (c++)

1. Server

#include "ros/ros.h"
#include "plumbing_server_client/AddInts.h"

 * @brief Server implementation
 * Parse the data submitted by the client, and calculate the response
 * 1,include header files
 * 2,Initialize the ros node
 * 3,Create a node handle
 * 4,Create service object
 * 5,Process the request and generate a response
 * 6,spin()Must be added, the loop is spinOnce
 * @param argc
 * @param argv
 * @return int
bool doNums(plumbing_server_client::AddInts::Request &request,
            plumbing_server_client::AddInts::Response &response){
    //1. Process the request
    int num1 = request.num1;
    int num2 = request.num2;
    ROS_INFO("The received request data is: num1=%d,num2=%d",num1,num2);
    // 2. Organizational Response
    int sum=num1+num2;
    response.sum = sum;
    ROS_INFO("Summation result sum=%d",sum);

    return true;
int main(int argc, char  *argv[])
    ROS_INFO("Server has started");
    //  * 2. Initialize the ros node
    ros::init(argc, argv, "HeiShui"); //Guaranteed to be unique
    //  * 3. Create a node handle
    ros::NodeHandle nh;

    //  * 4. Create a service object The service has a topic name addInts
    ros::ServiceServer server = nh.advertiseService("addInts",doNums);
    //  * 5. Process the request and generate a response
    //  * 6, spin() must be added, the loop is spinOnce
    return 0;

2. Client

insert code snippet here

3. Configure CMakeList.txt
4. Execute


An error occurred: Question 1. The topic name is added after the rosservice call keyword, but there is no tab filling. The barrage says that there is an empty space and then 2 tab fillings do not appear:

Solution: First try to use the slash method, and press the tab twice to find the current topic, but it is not successful; when calling the custom message or service information, an error ERROR: Unable to load type [xxx]: possible The reason is that the working environment of the currently used terminal has not been updated, so the customized message cannot be found. The working environment of the update terminal is executed, and the relevant experience found on the Internet, but i have already configured the working environment, so it is not such a problem. Reflecting on the creation of AddInts, it was written as Addints at the beginning, and after compiling, i replaced it with the same uppercase as the teacher. , so the addints-related header files appear in the /devel directory, but the AddInts-related header files are used later, so I decided to delete the entire function package and try again:

After rosservice call /AddInts, the tab can be completed, the function is normal, and the problem is successfully solved!
Question 2:
An error occurred: [WARN] [1664201474.083411553]: Shutdown request received.
[ WARN] [1664201474.084911617]: Reason given for shutdown: [[/HeiShui] Reason: new node registered with same name]
Solution: Careless, the reason is that add_executable(demo02_client src/demo01_server.cpp) in CMakeLists.txt repeatedly points to the same cpp file, so there is a conflict at runtime; the problem is solved after modification.
Question 3:

Solution: The reason is that in the service communication, it belongs to asynchronous communication, so during communication, the server should give priority to the client to run.
The client-side suspend options mentioned later are also mentioned here:

Issue 5: Error type error: invalid conversion from 'char*' to 'plumbing_server_client::AddIntsRequest_<std::allocator >::_num1_type' {aka 'int'} [-fpermissive]
35 | ai.request.num1 = argv[1];

Solution: Learn to use argc and argv for dynamic parameter transfer, but do not convert argv[] to a string:

Tags: Linux Ubuntu

Posted by Lytheum on Mon, 26 Sep 2022 23:13:41 +0530