Home » ROS2入门教程 » ROS2入门教程-services简介

ROS2入门教程-services简介

ROS2入门教程-services简介

说明:

  • 介绍ros2 services(服务)

概念:

  • services(服务)是ROS图上节点通信的另一种方法。
  • 服务基于呼叫响应模型,而不是主题的发布者-订阅者模型。
  • 尽管主题允许节点订阅数据流并获得连续更新,但是服务仅在客户端专门调用它们时才提供数据。

请输入图片描述

  • 服务端和客户端之间,是一对一或一对多关系

请输入图片描述

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
  • 显示服务及消息类型:
$ ros2 service list -t
/clear [std_srvs/srv/Empty]
/kill [turtlesim/srv/Kill]
/reset [std_srvs/srv/Empty]
/spawn [turtlesim/srv/Spawn]
/teleop_turtle/describe_parameters [rcl_interfaces/srv/DescribeParameters]
/teleop_turtle/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
/teleop_turtle/get_parameters [rcl_interfaces/srv/GetParameters]
/teleop_turtle/list_parameters [rcl_interfaces/srv/ListParameters]
/teleop_turtle/set_parameters [rcl_interfaces/srv/SetParameters]
/teleop_turtle/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]
/turtle1/set_pen [turtlesim/srv/SetPen]
/turtle1/teleport_absolute [turtlesim/srv/TeleportAbsolute]
/turtle1/teleport_relative [turtlesim/srv/TeleportRelative]
/turtlesim/describe_parameters [rcl_interfaces/srv/DescribeParameters]
/turtlesim/get_parameter_types [rcl_interfaces/srv/GetParameterTypes]
/turtlesim/get_parameters [rcl_interfaces/srv/GetParameters]
/turtlesim/list_parameters [rcl_interfaces/srv/ListParameters]
/turtlesim/set_parameters [rcl_interfaces/srv/SetParameters]
/turtlesim/set_parameters_atomically [rcl_interfaces/srv/SetParametersAtomically]

ros2 service type 命令:

  • 服务接收的消息类型
  • 格式:ros2 service type <service_name>
  • 例子:
$ ros2 service type /clear
std_srvs/srv/Empty

ros2 service find命令:

  • 找到使用某类消息类型的服务
  • 格式:ros2 service find <type_name>
  • 例子:
$ ros2 service find std_srvs/srv/Empty
/clear
/reset

ros2 interface show命令:

  • 显示服务的消息类型具体定义
  • 格式:ros2 interface show <type_name>.srv
  • 例子:
$ ros2 interface show std_srvs/srv/Empty.srv
---
  • 例子2:
$ ros2 interface show turtlesim/srv/Spawn.srv
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 call命令:

  • 调用服务命令
  • 格式:ros2 service call <service_name> <service_type>
  • 例子:
 ros2 service call /clear std_srvs/srv/Empty
  • 效果:

请输入图片描述

  • 清除行走轨迹

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros2入门教程