Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the ROS service communication mode

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what is the ROS service communication mode, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

ROS service communication mode

Services is another way for nodes to communicate with each other. The service allows the node to send a request (request) and get a response (response)

The AddTwoInts.h file is generated from the AddTwoInts.srv file

It will also be automatically generated

AddTwoIntsRequest.h

AddTwoIntsResponse.h

The directory where AddTwoInts.h is located is

\ catkin_ws\ devel

AddTwoInts.srv

Int64 aint64 b---int64 sum

Server.cpp

/ * AddTwoInts Server * / # include "ros/ros.h" # include "learning_communication/AddTwoInts.h" / / service callback function, input parameter req, output parameter resbool add (learning_communication::AddTwoInts::Request & req, learning_communication::AddTwoInts::Response & res) {/ / add the request data in the input parameters, and put the result in the response variable res.sum = req.a + req.b ROS_INFO ("request: x=%ld, y=%ld", (long int) req.a, (long int) req.b); ROS_INFO ("sending back response: [% ld]", (long int) res.sum); return true;} int main (int argc, char * * argv) {/ / ROS node initialization ros::init (argc, argv, "add_two_ints_server"); / / create node handle ros::NodeHandle n / / create a server named add_two_ints, register the callback function add () ros::ServiceServer service = n.advertiseService ("add_two_ints", add); / / Loop wait for the callback function ROS_INFO ("Ready to add two ints."); ros::spin (); return 0;}

Client.cpp

/ * AddTwoInts Client * / # include # include "ros/ros.h" # include "learning_communication/AddTwoInts.h" int main (int argc, char * * argv) {/ / ROS node initializes ros::init (argc, argv, "add_two_ints_client"); / / get two additions if (argc! = 3) {ROS_INFO ("usage: add_two_ints_client X Y") from the terminal command line; return 1 } / / create node handle ros::NodeHandle n; / / create a client, request add_two_int service / / service message type is learning_communication::AddTwoInts ros::ServiceClient client = n.serviceClient ("add_two_ints"); / / create service message of type learning_communication::AddTwoInts learning_communication::AddTwoInts srv; srv.request.a = atoll (argv [1]); srv.request.b = atoll (argv [2]) / issue a service request and wait for the result of the addition operation if (client.call (srv)) {ROS_INFO ("Sum:% ld", (long int) srv.response.sum);} else {ROS_ERROR ("Failed to call service add_two_ints"); return 1;} return 0;}

CMakeLists.txt

Add_executable (server src/server.cpp) target_link_libraries (server ${catkin_LIBRARIES}) add_dependencies (server ${PROJECT_NAME} _ gencpp) add_executable (client src/client.cpp) target_link_libraries (client ${catkin_LIBRARIES}) add_dependencies (client ${PROJECT_NAME} _ gencpp)

Package.xml

Learning_communication 0.0.0 The learning_communication package hcx TODO catkin geometry_msgs roscpp rospy std_msgs geometry_msgs roscpp rospy std_msgs message_generation message_runtime

Main functions of folders

1) config: place the configuration file in the feature pack, created by the user, and the file name can be different. 2) include: place the header files needed in the feature pack. 3) scripts: place Python scripts that can be run directly. 4) src: place C++ code that needs to be compiled. 5) launch: place all startup files in the feature pack. 6) msg: place the message type customized by the feature pack. 7) srv: place the service type customized by the feature pack. 8) action: place the action instructions customized by the feature pack. 9) CMakeLists.txt: rules for compiling feature packs by the compiler.

How to customize service data

Similar to topic messages, service data in ROS can be defined with language-independent interfaces through srv files, which are generally placed in the srv folder under the root of the feature package. The file contains two data fields of request and reply, and the content in the data field is the same as the data type of the topic message, except that the "-" three horizontal lines need to be used to divide the description of the request and reply.

To meet the service requirements in the addition routine, create a srv file learning_communication/srv/AddTwoInts.sint64 a that defines the service data type

Int64 b

The content of int64 sumv file is relatively simple. Two variables an and b of int64 type are defined in the data field of service request to store two additions, and a variable of int64 type sum is defined in the data field of service reply, which is used to store the result of "aquib".

After completing the description of the service data type, like the topic message, you also need to configure the dependency and compilation rules in the package.xml and CMakeLists.txt files of the feature package, and convert the description file into code that can be recognized by the programming language during the compilation process.

Open the package.xml file and add the following dependent configuration

Message_generation message_runtime

Open the CMakeLists.txt file and add the following configuration

Find_package (catkin REQUIRED COMPONENTS geometry_msgs roscpp rospy std_msgs message_generation) add_service_files (FILES AddTwoInts.srv)

The message_generation package can generate not only the corresponding code for the topic message, but also the relevant code according to the type description file of the service message. After the feature pack is compiled successfully, these defined service messages can be invoked directly in the code implementation of the Server node and Client node of the service. Next, we write the code for the Server and Client nodes to complete the service process of adding and summing the two numbers.

Code interpretation

The implementation process of Server node

1. Header file

# include "learning-communication/AddTwoInts.h"

To use a service in ROS, you must include a header file of the service data type. The header file used here is learning_communication/AddTwoInts.h, which is automatically generated based on the description file AddTwoInts.srv of the service data type we created earlier.

2. The main letter ros::ServiceServer service = n.advertiseService ("add_two_ints", add); the part is relatively simple. Initialize the node first, create a node handle, and focus on creating a Server of a service, specifying the name of the service and the callback function after receiving the service data. Then the loop waits for the service request; once there is a service request, Server jumps into the callback function to process it.

3. Callback function part

Bool add (learning_communication::AddTwoInts::Request & req,learning_communication::AddTwoInts::Response & res)

The callback function is the part that really implements the service function, and it is also the focus of the design. The add () function is used to add two variables, and its incoming parameters are the data structure of the request and reply that we declared in the service data type description file.

{res.sum = req.a + req.b;ROS_INFO ("request: x=%ld, y=%ld", (long int) req.a, (long int) req.b); ROS_INFO ("sending back response: [% ld]", (long int) res.sum); ROS_INFO ("sending back response: [% ld]", (long int) res.sum); return true;}

After the addition operation is completed, the summation result is put into the response data and fed back to Client, and the callback function returns true.

The Server implementation process in the service is as follows:

Initialize the ROS node.

Create an instance of Server.

The loop waits for the service request to enter the callback function.

Complete the processing of the service function and feedback the response data in the callback function.

The implementation process of Client node.

1. Create a Client

Ros::ServiceClient client = n.serviceClient ("add_two_ints")

First, you need to create a Client instance of add_two_ints, specifying the service type as learning_communication:AddTwoInts.

2. Publish service request

Learning_communication::AddTwoInts srv;srv.request.a = atoll (argv [1]); srv.request.b = atoll (argv [2])

Then instantiate a variable of the service data type that contains two members: request and response. The two parameters entered by the node at runtime are stored in the variable as two integers that need to be added.

If (client.call (srv))

The service invocation is then made. The call process will block. After a successful call, the call returns true, and you can access the srv.reponse to get the result of the service request. Cannot be used if the call fails and returns false,srv.reponse.

The Client implementation process in the service is as follows:

Initialize the ROS node.

Create an instance of Client.

Publish service request data.

Wait for the result of the reply after Server processing.

Compile the feature pack

Edit the CMakeLists.txt file and add the following compilation rules:

Add_executable (server src/server.cpp) target_link_libraries (server ${catkin_LIBRARIES}) add_dependencies (server ${PROJECT_NAME} _ gencpp) add_executable (client src/client.cpp) target_link_libraries (client ${catkin_LIBRARIES}) add_dependencies (client ${PROJECT_NAME} _ gencpp)

You can now compile the feature pack using the catkin_make command.

Run Server and Client

Run the compiled Server and Client nodes.

1. Start roscore

Before running the node, you first need to make sure that ROS Master has started successfully:

Roscore2. Run the Server node to open the terminal and run the Server node using the following command: roscore

2. Run the Server node to open the terminal and use the following command to run

Server node: rosrun learning_communication server

3. Run the Client node

Open a new terminal and run the Client node, and you need to enter two addition values for the addition operation:

$rosrun learning_communication client 3 5

After Client starts, the service request is issued and the feedback result is received successfully.

After receiving the service call, Server completes the addition solution and feeds back the result to Client.

Wiki

Rosservice list outputs information about available services rosservice call calls service rosservice type with parameters output service type rosservice find looks for service find services by service typerosservice uri output service ROSRPC uri based on type

What is the above content? what is the ROS service communication mode? have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report