ROS 消息通讯——服务器/客户端的来龙去脉

  • 创建功能包:
1 cd ~/learning_ws/src
2 catkin_create_pkg ros_tutorials_service message_generation std_msgs roscpp //创建功能包和依赖

 

 

  •  修改功能包说明文件package.xml:详细注释见上篇内容。
<?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="12345678@163.com">jjjj</author>
  <maintainer email="12345678@163.com">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编译配置文件CMakeLists.txt:详细注释见上篇内容
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})
  • 添加服务文件:与CMakeLists.txt对应,add_service_files( FILES SrvTutorial.srv)

  内容如下:---用于分割请求和响应,---上面为请求,下面为响应。

int64 a
int64 b
---
int64 result
  • 创建服务器节点:与CMakeLists.txt对应,add_executable(service_server src/service_server.cpp)

 

 

 内容如下:

 1 #include <ros/ros.h>
 2 #include <ros_tutorials_service/SrvTutorial.h>//服务头文件
 3 //如果收到客户端的请求,将执行以下处理,服务请求为req,服务响应为res
 4 bool calculation(ros_tutorials_service::SrvTutorial::Request &req,
 5                 ros_tutorials_service::SrvTutorial::Response &res)
 6 {
 7 //收到请求时,将两数之和保存到服务响应值中
 8 res.result = req.a+req.b;
 9 //显示a,b的值及结果
10 ROS_INFO("request:x=%ld,y=%ld",(long int)req.a,(long int)req.b);
11 ROS_INFO("sending back response:%ld",(long int)res.result);
12 return true;
13 }
14 
15 int main(int argc,char **argv)
16 {
17 ros::init(argc,argv,"service_server");//声明服务器节点名称
18 ros::NodeHandle nh;
19 //声明服务器,创建一个使用ros_tutorials_service功能包SrvTutorial服务文件的服务器
20 //ros_tutorial_service_server,服务名称是ros_tutorial_srv,有服务请求,执行calculation函数
21 ros::ServiceServer ros_tutorials_service_server = nh.advertiseService("ros_tutorial_srv", calculation);
22 
23 ROS_INFO("ready srv server!");
24 ros::spin();//等待客户端的(服务)请求
25 
26 return 0;
27 }                
  • 创建客户端节点:与CMakeLists.txt对应,add_executable(service_client src/service_client.cpp)

 

   内容如下:

 1 #include <ros/ros.h>
 2 #include <ros_tutorials_service/SrvTutorial.h>
 3 #include <cstdlib>
 4 
 5 int main(int argc,char **argv)
 6 {
 7 ros::init(argc,argv,"service_client");//声明客户端节点名称
 8 if(argc!=3)//输入值错误提示
 9 {
10   ROS_INFO("cmd:rosrun ros_tutorials_service service_client arg0 arg1");
11   ROS_INFO("arg0:double number,arg1:double number");
12   return 1;
13 }
14 ros::NodeHandle nh;
15 //声明客户端,创建一个使用ros_tutorials_service功能包SrvTutorial服务文件的客户端
16 //ros_tutorial_service_client,服务名称是ros_tutorial_srv,
17 ros::ServiceClient ros_tutorials_service_client = nh.serviceClient<ros_tutorials_service::SrvTutorial>("ros_tutorial_srv");
18 //声明要使用SrvTutorial服务文件名为srv的服务
19 ros_tutorials_service::SrvTutorial srv;
20 srv.request.a = atoll(argv[1]);
21 srv.request.b = atoll(argv[2]);
22 //请求服务,并显示服务器响应的结果
23 if(ros_tutorials_service_client.call(srv))
24 {
25 ROS_INFO("send srv,srv.Request.a and b:%ld,%ld",(long int)srv.request.a,(long int)srv.request.b);
26 ROS_INFO("sreceive srv,srv.Response.result:%ld",(long int)srv.response.result);
27 }
28 else
29 {
30 ROS_ERROR("Failed to call service ros_tutorial_srv");
31 return 1;
32 }
33 return 0;
34 }
  • 运行效果:如果没有输入a,b的值会有如下提示

 输入正确会有结果反馈(输入之间要有空格)

 

 同时看到,服务和客户端之间的请求是一次触发,只有客户端有正确的请求才会触发服务器响应,响应完后就会自动断开,不同于上一节的话题消息,只要建立后就一直会发送。

节点关系图:rqt_graph,由于服务是一次性的连接,不好采集建立时的关系图,只显示服务器,客户端请求完收到响应后自动断开不能看到。

利用rqt插件看一下它们的效果:在Experssion输入a,b的值,点击call.

 

posted @ 2020-03-27 12:26  静精进境  阅读(746)  评论(0编辑  收藏  举报