惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Lucky帅小武

AI学习入门,超清晰AI知识图谱整理及核心概念解析 基于SSM的在线考试系统毕业设计论文【范文】 基于SSM的仓库进销存系统毕业设计论文【范文】 基于SSM的在线外卖订餐系统毕业设计论文【范文】 基于SSM的酒店管理系统毕业设计论文【范文】 基于SSM的校园论坛网站系统毕业设计论文【范文】 ROS2笔记6--ROS2常用命令工具 ROS2笔记5--动作通讯 ROS2笔记3--话题通讯 ROS2笔记2--工作空间、功能包、节点 ROS2笔记1--简介及开发环境搭建 基于SSM的网上商城系统毕业设计论文【范文】 软考笔记(1)--操作系统 软考笔记(9)--计算机组成原理4--总线系统 软考笔记(9)--计算机组成原理3--处理器 软考笔记(9)--计算机组成原理2--指令系统 软考笔记(8)--多媒体技术 软考笔记(7)--数据库关系代数 软考笔记(5)--系统安全性与保密性设计
ROS2笔记4--服务通讯
Lucky帅小武 · 2024-04-19 · via 博客园 - Lucky帅小武

ROS2中话题通讯可以实现多个ROS节点之间数据的单向传输,不过话题通讯是一种异步通信机制,发布者无法准确知道订阅者是否收到消息。而服务通信是一种基于请求响应的通信模型,在通信双方中客户端发送请求数据到服务端,服务端响应结果给客户端。

0

从服务实现机制看这种形式是CS模型,客户端需要数据时针对具体的服务发送请求信息,服务端收到请求后进行处理并返回应答信息。

以加法计算器为例

1、新建功能包

ros2 pkg create pkg_service --build-type ament_python --dependencies rclpy --node-name server_demo

2、服务端实现

编写服务端server_demo.py文件实现服务端功能

import rclpy
from rclpy.node import Node
from example_interfaces.srv import AddTwoInts

class Service_Server(Node):
    def __init__(self, name):
        super().__init__(name)
# 创建一个服务端,使用的是create_service函数
# 参数分别为:服务数据类型、服务名称、服务回调函数 self.src = self.create_service(AddTwoInts, "/add_two_ints", self.AddTwoInts_callback)
# 定义服务回调函数 def AddTwoInts_callback(self, request, response): response.sum = request.a + request.b print("result.sum = ", response.sum) return response def main(): rclpy.init() server_demo = Service_Server("server_node") rclpy.spin(server_demo) server_demo.destroy_node() rclpy.shutdown()

服务回调函数AddTwoInts_callback这里需要传入参数除了self,还有requesst和response,request是服务需要的参数,response是服务返回的结果。request.a和request.b是request部分的内容,response.sum是response部分的内容。

AddTwoInts是类型,可以通过如下命令查看类型

 
ros2 interface show example_interfaces/srv/AddTwoInts

其中“--”上面部分是request,下面部分是response。request包含两个变量a,b;response包含变量sum

3、客户端实现

在server_demo.py统计目录下创建文件client_demo.py,编写客户端功能代码:

import rclpy
from rclpy.node import Node
from example_interfaces.srv import AddTwoInts

class Service_Client(Node):
    def __init__(self, name):
        super().__init__(name)
# 创建客户端,使用create_client函数
# 传入参数分别是:服务数据类型、服务名称 self.client = self.create_client(AddTwoInts, "/add_two_ints")
# 循环等待服务端启动 while not self.client.wait_for_service(timeout_sec=1.0): print("service not available, waiting...")
# 创建服务请求数据对象 self.request = AddTwoInts.Request() def send_request(self): self.request.a = 10 self.request.b = 20
# 发送服务请求 self.future = self.client.call_async(self.request) def main(): rclpy.init() client_demo = Service_Client("client_node") client_demo.send_request() # 发送服务请求 while rclpy.ok(): rclpy.spin_once(client_demo)
# 判断数据是否处理完成 if client_demo.future.done(): try:
# 获取服务返回结果 response = client_demo.future.result() print("request.a = ", client_demo.request.a) print("request.b = ", client_demo.request.b) print("response.sum = ", response.sum) except Exception as e: print("Service call failed") break client_demo.destroy_node() rclpy.shutdown()

4、编辑配置文件、编译工作空间

配置文件setup.py

0

编译工作空间

cd ~/ros_ws colcon build --package-selects pkg_service source install/setup.bash 

5、运行程序

分终端分别启动服务端节点和客户端节点

ros2 run pkg_service server_demo
ros2 run pkg_service client_demo

服务端打印:

0

客户端打印:

0