Understanding services:理解服务(Service)

简介: Understanding services:理解服务(Service)

@[toc]
参考官方文档:Understanding services

背景

服务(Service)是ROS图(ROS graph)中节点的另一种通信方法。服务是基于调用和响应(call-and-response)的模式,而不是话题的发布者-订阅者(publisher-subscriber)模式。话题允许节点订阅数据流并获得持续的更新,而服务只在被客户端调用时提供数据。
image.png
image.png

1. 准备工作

启动两个 turtlesim 节点,/turtlesim 和 /teleop_turtle。

打开一个新的终端并运行:

ros2 run turtlesim turtlesim_node

打开另一个终端并运行

ros2 run turtlesim turtle_teleop_key

2. ros2 service list 命令

在新终端中运行 ros2 service list 命令将返回一个列表,展示系统中当前活动的所有服务:

/clear
/kill
/reset
/spawn
/teleop_turtle/describe_parameters
/teleop_turtle/get_parameter_types
/teleop_turtle/get_parameters
/teleop_turtle/list_parameters
/teleop_turtle/set_parameters
/teleop_turtle/set_parameters_atomically
/turtle1/set_pen
/turtle1/teleport_absolute
/turtle1/teleport_relative
/turtlesim/describe_parameters
/turtlesim/get_parameter_types
/turtlesim/get_parameters
/turtlesim/list_parameters
/turtlesim/set_parameters
/turtlesim/set_parameters_atomically

你会看到 teleop_turtle 和 turtlesim 这两个节点都有同样的六个服务,其名称中都有 parameters 。在ROS 2中,几乎每个节点都有这些基础设施服务,而参数是建立在这些服务之上的。在下一个教程中会有更多关于参数的内容。在本教程中,将省略参数服务的讨论。

现在,让我们把注意力集中在针对海龟的服务上,/clear ,/kill ,/reset , /spawn , /turtle1/set_pen , /turtle1/teleport_absolute , 和 /turtle1/teleport_relative 。你可能记得在Using turtlesim and rqt:使用 turtlesim 和 rqt教程中使用rqt与这些服务中的一些进行互动。

3. ros2 service type 命令

要想知道一个服务的类型,使用命令:

ros2 service type <service_name>

让我们看一下 turtlesim 的 /clear 服务。在一个新的终端,输入以下命令:

ros2 service type /clear

应该返回:

std_srvs/srv/Empty

Empty 类型意味着服务调用在发出请求时不发送数据,在接收响应时不接收数据。

3.1 ros2 service list -t 命令

如果要看到所有正在运行的服务的类型,你可以在 list 命令中附加 --show-types 选项,缩写为 -t :

ros2 service list -t

返回结果如下:

/clear [std_srvs/srv/Empty]
/kill [turtlesim/srv/Kill]
/reset [std_srvs/srv/Empty]
/spawn [turtlesim/srv/Spawn]
...
/turtle1/set_pen [turtlesim/srv/SetPen]
/turtle1/teleport_absolute [turtlesim/srv/TeleportAbsolute]
/turtle1/teleport_relative [turtlesim/srv/TeleportRelative]
...

4. ros2 service find 命令

如果你想找到某个特定类型的所有服务,你可以使用命令:

ros2 service find <type_name>

例如,你可以通过下面这条命令找到所有 Empty 类型的服务:

ros2 service find std_srvs/srv/Empty

返回结果如下:

/clear
/reset

5. ros2 interface show 命令

你可以从命令行调用服务,但首先你需要知道输入参数的结构:

ros2 interface show <type_name>

例如:

ros2 interface show std_srvs/srv/Empty

返回结果如下:

---

---将请求结构(上面)和响应结构(下面)分开。但是,正如你前面所学到的,空(Empty)类型不发送或接收任何数据。所以,自然地,它的结构是空白的。

让我们来查看一个具有发送和接收数据类型的服务,比如 /spawn 。从 ros2 service list -t 的结果可以知道 /spawn 的类型是 turtlesim/srv/Spawn:

ros2 interface show turtlesim/srv/Spawn
float32 x
float32 y
float32 theta
string name # Optional. A unique name will be created and returned if this is empty
---
string name

6. ros2 service call 命令

现在你知道了什么是服务类型,如何找到一个服务的类型,以及如何找到该类型的参数结构,你可以用以下方法调用一个服务:

ros2 service call <service_name> <service_type> <arguments>

\<arguments> 部分是可选的。例如,空类型的服务没有任何参数:

ros2 service call /clear std_srvs/srv/Empty

这条命令将清除海龟模拟窗口中的任何线条。

现在让我们通过调用 /spawn 服务,输入参数来生成一个新的乌龟。在命令行的服务调用中,输入的 \<arguments> 需要使用YAML语法:

ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"

响应如下:

requester: making request: turtlesim.srv.Spawn_Request(x=2.0, y=2.0, theta=0.2, name='')
\
response:
turtlesim.srv.Spawn_Response(name='turtle2')

你的turtlesim窗口将立即生成新的乌龟。

目录
相关文章
|
9月前
|
存储 应用服务中间件 开发工具
Platform as a Service
PaaS(Platform as a Service,平台即服务)是一种云计算服务模型,它提供了一组开发工具和服务,使开发人员可以构建和部署应用程序,而无需关心底层基础设施的管理。PaaS 通常包括一个操作系统级别的基础设施、服务器、存储和网络服务,以及一系列开发工具和服务,如数据库、Web 服务器、应用服务器、消息传递、身份认证等。
148 2
Platform as a Service
|
4月前
|
JSON API 数据安全/隐私保护
【Azure Cloud Service】使用RESTAPI更新Cloud Service(Extended Support) 中所配置的证书
本文介绍了在更新Azure Cloud Service (Extended Support) 证书时,若旧证书(如中间证书、根证书)存储在Key Vault Secret中,而新证书仅匹配到服务器证书时,可能导致的错误及解决方法。建议使用PowerShell或RestAPI进行涉及机密的更新。文章详细描述了使用REST API更新证书的三个步骤:上传证书到Azure Key Vault、获取Cloud Service信息并发送GET请求、更新Cloud Service信息并发送PUT请求。通过这些步骤,可以成功更新证书并在云服务节点中验证证书信息。
|
9月前
|
数据库 数据中心
AWS 的Application Discovery Service (ADS)
AWS 的Application Discovery Service (ADS)
130 3
|
中间件 Serverless Go
Backend-as-a-Service (BaaS) for Efficient Software Development
The adoption of the Internet and mobile technologies has revolutionized the business ecosystem, with entrepreneurs able to implement ideas quickly by .
1769 0
Backend-as-a-Service (BaaS) for Efficient Software Development
|
容器
Introduction to Alibaba Cloud Container Service
In this whitepaper, we will take a look at the technologies and origins of containerization, as well as its benefits and challenges.
1228 0
Introduction to Alibaba Cloud Container Service
|
监控 网络协议 关系型数据库
|
弹性计算 Kubernetes Docker
Alibaba Cloud Container Service vs. Amazon EC2 Container Service vs. Azure Container Service
Containers have increasingly become the preferred way to run applications at scale in the cloud. Yet when running applications in production, organizations leave nothing to chance.
3333 0