- Creating Feature Pack:
. 1 CD ~ / learning_ws / the src 2 catkin_create_pkg ros_tutorials_service message_generation std_msgs roscpp // Create function package dependency and
- Modify Feature Pack documentation package.xml: detailed notes see above article content.
<?xml version="1.0"?> <package format="2"> <name>ros_tutorials_service</name> <version>0.0.0</version> <description>The ros_tutorials_service package</description> <author email="[email protected]">jjjj</author> <maintainer email="[email protected]">jjjj</maintainer> <license>Apache License 2.0</license> <url type="website">https://jjjj.blog</url> <buildtool_depend>catkin</buildtool_depend> <build_depend>message_generation</build_depend> <build_depend>roscpp</build_depend> <build_depend>std_msgs</build_depend> <build_export_depend>roscpp</build_export_depend> <build_export_depend>std_msgs</build_export_depend> <exec_depend>roscpp</exec_depend> <exec_depend>std_msgs</exec_depend> <export></export> </package>
- CMake to compile profiles CMakeLists.txt: detailed notes see above pieces of content
cmake_minimum_required(VERSION 2.8.3) project(ros_tutorials_service) find_package(catkin REQUIRED COMPONENTS message_generation roscpp std_msgs ) #添加服务文件即请求和响应信息 add_service_files( FILES SrvTutorial.srv ) generate_messages( DEPENDENCIES std_msgs ) catkin_package( LIBRARIES ros_tutorials_service CATKIN_DEPENDS roscpp std_msgs ) include_directories( ${catkin_INCLUDE_DIRS} ) add_executable(service_server src/service_server.cpp) add_dependencies(service_server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) target_link_libraries(service_server ${catkin_LIBRARIES}) add_executable(service_client src/service_client.cpp) add_dependencies(service_client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) target_link_libraries(service_client ${catkin_LIBRARIES})
- Add a service file: correspondence with CMakeLists.txt, add_service_files (FILES SrvTutorial.srv)
As follows: --- for dividing the requests and responses, --- to the above request, the following response.
int64 a int64 b --- int64 result
- Create a server node: the CMakeLists.txt correspondence, add_executable (service_server src / service_server.cpp)
It reads as follows:
. 1 #include <ROS / ros.h> 2 #include <ros_tutorials_service / SrvTutorial.h> // Service header 3 // if the client's request is received, the following process is performed, the service request is a req, as service response RES . 4 BOOL Calculation (ros_tutorials_service :: :: SrvTutorial the request & REQ, . 5 ros_tutorials_service :: :: SrvTutorial the response & RES) . 6 { . 7 // request is received, the sum of the two numbers stored in the service response value . 8 res.result = + req.a req.b; . 9 // display a, b values and the results of 10 ROS_INFO ( " Request: X =% LD, LD% = Y " , ( Long int ) req.a, (Long int ) req.b); . 11 ROS_INFO ( " sending Back Response:% LD " , ( Long int ) res.result); 12 is return to true ; 13 is } 14 15 int main ( int argc, char ** the argv) 16 { 17 ROS :: the init (argc, argv, " service_server " ); // declare server node name 18 ROS :: NodeHandle nh; 19 // declaration server, create a server using ros_tutorials_service feature pack SrvTutorial service file 20 //ros_tutorial_service_server, the service name is ros_tutorial_srv, there is a service request, performing Calculation function 21 is ROS ServiceServer ros_tutorials_service_server :: = nh.advertiseService ( " ros_tutorial_srv " , Calculation); 22 is 23 is ROS_INFO ( " READY Server SRV! " ); 24 ROS :: Spin ( ); // wait for the client (service) request 25 26 is return 0 ; 27 }
- Create a client node: the CMakeLists.txt correspondence, add_executable (service_client src / service_client.cpp)
It reads as follows:
. 1 #include <ROS / ros.h> 2 #include <ros_tutorials_service / SrvTutorial.h> . 3 #include <the cstdlib> . 4 . 5 int main ( int argc, char ** the argv) . 6 { . 7 ROS :: the init (argc, the argv , " service_client " ); // declare the name of the client node . 8 IF (argc =! . 3 ) // input value error . 9 { 10 ROS_INFO ( " cmd: rosrun ros_tutorials_service service_client the arg0 arg1 " ); . 11 ROS_INFO (" Arg0: Number The Double, arg1: Number The Double " ); 12 return 1 ; 13 } 14 ROS :: NodeHandle nh; 15 // declaration client, create a customer service ros_tutorials_service feature pack SrvTutorial files using the end of the 16 // ros_tutorial_service_client, service names are ros_tutorial_srv, 17 ROS :: = nh.serviceClient the ServiceClient ros_tutorials_service_client <ros_tutorials_service :: SrvTutorial> ( " ros_tutorial_srv " ); 18 // declaration to use SrvTutorial service file called srv service 19 ros_tutorials_service :: SrvTutorial srv; 20 srv = Atoll .request.a (the argv [ . 1 ]); 21 is srv.request.b = Atoll (the argv [ 2 ]); 22 is // request service, and display the results of the server response 23 is IF (ros_tutorials_service_client.call (SRV)) 24 { 25 ROS_INFO ( " Send SRV, srv.Request. B and A:% LD, LD% " , ( Long int ) srv.request.a, ( Long int ) srv.request.b); 26 is ROS_INFO ( " sreceive SRV, srv.Response.result:% LD " , ( Long int ) srv.response.result); 27 } 28 the else 29 { 30 ROS_ERROR ( "Failed to call service ros_tutorial_srv"); 31 return 1; 32 } 33 return 0; 34 }
- Operating results: If no input values a, b have the following prompt
Enter the correct results will be fed back (there must be a space between the input)
At the same time see that requests between the service and the client is a trigger, only the client has the right request will trigger a server responds, the response will be automatically disconnected after, different from the one on the topic of messages, as long as the establishment It would have been sent.
Node diagram: rqt_graph, since the service is a one-time connection diagram when establishing good acquisition, only the server, the client receives the response request automatically disconnect complete can not be seen.
Plug-use rqt look at their performance: input values a, b in Experssion, click on the call.