文章目录
- 
- ① 背景
- ② 前提
- ③ 任务
- 
- Ⅰ准备
- Ⅱ ros2 service 命令
- 
- ros2 service list
- ros2 srv show (eloquent版本是 ros2 service type)
- 
- eloquent 版本
 
- ros2 service find (dashing 不支持 )
- ros2 interface show (dashing 不支持)
- ros2 service call
 
 
- ④ 总结
 
① 背景
   service 也是通讯的一种方式,req/respone 的方式,是触发式的,topic 是订阅式的   
② 前提
- 装ros2
- 配置环境
- 装小乌龟
③ 任务
Ⅰ准备
启动小乌龟 命令:
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
Ⅱ ros2 service 命令
ros2 service list
列出服务 命令提示
usage: ros2 service list [-h] [--spin-time SPIN_TIME] [-t] [-c]
Output a list of available services
optional arguments:
  -h, --help            show this help message and exit
  --spin-time SPIN_TIME
                        Spin time in seconds to wait for discovery (only
                        applies when not using an already running daemon)
  -t, --show-types      Additionally show the service type
  -c, --count-services  Only display the number of services discovered
   可以通过命令和rqt去看  
 
   
ros2 srv show (eloquent版本是 ros2 service type)
就是看看srv的消息的几种方法,
- 执行命令行 ros2 srv show turtlesim/srv/SetPen
- rqt 查看
- 文件查看
  
 
   
eloquent 版本
  
![[ros2学习]-CLI Tools - 了解ROS 2服务(service) [ros2学习]-CLI Tools - 了解ROS 2服务(service)](https://img-blog.csdnimg.cn/20200403101407767.png) 
   
ros2 service find (dashing 不支持 )
根据类型找服务,使用eloquent版本
ros2 service find <type_name>
例如
ros2 service find std_srvs/srv/Empty
  
![[ros2学习]-CLI Tools - 了解ROS 2服务(service) [ros2学习]-CLI Tools - 了解ROS 2服务(service)](https://img-blog.csdnimg.cn/20200403101533958.png) 
   
ros2 interface show (dashing 不支持)
   查看service的消息信息,用的eloquent版本 命令:ros2 interface show <type_name>.srv 例子:ros2 interface show turtlesim/srv/Spawn.srv   
![[ros2学习]-CLI Tools - 了解ROS 2服务(service) [ros2学习]-CLI Tools - 了解ROS 2服务(service)](https://img-blog.csdnimg.cn/20200403101817438.png) 
   
ros2 service call
调用服务,命令行模式
<code class="prism language-bash has-numbering">ros2 <span class="token function">service</span> call <span class="token operator"><</span>service_name<span class="token operator">></span> <span class="token operator"><</span>service_type<span class="token operator">></span> <span class="token operator"><</span>arguments<span class="token operator">> </span> </code>
我们调用/clear 和 /spawn
<code class="prism language-bash has-numbering">ros2 <span class="token function">service</span> call /clear std_srvs/srv/Empty
ros2 <span class="token function">service</span> call /spawn turtlesim/srv/Spawn <span class="token string">"{x: 2, y: 2, theta: 0.2, name: 'bigggg'}"
</span>
</code>
  
 
   
④ 总结
   如果是不是连续的通讯,可以使用service 和action更好一点

![[ros2学习]-Awesome Robot Operating System2](https://blog.75271.com/wp-content/themes/com75271/timthumb.php?src=https://img-blog.csdnimg.cn/20200329153109520.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNjYyODc3OA==,size_16,color_FFFFFF,t_70&h=110&w=185&q=90&zc=1&ct=1)
![[ros2学习]-CLI Tools – 了解ROS 2参数(parameters)](https://blog.75271.com/wp-content/themes/com75271/timthumb.php?src=https://img-blog.csdnimg.cn/202004021658373.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNjYyODc3OA==,size_16,color_FFFFFF,t_70&h=110&w=185&q=90&zc=1&ct=1)
