Services are another method of communication for nodes in 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.
服务,是另外一种通讯方式,不同于topic 持续的,周期的。
服务是,事件类型的。 被客户端 请求了, 服务器端会响应。 响应结束,服务可能就结束了。
节点需要实现 客户端或服务端的逻辑。
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
ros2 service list -t
/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
基本上任意一个节点 ,都调用上面这6个服务。 (参数设置,读取, 基本的一些函数?)
服务类型: service type--
服务就是通讯方式,通讯的内容=service type中定义 (类比 topic, 的message,区别是topic 发布和订阅 ,message数据结构类型一样),服务,可能请求是一个消息类型,响应又是一个。(容易理解,有时候可能没有响应。)
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.
ros2 service type <service_name>
返回std_srvs/srv/Empty ...空表示,没有内容
ros2 service list -t
查询全部服务和他们的类型
反过来, 通过服务类型。来查哪些服务用了
ros2 service find <xx service type>
ros2 service find std_srvs/srv/Empty
返回:
/clear
/kill
ros2 interface show < type name>.srv
(和查看message 定义一样,就多了蓝色部分的后缀。)
ros2 interface show std_srvs/srv/Empty
---
luke@pc:~/Desktop$ 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(注:小乌龟,构造,出生地点,名字..)
luke@pc:~/Desktop$ ros2 interface show rcl_interfaces/srv/SetParameters
# A list of parameters to set.
Parameter[] parameters (注 Parameter 数据类型, 多个)---
# Indicates whether setting each parameter succeeded or not and why.
SetParametersResult[] results
luke@pc:~/Desktop$ ros2 interface show rcl_interfaces/srv/GetParameters
# TODO(wjwwood): Decide on the rules for grouping, nodes, and parameter "names"
# in general, then link to that.
#
# For more information about parameters and naming rules, see:
# https://design.ros2.org/articles/ros_parameters.html
# https://github.com/ros2/design/pull/241# A list of parameter names to get.
string[] names---
# List of values which is the same length and order as the provided names. If a
# parameter was not yet set, the value will have PARAMETER_NOT_SET as the
# type.
ParameterValue[] values
luke@pc:~/Desktop$ ros2 interface show rcl_interfaces/srv/ListParameters
# Recursively get parameters with unlimited depth.
uint64 DEPTH_RECURSIVE=0# The list of parameter prefixes to query.
string[] prefixes# Relative depth from given prefixes to return.
#
# Use DEPTH_RECURSIVE to get the recursive parameters and prefixes for each prefix.
uint64 depth---
# The list of parameter names and their prefixes.
ListParametersResult result
调用服务:
ros2 service call <service_name> <service_type> <arguments>
(类比 ros2 topic pub <topic name> <topic type> <opts>)
总结:
Nodes can communicate using services in ROS 2. Unlike a topic - a one way communication pattern where a node publishes information that can be consumed by one or more subscribers - a service is a request/response pattern where a client makes a request to a node providing the service and the service processes the request and generates a response.
服务也是节点的一种通讯方式, 和topic一样。
区别是:
topic 是单向的, A 节点到B节点 (或1对多个节点)
service是C/S 形式,一个请求,一个响应。