Matlab与ROS(1/2)---服务端和客户端数据通信(五)

news/2025/2/12 8:15:19/

0. 简介

在前几讲我们讲了Matlab中的Message以及Topic的相关知识。而ROS主要支持的通信机制还有服务这一类。服务通过允许请求以及响应的通信方式,来给整个系统完成更紧密的耦合。服务客户端向服务服务器发送请求消息并等待响应。服务器将使用请求中的数据构造响应消息并将其发送回客户端。每个服务都有一个类型,该类型决定请求和响应消息的结构。

1. ROS1的服务端和客户端

在这里插入图片描述

1.1 创建服务端

在ROS1当中,如果希望创建一个简单的服务服务器,并在调用服务时能够显示“A service client is calling”。使用rosssvcserver命令创建服务。从而指定服务名称和服务消息类型。然后还要将回调函数定义为exampleHelperROSEmptyCallback。

为了获得更快的性能,一般我们在设置rossvcsrve时候带上结构格式消息类型。

testserver = rossvcserver("/test","std_srvs/Empty",@exampleHelperROSEmptyCallback,"DataFormat","struct")

testserver =
ServiceServer with properties:
ServiceType: ‘std_srvs/Empty’
ServiceName: ‘/test’
NewRequestFcn: @exampleHelperROSEmptyCallback
DataFormat: ‘struct’

当列出ROS网络中的所有服务时,可以看到新服务/test。

rosservice list

/add
/matlab_global_node_55791/get_loggers
/matlab_global_node_55791/set_logger_level
/node_1/get_loggers
/node_1/set_logger_level
/node_2/get_loggers
/node_2/set_logger_level
/node_3/get_loggers
/node_3/set_logger_level
/reply
/test

此外还可以使用rosservice info获取有关您的服务的更多信息。

rosservice info /test

Node: /matlab_global_node_55791
URI: rosrpc://ah-csalzber:51639
Type: std_srvs/Empty
Args: MessageType

1.2 创建客户端

使用服务客户机从ROS服务服务器请求信息。要创建客户端,请使用带有服务名称的rossvcclient作为参数。

为我们刚刚创建的/test服务创建一个服务客户机。

testclient = rossvcclient("/test","DataFormat","struct")

testclient =
ServiceClient with properties:
ServiceType: ‘std_srvs/Empty’
ServiceName: ‘/test’
DataFormat: ‘struct’

为服务创建一个空请求消息。使用rosmessage函数并将客户端作为第一个参数传递。这将创建一个具有服务指定的消息类型和格式的服务请求函数。

testreq = rosmessage(testclient)

testreq = struct with fields:
MessageType: ‘std_srvs/EmptyRequest’

确保服务已连接到客户端,并在必要时等待客户端连接。

waitForServer(testclient,"Timeout",3)

当您希望从服务器获得响应时,请使用call函数,该函数将调用服务服务器并返回响应。您之前创建的服务服务器将返回一个空响应。此外,它将调用exampleHelperROSEmptyCallback函数并显示字符串“A service client is calling”。您还可以定义一个Timeout参数,该参数指示客户机应该等待响应的时间。

testresp = call(testclient,testreq,"Timeout",3);

如果上面的调用函数失败,就会出错。如果希望使用条件对调用失败做出反应,则可以从调用函数返回状态和statustext输出,而不是错误。状态输出指示调用是否成功,而statustext提供其他信息。waitForServer也可以返回类似的输出。

numCallFailures = 0;
[testresp,status,statustext] = call(testclient,testreq,"Timeout",3);
if ~statusnumCallFailures = numCallFailues + 1;fprintf("Call failure number %d. Error cause: %s\n",numCallFailures,statustext)
elsedisp(testresp)
end
MessageType: 'std_srvs/EmptyResponse'

1.3 两数之和

如果说我们想要完成两数求和这样的功能,我们可以先将现有的服务类型roscpp_tutorials/TwoInts用于此任务。您可以通过调用rosmsg show来检查请求和响应消息的结构。请求包含两个整数A和B,响应包含它们在Sum中的加法。

rosmsg show roscpp_tutorials/TwoIntsRequest

int64 A
int64 B

rosmsg show roscpp_tutorials/TwoIntsResponse

int64 Sum

使用此消息类型创建服务服务器和计算加法的回调函数。为了方便起见,exampleHelperROSSumCallback函数已经实现了这个计算。将函数指定为回调函数。

sumserver = rossvcserver("/sum","roscpp_tutorials/TwoInts",@exampleHelperROSSumCallback,"DataFormat","struct")

sumserver =
ServiceServer with properties:
ServiceType: ‘roscpp_tutorials/TwoInts’
ServiceName: ‘/sum’
NewRequestFcn: @exampleHelperROSSumCallback
DataFormat: ‘struct’

要调用服务服务器,必须创建一个服务客户机。注意,这个客户端可以在ROS网络中的任何地方创建。为了本例的目的,我们将在MATLAB中为/sum服务创建一个客户端。

sumclient = rossvcclient("/sum","DataFormat","struct")
sumreq = rosmessage(sumclient);
sumreq.A = int64(2);
sumreq.B = int64(1)
if isServerAvailable(sumclient)sumresp = call(sumclient,sumreq,"Timeout",3)
elseerror("Service server not available on network")
end

sumresp = struct with fields:
MessageType: ‘roscpp_tutorials/TwoIntsResponse’
Sum: 3

2. ROS2的服务端和客户端

…详情请参照古月居


http://www.ppmy.cn/news/39581.html

相关文章

帮小忙(网站)

首先,网站的工具超级丰富,涵盖面好广,包含有图片类工具,数据换算,生活娱乐,教育,文本工具,开发工具,文档转换,视频,PDF转换工具等等9个大类&#…

SpringBoot创建和使用

目录 什么是SpringBoot SpringBoot的优点 SpringBoot项目的创建 1、使用idea创建 2、项目目录介绍和运行 Spring Boot配置文件 1、配置文件 2、配置文件的格式 3、properties 3.1、properties基本语法 3.2、读取配置文件 3.3、缺点 4、yml 4.1、优点 4.2、yml基本…

研究的艺术 (The craft of research) 读书笔记

前言 如果你对这篇文章感兴趣,可以点击「【访客必读 - 指引页】一文囊括主页内所有高质量博客」,查看完整博客分类与对应链接。 对于研究者而言,写作是一件很重要的事,好的写作不仅能让更多人愿意读,获得更大影响力&…

lodash-es 工具库

数字化管理平台 Vue3ViteVueRouterPiniaAxiosElementPlus Vue权限系统案例 个人博客地址 Lodash中文文档 Lodash 是一个一致性、模块化、高性能的 JavaScript 实用工具库,算是从 Underscore 分离出来的超集。 Lodash 通过降低 array、number、objects、string 等…

Debezium报错处理系列之五十七:Can‘t compare binlog filenames with different base names

Debezium报错处理系列之五十七:Cant compare binlog filenames with different base names 一、完整报错二、错误原因三、解决方法Debezium报错处理系列一:The db history topic is missing. Debezium报错处理系列二:Make sure that the same history topic isn‘t shared b…

Redis篇之Redis事务

Redis事务 Redis事务的本质是一组命令的集合 一个事务中所有命令会按照顺序串行化执行队列中的命令,其他客户端提交的命令请求不会插入到事务执行命令序列中。 事务执行三阶段: 开启:以 MULTI 开始一个事务 入队:将多个命令入队…

基础练习 十进制转十六进制

number int(input()) hex_num hex(number) print(hex_num[2:].upper())

ORACLE EBS 系统主数据管理(2)

ORACLE EBS 系统主数据管理 五、结语 (三)Item 的类别(Category) 上面所讲到的Item编码中的分类(UNSPSC),一般来说还不是系统(各应用功能模块)中真正使用到的类别&…