当前位置:网站首页>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!"
边栏推荐
- Jenkins introduces custom jars
- [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
- What are the advantages and risks of paper gold investment
- 1.21 learning summary
- Minecraft 1.16.5 生化8 模组 1.9版本 1.18版本同步
- Create alicloud test instances
- PHP inherited in class return does not work
- Install dbeaver and connect Clickhouse
- Tips for using idea
- Construction of art NFT trading platform | NFT mall
猜你喜欢
1.24 learning summary
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
Minecraft 1.16.5 biochemical 8 module 1.9 version 1.18 version synchronization
0622-马棕榈跌9%
There is no response to redirection and jump in the laravel constructor [original]
[Qunhui] this suite requires you to start PgSQL adapter service
Multipass中文文档-远程使用Multipass
2021/11/6-burpsuit packet capturing and web page source code modification
随机推荐
CTF crypto (I) some simple encoding and encryption
Simple personal summary of tp6 multi application deployment -- Part I [original]
Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
Guide de la pompe de données Oracle
Nabicat connection: local MySQL & cloud service MySQL and error reporting
PHP installation SSH2 extension
Yapi cross domain request plug-in installation
How to carry out word-of-mouth marketing for enterprises' products and services? Can word of mouth marketing be done on behalf of others?
Zhimeng CMS will file a lawsuit against infringing websites
The statistics in the MySQL field become strings, and then they are converted into numbers for sorting
2022 talent strategic transformation under the development trend of digital economy
35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
1.18 learning summary
[Qunhui] this suite requires you to start PgSQL adapter service
LISP programming language
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
Thinkphp6 using kindeditor
2021-01-31
OSS CDN alicloud configuration method
LeetCode 94. Middle order traversal of binary tree