ROS基础 - 通过Service服务,实现ROS节点间通讯

ROS基础 - 通过Service服务,实现ROS节点间通讯

  1. 创建 srv 文件
  2. CMakeLists.txt 文件编写
  3. package.xml文件编写
  4. C++ 服务端代码编写
  5. C++ 客户端代码编写
  6. 运行
  7. 常用命令:rosservice list 以及 rossrv show 等

官方定义

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)数据,中间用三个小短线(—)隔开。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BGO7LPOz-1650359116302)(vx_images/78053180807110.png)]
示例内容如下
在这里插入图片描述

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. 服务端运行

首先我们将服务器节点开启

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1bugmEwu-1650359116303)(vx_images/297037112901250.png)]

6.2. 客户端运行

在需要时启动客户端。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-joL6ulag-1650359116304)(vx_images/116236919648626.png)]](https://img-blog.csdnimg.cn/69bf42634dad43478282e8bb3928663d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBATml1bml1QW5kQWR1,size_20,color_FFFFFF,t_70,g_se,x_16)

7. 常用命令

7.1. rosservice

7.1.1. rosservice list

前提:已启动相关服务。

先启动服务:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GhUmGqB9-1650359116305)(vx_images/39283630941056.png)]
在使用rosservice list 进行查看
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9ABmhv6y-1650359116306)(vx_images/263123894889460.png)]
关闭服务后查询:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R4HQPmA8-1650359116307)(vx_images/482852323576102.png)]

7.2. rossrv

7.2.1. rossrv show
catkin_make
source devel/setup.bash
rossrv show interactive_parking_spot/ParkingSpotInfo

在正确创建 .srv文件,并catkin_make成功后,执行该命令。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hbNZX0vK-1650359116308)(vx_images/396383122122580.png)]
参考网址:

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Adunn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值