ROS2 通信三大件之动作 -- Action

server/2024/10/18 1:03:25/

通信最后一个,也是不太容易理解的方式action,复杂且重要

1、创建action数据结构

创建工作空间和模块就不多说了

在模块  src/action_moudle/action/Counter.action  下创建文件 Counter.action

int32 target  # Goal: 目标 
---
int32 current_value  # Result: 结果
---
int32[] sequence  # Feedback: 中间状态反馈

需要注意的是 这几个值不要搞混,结果和中间状态容易搞错

添加依赖:src/action_moudle/package.xml

  <depend>rclcpp_action</depend> <depend>rosidl_default_generators</depend><depend>rosidl_default_runtime</depend><member_of_group>rosidl_interface_packages</member_of_group>

src/action_moudle/CMakeLists.txt

find_package(rosidl_default_generators REQUIRED)
find_package(rclcpp_action REQUIRED)# 设置消息和服务文件路径
set(action_FILES"action/Counter.action"
)# 添加消息和服务生成目标
rosidl_generate_interfaces(${PROJECT_NAME}${action_FILES}DEPENDENCIES std_msgs
)# 安装 Action 文件
install(DIRECTORY action/DESTINATION share/${PROJECT_NAME}/action
)

编译后生成install/action_moudle/include/action_moudle/action_moudle/action/counter.hpp

c6a140fc2d6a41b99d5792586c679cf9.png

至此action文件生成

2、编写Count计时的Action服务端和客户端代码

AI生成的代码靠不住,改了很多才编译过

src/action_moudle/src/action_server.cpp

#include "rclcpp/rclcpp.hpp"
#include "action_moudle/action/counter.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
#include <memory>
#include <vector>using Counter = action_moudle::action::Counter;
using namespace std::placeholders;class CounterActionServer : public rclcpp::Node
{
public:CounterActionServer() : Node("counter_action_server"){action_server_ =rclcpp_action::create_server<Counter>(this, "counter", std::bind(&CounterActionServer::handle_goal, this, _1, _2),std::bind(&CounterActionServer::handle_cancel, this, _1), std::bind(&CounterActionServer::handle_accepted, this, _1));}private:rclcpp_action::Server<Counter>::SharedPtr action_server_;rclcpp_action::GoalResponse handle_goal(const rclcpp_action::GoalUUID& uuid, std::shared_ptr<const Counter::Goal> goal){(void)uuid;RCLCPP_INFO(this->get_logger(), "Received goal request with target: %ld", goal->target);return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;}rclcpp_action::CancelResponse handle_cancel(const std::shared_ptr<rclcpp_action::ServerGoalHandle<Counter>> goal_handle){RCLCPP_INFO(this->get_logger(), "Received request to cancel goal");return rclcpp_action::CancelResponse::ACCEPT;}void handle_accepted(const std::shared_ptr<rclcpp_action::ServerGoalHandle<Counter>> goal_handle){using namespace std::placeholders;std::thread{std::bind(&CounterActionServer::execute, this, _1), goal_handle}.detach();}void execute(const std::shared_ptr<rclcpp_action::ServerGoalHandle<Counter>> goal_handle){RCLCPP_INFO(this->get_logger(), "Executing goal");rclcpp::Rate loop_rate(1);const auto   goal     = goal_handle->get_goal();auto         feedback = std::make_shared<Counter::Feedback>();auto         result   = std::make_shared<Counter::Result>();result->current_value = 0;feedback->sequence.push_back(0);if (goal_handle->is_canceling()){goal_handle->canceled(result);RCLCPP_INFO(this->get_logger(), "Goal canceled");return;}goal_handle->publish_feedback(feedback);for (int i = 1; i <= goal->target; ++i){result->current_value = i;feedback->sequence.push_back(i);if (goal_handle->is_canceling()){goal_handle->canceled(result);RCLCPP_INFO(this->get_logger(), "Goal canceled");return;}goal_handle->publish_feedback(feedback);loop_rate.sleep();}goal_handle->succeed(result);RCLCPP_INFO(this->get_logger(), "Returning result");}
};int main(int argc, char* argv[])
{rclcpp::init(argc, argv);rclcpp::spin(std::make_shared<CounterActionServer>());rclcpp::shutdown();return 0;
}

src/action_moudle/src/action_client.cpp

#include "rclcpp/rclcpp.hpp"
#include "action_moudle/action/counter.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
#include <memory>using Counter = action_moudle::action::Counter;
using namespace std::placeholders;class CounterActionClient : public rclcpp::Node
{
public:CounterActionClient() : Node("counter_action_client"){action_client_ = rclcpp_action::create_client<Counter>(this, "counter");while (!action_client_->wait_for_action_server()){if (!rclcpp::ok()){RCLCPP_ERROR(this->get_logger(), "Interrupted while waiting for the action server. Exiting.");return;}RCLCPP_INFO(this->get_logger(), "Action server not available, waiting again...");}auto goal   = Counter::Goal();goal.target = 10;auto send_goal_options                   = rclcpp_action::Client<Counter>::SendGoalOptions();send_goal_options.goal_response_callback = std::bind(&CounterActionClient::goal_response_callback, this, std::placeholders::_1);send_goal_options.feedback_callback      = std::bind(&CounterActionClient::feedback_callback, this, std::placeholders::_1, std::placeholders::_2);send_goal_options.result_callback        = std::bind(&CounterActionClient::result_callback, this, std::placeholders::_1);action_client_->async_send_goal(goal, send_goal_options);}private:rclcpp_action::Client<Counter>::SharedPtr action_client_;void goal_response_callback(rclcpp_action::ClientGoalHandle<Counter>::SharedPtr goal_handle){if (!goal_handle){RCLCPP_INFO(this->get_logger(), "Goal rejected");}else{RCLCPP_INFO(this->get_logger(), "Goal accepted");}}// void goal_response_callback(std::shared_future<std::shared_ptr<rclcpp_action::ClientGoalHandle<Counter>>> future)// {//     auto goal_handle = future.get();//     if (!goal_handle)//     {//         RCLCPP_INFO(this->get_logger(), "Goal rejected");//     }//     else//     {//         RCLCPP_INFO(this->get_logger(), "Goal accepted");//     }// }void feedback_callback(std::shared_ptr<rclcpp_action::ClientGoalHandle<Counter>>, const std::shared_ptr<const Counter::Feedback> feedback){// RCLCPP_INFO(this->get_logger(), "Current value: %ld", feedback->current_value);RCLCPP_INFO(this->get_logger(), "Sequence value: %s", print_sequence(feedback->sequence).c_str());}void result_callback(const rclcpp_action::ClientGoalHandle<Counter>::WrappedResult& result){switch (result.code){case rclcpp_action::ResultCode::SUCCEEDED:RCLCPP_INFO(this->get_logger(), "Result: %d", result.result->current_value);break;case rclcpp_action::ResultCode::ABORTED:RCLCPP_ERROR(this->get_logger(), "Goal was aborted");break;case rclcpp_action::ResultCode::CANCELED:RCLCPP_ERROR(this->get_logger(), "Goal was canceled");break;default:RCLCPP_ERROR(this->get_logger(), "Unknown result code");break;}rclcpp::shutdown();}std::string print_sequence(const std::vector<int32_t>& sequence){std::stringstream ss;ss << "[";for (size_t i = 0; i < sequence.size(); ++i){if (i > 0){ss << ", ";}ss << sequence[i];}ss << "]";return ss.str();}
};int main(int argc, char* argv[])
{rclcpp::init(argc, argv);rclcpp::spin(std::make_shared<CounterActionClient>());rclcpp::shutdown();return 0;
}

src/action_moudle/CMakeLists.txt  添加

find_package(action_moudle REQUIRED)
find_package(rclcpp_action REQUIRED)# 创建可执行文件
add_executable(action_server src/action_server.cpp)
ament_target_dependencies(action_server rclcpp std_msgs action_moudle rclcpp_action)add_executable(action_client src/action_client.cpp)
ament_target_dependencies(action_client rclcpp std_msgs action_moudle rclcpp_action)# 安装目标
install(TARGETSaction_server action_clientDESTINATION lib/${PROJECT_NAME}
)

3、编译后测试

edaa2982375c40b588702640e4975cf9.png

 


http://www.ppmy.cn/server/131338.html

相关文章

Python知识点:基于Python工具,如何使用Seq2Seq进行机器翻译

开篇&#xff0c;先说一个好消息&#xff0c;截止到2025年1月1日前&#xff0c;翻到文末找到我&#xff0c;赠送定制版的开题报告和任务书&#xff0c;先到先得&#xff01;过期不候&#xff01; 如何使用Python工具进行Seq2Seq机器翻译 概述 Seq2Seq&#xff08;Sequence-to…

无心剑七绝《泊院雕楼》

七绝泊院雕楼 清歌咏尽桂花香 泊院雕楼醉夕阳 逸兴无端飞万里 幽情宛转忆潇湘 2024年10月13日 平水韵七阳平韵 这首七绝《泊院雕楼》以清新脱俗的语言&#xff0c;描绘了一幅宁静致远的画面。 首句“清歌咏尽桂花香”&#xff0c;以“清歌”起兴&#xff0c;形象地描绘了桂花香…

利用Spring Boot构建高效B2B医疗病历平台

第1章绪论 计算机已经从科研院所&#xff0c;大中型企业&#xff0c;走进了平常百姓家&#xff0c;Internet遍及世界各地&#xff0c;在网上能够用计算机进行文字草拟、修改、打印清样、文件登陆、检索、综合统计、分类、数据库管理等&#xff0c;用科学的方法将无序的信息进行…

linux 中mysql my.cnf 配置模版

前置准备 sudo systemctl stop mysqld 注意&#xff1a; 原本配置重命名做备份 备份数据 删文件 直接新建 my.cnf 把配置 11要粘进去的内容 直接粘进去 注意&#xff1a;尽管log-bin 和 log_bin 都可以启用二进制日志&#xff0c;但为了保持与现代MySQL版本的兼容性和一…

Tomcat常用配置和调优

文章目录 1 Tomcat常用配置 1.1 修改端口号 1.2 配置为域名访问 1.3 设置字符编码 1.4 调整连接超时 1.5 管理用户权限 1.6 配置JDK路径 2 Tomcat优化 前面文章介绍了如何快速安装Tomcat,接下来将对Tomcat的常用配置和调优等操作进行详细讲解。 安装部署Tomcat 1 Tomcat常…

Telnet命令详解:安装、用法及应用场景解析

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐&#xff1a;「storm…

MBP - HomeBrew的使用教程

HomeBrew简介 Homebrew 由开发者 Max Howell 开发&#xff0c;并基于 BSD 开源&#xff0c;是一个非常方便的包管理器工具。在早期&#xff0c; Homebrew 仅有 macOS 的版本&#xff0c;后续随着用户的增多&#xff0c;Homebrew 还提供了 Linux 的版本&#xff0c;帮助开发者在…

我们是如何将Docker构建时间缩短40%的

by: WL Mapmost从设计之初&#xff0c;便选择了云原生道路&#xff0c;在软件开发过程中自然也少不了容器化技术的使用。当然&#xff0c;我们也为Mapmost产品中使用的所有组件构建了 docker 镜像。然而&#xff0c;随着时间的推移&#xff0c;其中一些镜像变得越来越大&#…