ROS2--动作通信

ROS2动作通信详解

1️⃣ 核心概念

动作(Action)是ROS2中用于长时间运行任务的通信机制,包含三个核心部分:

  • Goal:客户端发送的任务目标(如"移动到坐标(5,3)")
  • Feedback:执行过程中的进度反馈(如"当前已移动3米")
  • Result:任务完成后的最终结果(如"成功到达目标")

2️⃣ 与服务/话题的区别

特性 服务(Service) 话题(Topic) 动作(Action)
通信模式 同步请求-响应 异步发布-订阅 异步目标-反馈-结果
适用场景 快速完成的简单操作 持续数据流 长时间运行、需要进度反馈的任务
典型应用 开关传感器 传感器数据流 导航任务、机械臂控制
是否支持取消 ✔️

3️⃣ 动作文件定义

创建.action文件定义通信结构(示例:MoveRobot.action):

action 复制代码
# Goal定义
geometry_msgs/Point target
---
# Result定义
bool success
string message
---
# Feedback定义
float32 progress
string status

4️⃣ 通信流程

5️⃣ 服务端代码示例

python 复制代码
import rclpy
from action_msgs.msg import GoalStatus
from rclpy.action import ActionServer

class MoveRobotServer(Node):
    def __init__(self):
        super().__init__('move_robot_server')
        self._action_server = ActionServer(
            self,
            MoveRobot,
            'move_robot',
            self.execute_callback)
    
    async def execute_callback(self, goal_handle):
        # 执行任务
        while not done:
            # 发送反馈
            feedback_msg = MoveRobot.Feedback()
            feedback_msg.progress = current_progress
            goal_handle.publish_feedback(feedback_msg)
            
        # 返回结果
        goal_handle.succeed()
        result = MoveRobot.Result()
        result.success = True
        return result

6️⃣ 客户端代码示例

python 复制代码
from rclpy.action import ActionClient

class MoveRobotClient(Node):
    def __init__(self):
        super().__init__('move_robot_client')
        self._action_client = ActionClient(self, MoveRobot, 'move_robot')
    
    def send_goal(self, target):
        goal_msg = MoveRobot.Goal()
        goal_msg.target = target
        
        self._action_client.wait_for_server()
        self._send_goal_future = self._action_client.send_goal_async(
            goal_msg,
            feedback_callback=self.feedback_callback)
        
        self._send_goal_future.add_done_callback(self.goal_response_callback)
    
    def feedback_callback(self, feedback_msg):
        print(f'当前进度: {feedback_msg.progress}%')
    
    def goal_response_callback(self, future):
        goal_handle = future.result()
        if not goal_handle.accepted:
            return
            
        self._get_result_future = goal_handle.get_result_async()
        self._get_result_future.add_done_callback(self.get_result_callback)
    
    def get_result_callback(self, future):
        result = future.result().result
        print(f'任务完成! 结果: {result.message}')

7️⃣ 常用命令行工具

bash 复制代码
# 查看可用动作列表
ros2 action list

# 查看动作类型
ros2 action info /move_robot

# 手动发送目标
ros2 action send_goal /move_robot MoveRobot "{target: {x: 5.0, y: 3.0}}"

8️⃣ 适用场景建议

✅ 使用动作的时机:

  • 需要超过1秒的长时间任务
  • 需要持续进度反馈
  • 需要支持任务取消
  • 需要处理可能的执行失败

❌ 不使用动作的情况:

  • 瞬时完成的简单请求(改用服务)
  • 单向数据流(改用话题)
  • 需要实时性极高的通信(考虑自定义消息类型)

生命中真正重要的不是你遭遇了什么,而是你记住了哪些事,又是如何铭记的。 ---加西亚·马尔克斯-\

相关推荐
flashlight_hi13 分钟前
LeetCode 分类刷题:2563. 统计公平数对的数目
python·算法·leetcode
java1234_小锋14 分钟前
Scikit-learn Python机器学习 - 特征预处理 - 归一化 (Normalization):MinMaxScaler
python·机器学习·scikit-learn
雨中散步撒哈拉22 分钟前
13、做中学 | 初一下期 Golang数组与切片
开发语言·后端·golang
0wioiw023 分钟前
Go基础(③Cobra)
开发语言·后端·golang
星空的资源小屋26 分钟前
网易UU远程,免费电脑远程控制软件
人工智能·python·pdf·电脑
楼田莉子31 分钟前
C++算法专题学习:栈相关的算法
开发语言·c++·算法·leetcode
IMER SIMPLE33 分钟前
人工智能-python-深度学习-神经网络-MobileNet V1&V2
人工智能·python·深度学习
晨非辰36 分钟前
#C语言——刷题攻略:牛客编程入门训练(九):攻克 分支控制(三)、循环控制(一),轻松拿捏!
c语言·开发语言·经验分享·学习方法·visual studio
_oP_i43 分钟前
Java 服务接口中解决跨域(CORS,Cross-Origin Resource Sharing)问题
java·开发语言
eleqi1 小时前
Python+DRVT 从外部调用 Revit:批量创建楼板
python·系统集成·revit·外部调用·drvt·自动化生产流水线