File contents SKILL — ROS2 Action 模式
ROS2 Action 异步任务接口。三要素:Goal(目标)/ Feedback(反馈)/ Result(结果)。
核心概念
要素
类型
说明
Goal
Action.Goal
客户端发送的任务目标
Feedback
Action.Feedback
服务端实时推送的进度
Result
Action.Result
任务完成时的最终结果
Action Server 模式
// 创建 Server
action_server_ = rclcpp_action::create_server<Action>(
this, "action_name",
handle_goal, // 验证并接受/拒绝 Goal
handle_cancel, // 处理取消请求
handle_accepted // 启动后台执行
);
// Goal 验证:返回 ACCEPT_AND_EXECUTE / REJECT
rclcpp_action::GoalResponse handle_goal(goal_uuid, goal);
// Cancel 响应:返回 ACCEPT / REJECT
rclcpp_action::CancelResponse handle_cancel(goal_handle);
// 实际执行:在后台线程运行
void execute(std::shared_ptr<GoalHandle> goal_handle) {
// 循环:检查取消 + 计算 + 发布反馈
goal_handle->publish_feedback(feedback);
goal_handle->succeed(result); // 或 canceled() / aborted()
}
注意事项
后台线程 :handle_accepted 中用 std::thread.detach() 执行,避免阻塞 spin
取消检测 :is_canceling() 必须在循环中定期检查
超时处理 :设置 goal 响应超时,避免客户端无限等待
常用命令
ros2 action list # 列出所有 Action
ros2 action info /fibonacci # 查看 Action 类型
ros2 action send_goal /fibonacci example_interfaces/action/Fibonacci "{order: 5}"
ros2 action send_goal /fibonacci ... --feedback # 带反馈
ros2 action cancel /fibonacci # 取消 Goal
1 --- 2 name: action-fibonacci-demo 3 description: SKILL — ROS2 Action 模式 4 --- 5 # SKILL — ROS2 Action 模式 6 7 > ROS2 Action 异步任务接口。三要素:Goal(目标)/ Feedback(反馈)/ Result(结果)。 8 9 ## 核心概念 10 11 | 要素 | 类型 | 说明 | 12 |------|------|------| 13 | Goal | Action.Goal | 客户端发送的任务目标 | 14 | Feedback | Action.Feedback | 服务端实时推送的进度 | 15 | Result | Action.Result | 任务完成时的最终结果 | 16 17 ## Action Server 模式 18 19 ```cpp 20 // 创建 Server 21 action_server_ = rclcpp_action::create_server<Action>( 22 this, "action_name", 23 handle_goal, // 验证并接受/拒绝 Goal 24 handle_cancel, // 处理取消请求 25 handle_accepted // 启动后台执行 26 ); 27 28 // Goal 验证:返回 ACCEPT_AND_EXECUTE / REJECT 29 rclcpp_action::GoalResponse handle_goal(goal_uuid, goal); 30 31 // Cancel 响应:返回 ACCEPT / REJECT 32 rclcpp_action::CancelResponse handle_cancel(goal_handle); 33 34 // 实际执行:在后台线程运行 35 void execute(std::shared_ptr<GoalHandle> goal_handle) { 36 // 循环:检查取消 + 计算 + 发布反馈 37 goal_handle->publish_feedback(feedback); 38 goal_handle->succeed(result); // 或 canceled() / aborted() 39 } 40 ``` 41 42 ## 注意事项 43 44 1. **后台线程**:`handle_accepted` 中用 `std::thread.detach()` 执行,避免阻塞 spin 45 2. **取消检测**:`is_canceling()` 必须在循环中定期检查 46 3. **超时处理**:设置 goal 响应超时,避免客户端无限等待 47 48 ## 常用命令 49 50 ```bash 51 ros2 action list # 列出所有 Action 52 ros2 action info /fibonacci # 查看 Action 类型 53 ros2 action send_goal /fibonacci example_interfaces/action/Fibonacci "{order: 5}" 54 ros2 action send_goal /fibonacci ... --feedback # 带反馈 55 ros2 action cancel /fibonacci # 取消 Goal 56 ```
miuav/vibe-coding-ros2/tree/main/examples/mcp-workflow/cases/action-fibonacci-demo commit d34b89dc63
Frequently asked questions How do I install the Action Fibonacci Demo skill? Run npx skillmds@latest add miuav/action-fibonacci-demo in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Action Fibonacci Demo skill do? SKILL — ROS2 Action 模式 It is listed under AI & ML on SkillMD.
Is Action Fibonacci Demo safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Action Fibonacci Demo? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Action Fibonacci Demo free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Action Fibonacci Demo? miuav (@miuav) published this skill. Their other Agent Skills are listed on their SkillMD profile.