Writing a simple Service and Client (c + +)

Source: Internet
Author: User

Here we'll create the service ("Add_two_ints_server") node which would receive both ints and return the sum.

Change directories to your Beginner_tutorials package created in your Catkin workspace previous tutorials:

ROSCD beginner_tutorials
Writing a Service Node

Sure you have followed the directions in the previous tutorial for creating the service needed in this Tutoria L, creating the addtwoints.srv (be sure to choose the right version of the build tool you ' re using at the top of the wiki page in The link).

#include"Ros/ros.h"#include"Beginner_tutorials/addtwoints.h"BOOLAdd (Beginner_tutorials::addtwoints::request &req, Beginner_tutorials::addtwoints::response&Res) {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); Returntrue;}intMainintargcChar**argv) {Ros::init (argc, argv,"Add_two_ints_server");  Ros::nodehandle N; Ros::serviceserver Service= N.advertiseservice ("add_two_ints", add); Ros_info ("Ready to Add the ints.");  Ros::spin (); Return0;}
Code Explained
" Ros/ros.h "  "beginner_tutorials/addtwoints.h"

beginner_tutorials/addtwoints.h is the header of file generated from the SRV file, that we created earlier.

bool Add (beginner_tutorials::addtwoints::request  &req,         &res)

This function provides the service for adding the INTs, it takes with the request and response type defined in the SRV file and returns a Boolean.

 {Res.   sum  = req.a + req.b; Ros_info (  " request:x=%ld, Y=%ld  Span style= "COLOR: #800000" > ", (long  int ) req.a, (long  int  ) req.b); Ros_info (  " sending back response: [%LD]   ", (long    int ) Res.sum  ); return  true  ;}  

Here the ints is added and stored in the response. Then some information about the request and response is logged. Finally The service returns True when it was complete.

  Ros::serviceserver service = N.advertiseservice ("add_two_ints", add);

Here the service was created and advertised over ROS.

Writing the Client Node
#include"Ros/ros.h"#include"Beginner_tutorials/addtwoints.h"#include<cstdlib>intMainintargcChar**argv) {Ros::init (argc, argv,"add_two_ints_client"); if(ARGC! =3) {Ros_info ("usage:add_two_ints_client X Y"); Return1;  } ros::nodehandle N; Ros::serviceclient Client= N.serviceclient<beginner_tutorials::addtwoints> ("add_two_ints");  Beginner_tutorials::addtwoints SRV; SRV.REQUEST.A= Atoll (argv[1]); srv.request.b= Atoll (argv[2]); if(Client.call (SRV)) {Ros_info ("Sum:%ld", (Long int) Srv.response.sum); }  Else{ros_error ("Failed to call service add_two_ints"); Return1; } return0;}
Code Explained
Ros::serviceclient client = n.serviceclient<beginner_tutorials::addtwoints> ("add_two_ints ");

This creates a client for the add_two_ints service. The ros::serviceclient object is used the service later on.

  beginner_tutorials::addtwoints srv;   = Atoll (argv[1]);   = Atoll (argv[2]);

Here we instantiate an autogenerated service class, and assign values to its request member. A Service class contains the members, request and response. It also contains the class definitions, Request and Response.

if (Client.call (SRV))

This actually calls the service. Since service calls is blocking, it'll return once the call was done. If The service call succeeded, call () would return true and the value in srv.response would be valid. If The call didn't succeed, call () would return false and the value in srv.response would be invalid.

Building Your nodes

(CMakeLists.txt) Add the following at the end:

 add_executable (add_two_ints_server src/add_two_ints_server. CPP  ) target_link_libraries (Add_two_ints_server ${catkin_libraries}) add_ Dependencies (Add_two_ints_server beginner_tutorials_gencpp) add_executable (add_two_ints_client src /add_two _ints_client. cpp  ) target_link_libraries (Add_two_ints_client ${catkin_libraries}) add_dependencies ( Add_two_ints_client beginner_tutorials_gencpp)  

This would create the executables, add_two_ints_server and add_two_ints_client, which by default would go into package directory of your devel space, located by default at ~/catkin_ws/devel/lib/<package name>. You can invoke the executables directly or you can use the Rosrun to invoke them. They is not placed in ' <prefix>/bin ' because, would pollute the PATH when installing your package to the system . If you wish for your executable to is on the PATH at installation time, you can setup an install target, see:catkin/cmake Lists.txt

Catkin_make
Examining
1 3  1+3#134

Writing a simple Service and Client (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.