当前位置:网站首页>ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
2022-06-26 04:35:00 【wohu1104】
服务通信也是 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!"

边栏推荐
- The select option in laravel admin contains a large amount of data
- PHP has the problem of using strtotime to obtain time in months and months [original]
- CTF PHP audit bypasses filtering learning from topics
- 做软件测试学历重要还是能力重要
- [H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface
- Report on demand situation and development trend of China's OTC industry from 2022 to 2028
- Install SVN in Pagoda and build SVN version Library
- numpy 通用函数
- 1.24 learning summary
- 2022 talent strategic transformation under the development trend of digital economy
猜你喜欢
![[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching](/img/e4/27611abdd000019e70f4447265808c.jpg)
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching

Motivational skills for achieving goals

mysql高级学习(跟着尚硅谷老师周阳学习)

08_ Spingboot integrated redis

0622-马棕榈跌9%

pip 批量完全卸载包

2021/11/6-burpsuit packet capturing and web page source code modification

Tp6 controller does not exist: app\index\controller\index

NVM installation and use and NPM package installation failure record

Construction of art NFT trading platform | NFT mall
随机推荐
Multipass中文文档-提高挂载性能
1.12 learning summary
Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]
6、 Project practice --- identifying cats and dogs
Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
JWT token authentication verification
How can the intelligent transformation path of manufacturing enterprises be broken due to talent shortage and high cost?
Analysis report on the development trend and operation status of China's environmental monitoring instrument industry from 2022 to 2028
Video label forbids downloading. The test is valid. Hide button. The test is valid at three points
Dameng database backup and restore
Oracle 数据泵导表
Your requirements could not be resolved
CTF crypto (I) some simple encoding and encryption
Use of better scroll
Svn error command revert error previous operation has not finished; run ‘ cleanup‘ if
MySQL index details
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
Clickhouse stand alone installation
A new paradigm for large model application: unified feature representation optimization (UFO)
Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start