ROS entry (xiii) write simple server and client (C ++)

Write Service Node

Here, we will create a simple service node ( "add_two_ints_server"), the node will receive two plastic figures, and returns their sum.

Beginner_tutorials package into the directory you previously created in catkin workspace tutorial here:

cd ~/catkin_ws/src/beginner_tutorials

Make sure you have creating the tutorial steps AddTwoInts.srv srv created this tutorial needed (be sure to select the corresponding compiler system "catkin" and "rosbuild").

Code

Creating src / add_two_ints_server.cpp file beginner_tutorials package, and copy and paste the following code:

 1 #include "ros/ros.h"
   2 #include "beginner_tutorials/AddTwoInts.h"
   3 
   4 bool add(beginner_tutorials::AddTwoInts::Request  &req,
   5          beginner_tutorials::AddTwoInts::Response &res)
   6 {
   7   res.sum = req.a + req.b;
   8   ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
   9   ROS_INFO("sending back response: [%ld]", (long int)res.sum);
  10   return true;
  11 }
  12 
  13 int main(int argc, char **argv)
  14 {
  15   ros::init(argc, argv, "add_two_ints_server");
  16   ros::NodeHandle n;
  17 
  18   ros::ServiceServer service = n.advertiseService("add_two_ints", add);
  19   ROS_INFO("Ready to add two ints.");
  20   ros::spin();
  21 
  22   return 0;
  23 }

Code explanation

Now, let's step by step analysis of the code.

   1 #include "ros/ros.h"
   2 #include "beginner_tutorials/AddTwoInts.h"
   3 

beginner_tutorials / AddTwoInts.h automatically according to the header file srv file we created earlier to be generated by the compiler srv file system.

   4 bool add(beginner_tutorials::AddTwoInts::Request  &req,
   5          beginner_tutorials::AddTwoInts::Response &res)

This function int values ​​are summed to provide two services, int value acquisition request from the inside, and returns the data loaded into the Response, these data types are defined in the interior srv file, the function returns a boolean value.

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

Now, two int values ​​have been added and stored in the response. Then some of the information about the request and the response is recorded. Finally, service returns a true value after the completion of calculation.

  18   ros::ServiceServer service = n.advertiseService("add_two_ints", add);

Here, service has been established, and publish it in the ROS.

Client write node

Code

Creating src / add_two_ints_client.cpp file beginner_tutorials package, and copy and paste the following code:

   1 #include "ros/ros.h"
   2 #include "beginner_tutorials/AddTwoInts.h"
   3 #include <cstdlib>
   4 
   5 int main(int argc, char **argv)
   6 {
   7   ros::init(argc, argv, "add_two_ints_client");
   8   if (argc != 3)
   9   {
  10     ROS_INFO("usage: add_two_ints_client X Y");
  11     return 1;
  12   }
  13 
  14   ros::NodeHandle n;
  15   ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
  16   beginner_tutorials::AddTwoInts srv;
  17   srv.request.a = atoll(argv[1]);
  18   srv.request.b = atoll(argv[2]);
  19   if (client.call(srv))
  20   {
  21     ROS_INFO("Sum: %ld", (long int)srv.response.sum);
  22   }
  23   else
  24   {
  25     ROS_ERROR("Failed to call service add_two_ints");
  26     return 1;
  27   }
  28 
  29   return 0;
  30 }

Code explanation

Now, let's step by step analysis of the code.

  15   ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

This code creates a client is add_two_ints service. ros :: ServiceClient objects can be used to invoke the service.

  16   beginner_tutorials::AddTwoInts srv;
  17   srv.request.a = atoll(argv[1]);
  18   srv.request.b = atoll(argv[2]);

Here, we instantiate a compiler automatically generated by the ROS system service class, and its request to the members of the assignment. A service class includes two members of the request and response. It also includes two class definitions Request and Response.

19   if (client.call(srv))

This code is calling service. Since the service call is modal process (a process called when occupancy prevent the execution of other code), once the call is completed, the results will return the call. If the service call is successful, call () function returns true, srv.response inside the values ​​are legal values. If the call fails, call () function returns false, srv.response inside the value would be illegal.

Compile node

Edit it again beginner_tutorials inside CMakeLists.txt, files located in ~ / catkin_ws / src / beginner_tutorials / CMakeLists.txt, and add the following code at the end of the file:

https://raw.github.com/ros/catkin_tutorials/master/create_package_srvclient/catkin_ws/src/beginner_tutorials/CMakeLists.txt

  27 add_executable(add_two_ints_server src/add_two_ints_server.cpp)
  28 target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})
  29 add_dependencies(add_two_ints_server beginner_tutorials_gencpp)
  30 
  31 add_executable(add_two_ints_client src/add_two_ints_client.cpp)
  32 target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})
  33 add_dependencies(add_two_ints_client beginner_tutorials_gencpp)

This code will generate two executable "add_two_ints_server" and "add_two_ints_client", the two executables are placed in the default directory of your package devel space, the default is ~ / catkin_ws / devel / lib / share / . You can call directly executable program, or use rosrun command to call them. They are not installed in the / bin directory, because when you install this package on your system, this will contaminate the PATH variable. If you ever wanted to install an executable program in your PATH variable inside, you need to set up install target, please refer to: catkin / CMakeLists.txt

About CMakeLists.txt file a more detailed description, please refer to: catkin / CMakeLists.txt

Now run catkin_make command:

# In your catkin workspace
cd ~/catkin_ws
catkin_make

If your compilation process fails for some reason:

To ensure that the previous creating the AddTwoInts.srv tutorial steps you've completed in accordance with the operation.

Now that you have learned how to write a simple server and client Service Client, start a simple test and Client Service, right.

Published 34 original articles · won praise 2 · Views 2337

Guess you like

Origin blog.csdn.net/weixin_44088559/article/details/104990133