服务通信也是 ROS
中一种极其常用的通信模式,服务通信是基于请求响应模式的,是一种应答机制。也即: 一个节点A向另一个节点B发送请求,B接收处理请求并产生响应结果返回给A。比如如下场景:
机器人巡逻过程中,控制系统分析传感器数据发现可疑物体或人… 此时需要拍摄照片并留存。
在上述场景中,就使用到了服务通信。
一个节点需要向相机节点发送拍照请求,相机节点处理请求,并返回处理结果
与上述应用类似的,服务通信更适用于对时时性有要求、具有一定逻辑处理的应用场景。
服务通信较之于话题通信更简单些,理论模型如下图所示,该模型中涉及到三个角色:
ROS master
(管理者)Server
(服务端)Client
(客户端)
ROS Master
负责保管 Server
和 Client
注册的信息,并匹配话题相同的 Server
与 Client
,帮助 Server
与 Client
建立连接,连接建立后,Client
发送请求信息,Server
返回响应信息。
1. 客户端
1.1 客户端模型
客户端模型如下:
1.2 创建功能包
注意:一定要在 工作空间的 src
目录下创建功能包。
$ cd ros_demo/src/
$ catkin_create_pkg service_client roscpp rospy std_msgs geometry_msgs turtlesim
1.3 客户端代码
client.py
代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn
import sys
import rospy
from turtlesim.srv import Spawn
def turtle_spawn():
# ROS节点初始化
rospy.init_node('turtle_spawn')
# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
rospy.wait_for_service('/spawn')
try:
add_turtle = rospy.ServiceProxy('/spawn', Spawn)
# 请求服务调用,输入请求数据
response = add_turtle(2.0, 2.0, 0.0, "turtle2")
return response.name
except rospy.ServiceException, e:
print "Service call failed: %s"%e
if __name__ == "__main__":
#服务调用并显示调用结果
print "Spwan turtle successfully [name:%s]" %(turtle_spawn())
2. 服务端
2.1 服务端模型
2.2 服务端代码
server.py
内容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将执行/turtle_command服务,服务数据类型std_srvs/Trigger
import rospy
import thread,time
from geometry_msgs.msg import Twist
from std_srvs.srv import Trigger, TriggerResponse
pubCommand = False;
turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
def command_thread():
while True:
if pubCommand:
vel_msg = Twist()
vel_msg.linear.x = 0.5
vel_msg.angular.z = 0.2
turtle_vel_pub.publish(vel_msg)
time.sleep(0.1)
def commandCallback(req):
global pubCommand
pubCommand = bool(1-pubCommand)
# 显示请求数据
rospy.loginfo("Publish turtle velocity command![%d]", pubCommand)
# 反馈数据
return TriggerResponse(1, "Change turtle command state!")
def turtle_command_server():
# ROS节点初始化
rospy.init_node('turtle_command_server')
# 创建一个名为/turtle_command的server,注册回调函数commandCallback
s = rospy.Service('/turtle_command', Trigger, commandCallback)
# 循环等待回调函数
print "Ready to receive turtle command."
thread.start_new_thread(command_thread, ())
rospy.spin()
if __name__ == "__main__":
turtle_command_server()
3. 运行代码
3.1 运行客户端
$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ rescore
$ rosrun turtlesim turtlesim_node
$ rosrun service_client client.py
运行结果:
$ rosrun service_client client.py
Spwan turtle successfully [name:turtle2]
3.2 运行服务端
$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ rescore
$ rosrun turtlesim turtlesim_node
分别新开两个命令行终端,执行以下命令
$ rosrun service_client server.py
Ready to receive turtle command.
[INFO] [1655691376.455429]: Publish turtle velocity command![1]
[INFO] [1655691386.447185]: Publish turtle velocity command![0]
[INFO] [1655703701.212224]: Publish turtle velocity command![1]
[INFO] [1655703752.681859]: Publish turtle velocity command![0]
$ rosservice call /turtle_command "{}"
success: True
message: "Change turtle command state!"
$ rosservice call /turtle_command "{}"
success: True
message: "Change turtle command state!"