ROS 2服务-services-

本文深入讲解ROS2中的服务通信机制,对比主题发布-订阅模型,服务基于调用-响应模式,仅在请求时提供数据。文章涵盖服务的概念、命令功能、实践操作及调用示例。

前序:ROS 2主题-topics-

视频教程(Linux):https://www.bilibili.com/video/BV1WT4y177dK/

本节详细介绍服务service。

1. 如何理解服务service:

服务是ROS图中节点通信的另一种方法。 服务基于调用-响应模型,不同于主题的发布-订阅模型。 主题实现节点订阅数据流并获得连续更新,但是服务仅在客户端专门调用它们时才提供数据。

注意:本教程中提到的一些概念(例如节点和主题)已在前序教程中介绍。本节需要turtlesim包。 与往常一样,请不要忘记在打开的每个新终端后更新ROS 2。Linux:source;windows:call。

2. 服务service有哪些命令功能:

使用帮助命令:

ros2 service -h

显示如下:

命令有:

  • call:调用服务
  • find:列出给定类型服务
  • list:列出活动服务表
  • type:输出服务类型

具体各命令使用细节,使用 ros2 service <command> -h 进行查阅。

3. 服务命令实践

如之前一样,先开启:

  1. ros2 run turtlesim turtlesim_node
  2. ros2 run turtlesim turtle_teleop_key

3.1 列表

看看有哪些服务被启动了?

ros2 service list

看到两个节点都具有相同的六个服务,它们的名称带有参数。 ROS 2中几乎每个节点都具有类似结构。 在下一个教程中将有更多关于参数的信息。 在本教程中,将不讨论服务中的参数。

针对其中一些服务进行学习,如果感觉不熟练,推荐复习一下turtlesim和rqt简介教程。

3.2 类型

服务的数据类型采用具有描述服务请求和响应的结构。 服务类型的定义与主题类型相似,不同的是,服务类型包括两部分:

一个消息用于请求,另一个消息用于响应。

  • 请求
  • 响应

例如,一个典型的主题类型color:

uint8 r
uint8 g
uint8 b

定义了r g b三色。

而一个典型的服务类型setpen:

uint8 r
uint8 g
uint8 b
uint8 width
uint8 off
---

此处注意并没有消息用于响应,但是又---

又例如spawn:

float32 x
float32 y
float32 theta
string name # Optional.  A unique name will be created and returned if this is empty
---
string name

要找出服务的类型,请使用以下命令:

ros2 service type <service_name>

比如服务/clear

ros2 service type /clear

显示

std_srvs/srv/Empty

空类型表示服务调用在发出请求时不发送任何数据,而在接收响应时不接收任何数据。

看看ros2 service type /spawn                                                                                    

turtlesim/srv/Spawn

3.3 列表

这个命令类似topic,用法也类似:

ros2 service list -h

  • -h 帮助
  • -t 显示类型
  • -c 输出数量

ros2 service list -t

显示如下:

3.4 类型

如果需要查找给定类型,使用这些类型的服务使用如下命令:

ros2 service find <type_name>

比如查找空Empty类型

ros2 service find std_srvs/srv/Empty

会返回:

3.5 接口

可以从命令行调用服务,但是首先需要了解输入参数的结构。

ros2 interface show <type_name>.srv

例如,服务/clear的类型Empty:

ros2 interface show std_srvs/srv/Empty.srv

显示

---

---将请求结构(上方)与响应结构(下方)分开。 但是,正如先前所了解的,Empty类型不会发送或接收任何数据。 因此,自然地,它的结构是空白的。

再如/spawn

 

---行上方的信息可知调用/spawn所需的参数。 x,y和theta确定了小乌龟的位置和角度,命名是可选的。

在这种情况下,不需要了解该行下方的信息,但可以帮助了解通过调用获得的响应的数据类型。

3.6 调用

现在已经知道什么是服务类型,如何找到服务类型以及如何找到该类型的参数结构,可以使用以下命令调用服务:

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

更多内容:

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

如下命令将清除turtlesim窗口中小乌龟绘制的所有线条。

之前:

命令:

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: ''}"

回应如下:

虽然没有指定名称,但是系统给定turtle2。

 

节点可以使用ROS 2中的服务进行通信。服务仅将信息传递给节点(如果该节点明确要求该信息),并且每个请求仅将信息传递一次(而不是连续流)。 通常不希望使用服务进行连续调用; 否则主题甚至行动将更适合。

在本教程中,使用了命令行工具对服务进行识别,详细说明和调用。


 

### ROS Humble Husky RViz Configuration and Usage Tutorial In the context of working with a Husky robot under ROS 2 Humble, configuring RViz involves several key steps to ensure proper visualization and interaction capabilities. The process starts by obtaining an appropriate Docker image that includes ROS 2 Humble as described previously[^1]. Once this environment is set up, specific configurations tailored for Husky can be applied. #### Setting Up the Environment To begin using RViz specifically configured for Husky within ROS 2 Humble: - Ensure installation of the correct Docker container which contains all necessary dependencies including ROS 2 Humble. ```bash docker pull osrf/ros:humble-desktop-full-jammy ``` After pulling down the required Docker image, start it while mounting any needed directories from your host machine into the container so you have access to custom packages or files outside the default setup provided by OSRF's official images. #### Launching RViz Configured for Husky For launching RViz pre-configured for use with Husky robots, one typically uses launch files included in the `husky_viz` package available through standard ROS repositories when setting up environments based on ROS distributions like Humble. These launch files often come equipped with preset displays such as laser scans, camera feeds, point clouds among others relevant sensors commonly found aboard these platforms. If not already installed inside the running Docker instance, ```bash apt-get update && apt-get install -y ros-humble-husky-viz ``` Then execute the following command to bring up RViz along with its initial settings optimized for interacting visually with a Husky unit over ROS topics/services: ```bash ros2 launch husky_viz view_robot.launch.py ``` This will open RViz displaying various sensor outputs associated with the Husky platform automatically without manual adjustments being immediately necessary unless customization beyond defaults is desired. #### Customizing Displays Using Plugins As mentioned earlier about extending functionality via plugins[^3], users may wish to add more specialized visualizations pertinent to their applications involving Husky units. This could involve integrating third-party libraries supporting additional types of data representation or even developing proprietary extensions depending upon project requirements. #### Utilizing Rviz Effectively With Husky Beyond basic viewing functionalities offered out-of-the-box, leveraging tools integrated alongside RViz—such as dynamic reconfiguration interfaces accessible through commands similar to those used for color detection tasks outlined before[^4]—can enhance operational efficiency during development phases where real-time parameter tuning proves beneficial.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangrelay

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

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

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

打赏作者

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

抵扣说明:

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

余额充值