We will create a server node Add_two_ints_server, which will receive two integers and return their and. Switch directories to the previously established Beginner_tutorials package:
CD ~/catkin_ws/src/beginner_tutorials
Edit Src/add_two_ints_server.cpp File:
Copy the following code to the file and exit after saving:
#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); return true; } 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 (); return 0; }
The following explains the code:
" Ros/ros.h " "Beginner_tutorials/addtwoints.h"
beginner_tutorials/addtwoints.h is a header file that is automatically generated by the SRV file that we created earlier.
bool Add (beginner_tutorials::addtwoints::request &req, &res)
This function provides a service for adding two integers, which uses the request and response types defined in the SRV file, and returns a Boolean type value.
{ = req.a + req.b; Ros_info ("request:x=%ld, y=%ld", (longint) req.a, (long int) req.b); Ros_info ("sending back response: [%ld]", (longint ) res.sum); return true ; }
Here the two integers are added and stored in the response, then output some information about the request and response, and finally return a true value.
Ros::serviceserver service = N.advertiseservice ("add_two_ints", add);
This line of code creates a service.
Next, create a client and open a terminal input:
Gedit Src/add_two_ints_client.cpp
Copy the following code and exit after saving:
#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"); return 1; } 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"); return 1; } return 0; }
Here's a look at the code explanation:
Ros::serviceclient client = n.serviceclient<beginner_tutorials::addtwoints> ("add_two_ints ");
This line of code creates a client for add_two_ints, and theros::serviceclient object is then used to invoke the service.
beginner_tutorials::addtwoints srv; = Atoll (argv[1]); = Atoll (argv[2]);
We have an example of an automatically generated service class and assign values to its request members. A service class consists of two members, requests, and services. It also includes definitions, requests, and responses for two classes.
if (Client.call (SRV))
This code begins to actually invoke the service because the service call is blocked and returns immediately when the call is complete. If the service invocation succeeds, call() returns the value in true and SRV. Otherwise, call() returns the value in False and SRV.
Open ~/catkin_ws/src/beginner_tutorials/cmakelists.txt, copy the following code to the end of the file, save, exit:
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 will create two executables,add_two_ints_server and add_two_ints_client, which will be generated by default in ~/catkin_ws/devel/lib/ Share/<package name> directory, you can run them directly, or you can run them through Rosrun.
Start building Now:
CD ~/CATKIN_WS
Catkin_make
OK, everything is ready, now check to see if the program is correct. Open a terminal and run the server:
Rosrun beginner_tutorials Add_two_ints_server
A hint will appear:
- Ready to add the ints.
Then open a terminal and run the client:
- Rosrun beginner_tutorials add_two_ints_client 1 3
We can see the results returned as follows:
- Requesting 1+3
- 1 + 3 = 4
It means that you have succeeded.
Ros Server and Client