(五)阅读教程Understanding ROS 2 services

#十分钟的beginner教程两天看一篇我……

 

背景

【Services are another method of communication for nodes on the ROS graph. Services are based on a call-and-response model, versus topics’ publisher-subscriber model. While topics allow nodes to subscribe to data streams and get continual updates, services only provide data when they are specifically called by a client.】

services是另一种ROS 图中节点间交流 的方式。Services基于呼叫-应答模式,而不是主题的发布-订阅模式。相较于topics允许节点订阅数据流并且持续获取更新,services只在他们专门被客户端呼叫(调用)时提供数据。

【there can be many service clients using the same service.but there can only be one service server for a service】

可能有许多服务客户端使用同一个服务。但是一个服务只能有一个服务器服务。

 

Tasks

1、Setup

【Start up the two turtlesim nodes, /turtlesim and /teleop_turtle.】

 

2、ros2 service list

【Running the ros2 service list command in a new terminal will return a list of all the services currently active in the system:】

在新终端运行 ros2 service list 命令将会返回系统当前正在活动的所有services

【You will see that both nodes have the same six services with parameters in their names. Nearly every node in ROS 2 has these infrastructure services that parameters are built off of. There will be more about parameters in the next tutorial. In this tutorial, the parameter services will be omitted from discussion.】

我们将会看到所有nodes都有相同的六个services,他们名称中都带有 parameters。几乎ROS2中每个节点都有这些基础设施services,它们是基于parameter构建的。(built off of 建立于)下一节将有更多关于parameters的内容,这一教程中将略去对参数的讨论。

【For now, let’s focus on the turtlesim-specific services, /clear, /kill, /reset, /spawn, /turtle1/set_pen, /turtle1/teleport_absolute, and /turtle1/teleport_relative. You may recall interacting with some of these services using rqt in the “Introducing turtlesim and rqt” tutorial.】

现在让我们将关注点放在 turtlesim-specific services, /clear, /kill, /reset, /spawn, /turtle 1/set_pen, /turtle1/teleport_absolute, and /turtle1/teleport_relative.你或许还能想起教程"introducing turtlesim and rqt"中使用rqt与一些services进行交互的情况。

 

3、ros2 service type

【Services have types that describe how the request and response data of a service is structured. Service types are defined similarly to topic types, except service types have two parts: one message for the request and another for the response.】

Services 具有描述了如何从一个结构化的服务中请求和相应数据的types。services的种类和topic的种类定义是类似的,不同的是,一个message用于请求,另一个的message用于响应。

#To find out the type of a service, use the command:
ros2 service type <service_name>

【Let’s take a look at turtlesim’s /clear service. In a new terminal, enter the command:】

ros2 service type /clear

 Which should return:

【The Empty type means the service call sends no data when making a request and receives no data when receiving a response.】

这个Empty类型意味着,在发送请求时这个service调用不发送任何数据,在接收响应时这个service不接受任何数据。

 

3.1 ros2 service list -t

【To see the types of all the active services at the same time, you can append the --show-types option, abbreviated as -t, to the list command:】

想要同时看到所有活动services的类型,可以在list命令后加上 --show-types 选项,缩写为 -t

ros2 service list -t

 Which will return:

 

4、ros2 service find

【If you want to find all the services of a specific type, you can use the command:】

如果你想要的了解所有services的特定类型,你可以用以下命令:

ros2 service find <type_name>
#For example, you can find all the Empty typed services like this:
ros2 service find std_srvs/srv/Empty

 Which will return:

 

5、ros2 interface show

【You can call services from the command line, but first you need to know the structure of the input arguments.】

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

ros2 interface show <type_name>.srv
#To run this command on the /clear service’s type, Empty:
ros2 interface show std_srvs/srv/Empty.srv

Which will return:

【The --- separates the request structure (above) from the response structure (below). But, as you learned earlier, the Empty type doesn’t send or receive any data. So, naturally, its structure is blank.】

‘---’将(上方的)请求结构和(下方的)响应结构分开。但是就像我们之前所学的,Empty 类型将不会接收或发送任何数据。座椅自然地,这个结构是空白的。

#To see the arguments in a /spawn call-and-request, run the command:
ros2 interface show turtlesim/srv/Spawn.srv

Which will return:

【The information above the --- line tells us the arguments needed to call /spawn. x, y and theta determine the location of the spawned turtle, and name is clearly optional.】

在'---'以上的信息告诉我们,调用/spawn所需的参数。x,y和theta决定了spawned turtle的位置,显然名称是可选项。

【The information below the line isn’t something you need to know in this case, but it can help you understand the data type of the response you get from the call.】

分割线下方的信息此时我们并不需要了解,但是它帮助我们理解我们从调用得到的响应的数据类型。

 

6、ros2 service call

【Now that you know what a service type is, how to find a service’s type, and how to find the structure of that type’s arguments, you can call a service using:】

现在我们已知什么是service type,如何查询services的类型,以及如何找到该参数类型的结构,可以用以下的代码来调用一个service:

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

【The <arguments> part is optional. For example, you know that Empty typed services don’t have any arguments:】

参数部分是可选的。比如,我们已知Empty类型的services不含有任何参数:

ros2 service call /clear std_srvs/srv/Empty

【This command will clear the turtlesim window of any lines your turtle has drawn.】

这个命令将清空你的海龟在模拟窗口中画下的所有路线。

 

 

【Now let’s spawn a new turtle by calling /spawn and inputting arguments. Input <arguments> in a service call from the command-line need to be in YAML syntax.】

现在让我们通过调用/spawn并且输入参数来生成一个新的海龟。在命令行中输入service调用参数时需要使用YAML语法。

Enter the command:

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

 【You will get this method-style view of what’s happening, and then the service response:】

你将会得到这个方法样式的视图来描述这个事件,以及这个service的响应:

【Your turtlesim window will update with the newly spawned turtle right away:】

你的小海龟模拟窗口将会立即使用新产生的海龟进行更新:

 

 

summary

【Nodes can communicate using services in ROS 2. Services only pass information to a node if that node specifically requests it, and will only do so once per request (not in a continuous stream). You generally don’t want to use a service for continuous calls; topics or even actions would be better suited.】

ROS2 中nodes可以使用services来进行交互。Services只在node特别要求时传递信息到node,并且对每个需求只做一次响应(而不是一个连续流)。你平时并不想用service来持续的调用;topics甚至actions将会更适合(持续的调用)。

【In this tutorial you used command line tools to identify, elaborate on, and call services.】

在这个教程中使用命令行工具来识别、详细说明和调用services。

 

related content

【Check out this tutorial; it’s an excellent realistic application of “Understanding ROS 2 services” using a Robotis robot arm.】

这是一个使用Robotis机械臂来“理解ROS2services”的出色的实践应用

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值