ROS基础 - 通过Service服务,实现ROS节点间通讯
文章目录
官方定义
Request / reply is done via a Service, which is defined by a pair of messages: one for the request and one for the reply.
概念
服务是同步的跨进程函数调用。使用 client/server 模型,能够让客户端节点调用运行在服务端节点中的函数。
service服务通讯机制是一种双向同步数据传输模式。基于客户端/服务器模型,两部分通信数据类型:一个用于请求,一个用于应答,类似web服务器。
同步:客户端发送请求数据,服务端完成处理后返回应答数据。client 端发送请求后会阻塞,直到 server 端返回结果才会继续执行。
Service服务与Topic话题的区别
话题:订阅/发布话题是不同步的,发布者只管发布消息,不管有没有或有几个订阅者,也不管订阅者能不能跟得上自己的发布速度。订阅者则只管监听消息,不会告诉发布者听没听到。这种方式交换数据的效率高,但完全不具备应答功能。
服务:当服务端收到服务请求后,会对请求做出响应,将数据的处理结果返回给客户端。这种模式更适用于双向同步的信息传输。服务调用非常适合那些只需要偶尔去做,并且会在有限的时间里完成的事。
1. 创建 srv 文件
ros 已经定义了一些服务,但我们也可以定义自己的服务。服务自定义文件通常放在功能包的 srv 文件夹下,文件扩展名为 .srv 。服务包含请求(request)数据和应答(response)数据,中间用三个小短线(—)隔开。
示例内容如下
2. CMakeLists.txt 文件编写
cmake_minimum_required(VERSION 3.0.2)
project(interactive_parking_spot)
find_package(catkin REQUIRED COMPONENTS
interactive_markers roscpp
visualization_msgs tf
rospy
std_msgs
message_generation
add_service_files(FILES ParkingSpotInfo.srv)
generate_messages(DEPENDENCIES std_msgs) #generate_messages() must be called before catkin_package() in project
catkin_package(
CATKIN_DEPENDS interactive_markers
roscpp visualization_msgs tf
rospy
std_msg
message_runtime # this will not be the only thing here
)
include_directories(include
../../devel/include # note:new add
${catkin_INCLUDE_DIRS}
)
add_executable(queryParkingSpotInfoServer src/QueryParkingSpotInfoServer.cpp)
target_link_libraries(interactive_parking_spot
${catkin_LIBRARIES}
)
add_executable(queryParkingSpotInfoClient src/QueryParkingSpotInfoClient.cpp)
target_link_libraries(queryParkingSpotInfoClient
${catkin_LIBRARIES}
)
注意:include_directories中增加了 …/…/devel/include ,否则在编写代码时,引用头文件 “#include <interactive_parking_spot/ParkingSpotInfo.h>” 会显示波浪线。 且头文件中的 相关结构 也会提示红色字体,认为是未识别。但其实是可以编译通过的。 建议在CMakeList.txt中,增加 “ …/…/devel/include”
运行catkin_make会生成三个类:ParkingSpotInfo,ParkingSpotInfoRequest,和 ParkingSpotInfoResponse。可以在 devel 文件夹下找到相应的生成文件,当然你可能永远都不需要去查看这些细节。
2.1. 执行 catkin_make 编译程序
cd Projects/HDMap/SourceCode/20220328-RViz/navigation_mesh_building/
catkin_make
2.2. 执行source 部署
source devel/setup.bash
2.3. 执行rossrv show 查看测试是否已经安装成功:
rossrv show interactive_parking_spot/ParkingSpotInfo
3. package.xml文件编写
在原文件中,增加:
<depend>rospy</depend>
<depend>std_msg</depend>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
4. C++ 服务端代码编写
#include <ros/ros.h>
#include <interactive_parking_spot/ParkingSpotInfo.h>
/* respose the client request of query parking spot info */
bool Server_Callback_ResposeQueryParkingSpotInfo(interactive_parking_spot::ParkingSpotInfo::Request &req,
interactive_parking_spot::ParkingSpotInfo::Response &res)
{
//res.c = req.a * req.b;
res.count = 10;
printf("Server: query_all_parking_spot_info result is as following: \n");
printf("ParkingSpot.Num = [%d] \n", res.count);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "simple_marker");
ros::NodeHandle nh2;
ros::ServiceServer server = nh2.advertiseService("query_all_parking_spot_info", Server_Callback_ResposeQueryParkingSpotInfo);
ROS_INFO("waiting query_all_parking_spot_info result ... \n ");
while (nh2.ok())
{
ros::spinOnce();
}
return 0;
}
5、C++ 客户端代码编写
#include <ros/ros.h>
#include<cstdlib>
#include <time.h>
#include <interactive_parking_spot/ParkingSpotInfo.h>
int main(int argc, char **argv)
{
srand(int(time(0)));
double a = rand()/(double(RAND_MAX)/100);
double b = rand()/(double(RAND_MAX)/100);
ros::init(argc, argv, "multi_client");
ros::NodeHandle nh;
ros::ServiceClient client = nh.serviceClient<interactive_parking_spot::ParkingSpotInfo>("query_all_parking_spot_info");
interactive_parking_spot::ParkingSpotInfo srv;
srv.request.cmdType = std::to_string(a);
if (client.call(srv))
{
printf("Client: query_all_parking_spot_info result is as following: \n");
printf("ParkingSpot.Num = [%d] \n", srv.response.count);
}
else
{
ROS_ERROR("Failed to call service");
}
return 0;
}
6. 运行
6.1. 服务端运行
首先我们将服务器节点开启
6.2. 客户端运行
在需要时启动客户端。
7. 常用命令
7.1. rosservice
7.1.1. rosservice list
前提:已启动相关服务。
先启动服务:
在使用rosservice list 进行查看
关闭服务后查询:
7.2. rossrv
7.2.1. rossrv show
catkin_make
source devel/setup.bash
rossrv show interactive_parking_spot/ParkingSpotInfo
在正确创建 .srv文件,并catkin_make成功后,执行该命令。
参考网址:
Linux环境下,ROS工程创建示例:
https://aduandniuniu.blog.csdn.net/article/details/123100280?spm=1001.2014.3001.5502
PCL系列:自定义Point 读、写文件代码示例
https://aduandniuniu.blog.csdn.net/article/details/123175013?spm=1001.2014.3001.5502
ROS::dynamic_reconfigure 动态参数使用 示例详解
https://aduandniuniu.blog.csdn.net/article/details/124235396?spm=1001.2014.3001.5502
ROS项目系列:自动驾驶之高精地图基本概念
https://aduandniuniu.blog.csdn.net/article/details/123037674?spm=1001.2014.3001.5502
Linux(Ubuntu) C++ Map使用示例代码(结构体)
https://aduandniuniu.blog.csdn.net/article/details/123082274?spm=1001.2014.3001.5502
其他参考:
1、https://wenku.baidu.com/view/f557a6a4f221dd36a32d7375a417866fb84ac0df?fr=shopSearch-pc
2、https://wenku.baidu.com/view/738f604425d3240c844769eae009581b6ad9bd4d.html
3、https://wenku.baidu.com/view/84ab33c01be8b8f67c1cfad6195f312b3169eb08.html
4、https://aduandniuniu.blog.csdn.net/article/details/123082101?spm=1001.2014.3001.5502
5、https://aduandniuniu.blog.csdn.net/article/details/123322139?spm=1001.2014.3001.5502
6、https://aduandniuniu.blog.csdn.net/article/details/123452283?spm=1001.2014.3001.5502
7、https://aduandniuniu.blog.csdn.net/article/details/124200188?spm=1001.2014.3001.5502