{"id":153,"date":"2021-11-21T12:44:56","date_gmt":"2021-11-21T12:44:56","guid":{"rendered":"http:\/\/47.103.123.166\/?p=153"},"modified":"2021-11-21T12:44:57","modified_gmt":"2021-11-21T12:44:57","slug":"ros2_foxy%e5%ad%a6%e4%b9%a06-%e8%bf%9b%e9%98%b6%e7%bc%96%e7%a8%8b_c","status":"publish","type":"post","link":"http:\/\/47.103.123.166\/?p=153","title":{"rendered":"ROS2_Foxy\u5b66\u4e606\u2014\u2014\u8fdb\u9636\u7f16\u7a0b_C++"},"content":{"rendered":"<p>\u91cc\u9762\u7684\u4f8b\u5b50\u53c2\u8003<a href=\"https:\/\/github.com\/ros2\/ros2_documentation\">\u5b98\u65b9\u6559\u7a0b<\/a>\uff0c\u7136\u540e\u9644\u5e26\u4e00\u4e9b\u89e3\u91ca\u548c\u4e00\u4e9b\u63a8\u8350\u7684\u4fbf\u4e8e\u7406\u89e3\u7684\u6587\u7ae0\u3002<\/p>\n<h1>1 \u7f16\u5199 action<\/h1>\n<p>\u65b0\u5efa\u5de5\u4f5c\u7a7a\u95f4\uff0c\u5e76\u65b0\u5efa\u4e24\u4e2a\u529f\u80fd\u5305\uff1a\u52a8\u4f5c\u6d88\u606f\u529f\u80fd\u5305\u548c\u52a8\u4f5c\u529f\u80fd\u5305\u3002\u52a8\u4f5c\u6d88\u606f\u529f\u80fd\u5305\u4e2d\u7f16\u5199action\u6587\u4ef6\uff0c\u7136\u540e\u5728\u52a8\u4f5c\u529f\u80fd\u5305\u4e2d\u4f7f\u7528\u524d\u8005\u7684action\u63a5\u53e3\u3002<\/p>\n<h2>1.1 \u81ea\u5b9a\u4e49\u6d88\u606f action<\/h2>\n<p>1\u3001\u521b\u5efa\uff1a\u5728\u52a8\u4f5c\u6d88\u606f\u529f\u80fd\u5305\uff08\u8fd9\u91cc\u5305\u540d\u662faction_tutorials_interfaces\uff09\u4e0b\uff0c\u521b\u5efa\/action\u6587\u4ef6\u5939\uff08\u4e0e\/src\u540c\u7ea7\uff09\uff0c\u5e76\u5728\u5176\u4e2d\u5efa\u7acb.action\u6d88\u606f\u6587\u4ef6\uff0c\u6709\u5173\u6d88\u606f\u5185\u5bb9\uff0c\u8be6\u89c1\u672c\u7cfb\u5217\u7b2c\u4e09\u7bc7<a href=\"https:\/\/blog.csdn.net\/fangfang12138\/article\/details\/111593629\">\u300aROS2_Foxy\u5b66\u4e60\uff08\u4e09\uff09\u6838\u5fc3\u6982\u5ff5\u300b<\/a>\u3002<\/p>\n<p><strong>Fibonacci.action<\/strong><\/p>\n<pre><code class=\"language-cpp\">int32 order\n---\nint32[] sequence\n---\nint32[] partial_sequence<\/code><\/pre>\n<p>2\u3001\u5728package.xml\u6587\u4ef6\u4e2d\u6dfb\u52a0<\/p>\n<pre><code class=\"language-xml\">    &lt;buildtool_depend&gt;rosidl_default_generators&lt;\/buildtool_depend&gt;\n    &lt;depend&gt;action_msgs&lt;\/depend&gt;\n    &lt;member_of_group&gt;rosidl_interface_packages&lt;\/member_of_group&gt;<\/code><\/pre>\n<p>\u6ce8\uff1aaction_msgs\u5fc5\u8981\u3002<br \/>\n3\u3001\u5728CMakeLists.txt\u6587\u4ef6\u4e2d\u6dfb\u52a0<\/p>\n<pre><code class=\"language-bash\">find_package(rosidl_default_generators REQUIRED)\n\nrosidl_generate_interfaces(${PROJECT_NAME}\n  \"action\/Fibonacci.action\"\n)<\/code><\/pre>\n<p>4\u3001\u6d88\u606f\u6587\u4ef6\u6d4b\u8bd5<br \/>\n\u529f\u80fd\u5305\u7f16\u8bd1\u540e\uff08\u6ca1\u6709\u8282\u70b9\uff0c\u6ca1\u6709\u8fd0\u884c\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4ros2 interface show\u6765\u67e5\u770b\u6d88\u606f\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-powershell\">$ ros2 interface show action_tutorials_interfaces\/action\/Fibonacci<\/code><\/pre>\n<hr \/>\n<p><strong>\u6ce8<\/strong> \uff1a\u4e0b\u9762\u521b\u5efa\u7684\u52a8\u4f5c\u529f\u80fd\u5305\u540d\u662faction_tutorials_cpp\uff0c\u9644\u5e26\u4ee5\u4e0b\u4f9d\u8d56\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5c31\u662f\u521a\u521a\u521b\u5efa\u7684\u81ea\u5b9a\u4e49\u6d88\u606f\u529f\u80fd\u5305\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u5305\u65f6\uff0c\u901a\u8fc7\u53c2\u6570dependencies\u6dfb\u52a0\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u4fee\u6539package.xml\uff08&lt; depend &gt;\uff09\u548cCMakeLists.txt\uff08find_package\uff09\u3002<\/p>\n<pre><code class=\"language-powershell\">action_tutorials_interfaces \nrclcpp \nrclcpp_action \nrclcpp_components<\/code><\/pre>\n<h2>1.2 \u52a8\u4f5c\u670d\u52a1\u7aef action server<\/h2>\n<p>\u5728\u52a8\u4f5c\u529f\u80fd\u5305\u7684\/src\u76ee\u5f55\u4e0b\uff0c\u7f16\u5199\u52a8\u4f5c\u670d\u52a1\u7aef\u8282\u70b9\uff0cfibonacci_action_server.cpp<\/p>\n<pre><code class=\"language-cpp\">#include &lt;functional&gt;\n#include &lt;memory&gt;\n#include &lt;thread&gt;\n\n#include \"action_tutorials_interfaces\/action\/fibonacci.hpp\"\n#include \"rclcpp\/rclcpp.hpp\"\n#include \"rclcpp_action\/rclcpp_action.hpp\"\n#include \"rclcpp_components\/register_node_macro.hpp\"\n\n\/\/#include \"action_tutorials_cpp\/visibility_control.h\"\n\nnamespace action_tutorials_cpp\n{\nclass FibonacciActionServer : public rclcpp::Node\n{\npublic:\n    using Fibonacci = action_tutorials_interfaces::action::Fibonacci;\n    using GoalHandleFibonacci = rclcpp_action::ServerGoalHandle&lt;Fibonacci&gt;;\n\n    \/\/\u6784\u9020\u51fd\u6570\uff1a\n    \/\/1\u3001\u521d\u59cb\u5316\u52a8\u4f5c\u670d\u52a1\u8282\u70b9\u540d fibonacci_action_server\n    \/\/2\u3001\u521d\u59cb\u5316\u52a8\u4f5c\u670d\u52a1\u5668\uff1a\u6d88\u606f\u7c7b\u578bFibonacci\uff0c\u6240\u5c5e\u8282\u70b9this\uff0c\u52a8\u4f5c\u540dfibonacci\uff0c\u56de\u8c03\u51fd\u6570\n    explicit FibonacciActionServer(const rclcpp::NodeOptions &amp; options = rclcpp::NodeOptions()) : Node(\"fibonacci_action_server\", options)\n    {\n    using namespace std::placeholders;\n\n    this-&gt;action_server_ = rclcpp_action::create_server&lt;Fibonacci&gt;( \/\/\u6d88\u606f\u7c7b\u578bFibonacci\n    this,                                   \/\/\u6240\u5c5e\u8282\u70b9this\uff0c\n    \"fibonacci\",                                \/\/\u52a8\u4f5c\u540dfibonacci\n    std::bind(&amp;FibonacciActionServer::handle_goal, this, _1, _2),   \/\/\u56de\u8c03\u51fd\u6570\uff1ahandle_goal      \u63a5\u53d7\u76ee\u6807\n    std::bind(&amp;FibonacciActionServer::handle_cancel, this, _1),     \/\/\u56de\u8c03\u51fd\u6570\uff1ahandle_cancel    \u63a5\u53d7\u76ee\u6807\u53d6\u6d88\n    std::bind(&amp;FibonacciActionServer::handle_accepted, this, _1));  \/\/\u56de\u8c03\u51fd\u6570\uff1ahandle_accepted  \u63a5\u53d7\u65b0\u76ee\u6807\u5e76\u5904\u7406\n    }\n\nprivate:\n    rclcpp_action::Server&lt;Fibonacci&gt;::SharedPtr action_server_;\n\n    \/\/\u63a5\u53d7\u76ee\u6807\uff0c\u540c\u65f6\u544a\u77e5client\uff0c\u8fd9\u8fb9\u77e5\u6653\u76ee\u6807\u4e86\uff0c\u544a\u77e5client\u7684\u8fc7\u7a0b\u88ab\u81ea\u52a8\u5904\u7406\u4e86\uff0c\u4e0b\u540c\n    rclcpp_action::GoalResponse handle_goal(const rclcpp_action::GoalUUID &amp; uuid, std::shared_ptr&lt;const Fibonacci::Goal&gt; goal)\n    {\n        RCLCPP_INFO(this-&gt;get_logger(), \"Received goal request with order %d\", goal-&gt;order);\n        (void)uuid;\n        return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;\n    }\n\n    \/\/\u63a5\u53d7\u76ee\u6807\u53d6\u6d88\uff0c\u540c\u65f6\u544a\u77e5client\uff0c\u8fd9\u8fb9\u77e5\u9053\u53d6\u6d88\u4e86\n    rclcpp_action::CancelResponse handle_cancel(const std::shared_ptr&lt;GoalHandleFibonacci&gt; goal_handle)\n    {\n        RCLCPP_INFO(this-&gt;get_logger(), \"Received request to cancel goal\");\n        (void)goal_handle;\n        return rclcpp_action::CancelResponse::ACCEPT;\n    }\n\n    \/\/\u63a5\u53d7\u65b0\u76ee\u6807\u5e76\u5904\u7406\n    void handle_accepted(const std::shared_ptr&lt;GoalHandleFibonacci&gt; goal_handle)\n    {\n        using namespace std::placeholders;\n\n        \/\/\u56e0\u4e3a\u5904\u7406\u8fc7\u7a0b\u662f\u4e00\u4e2a\u8017\u65f6\u4e14\u957f\u671f\u8fd0\u884c\u7684\u64cd\u4f5c\uff0c\u56e0\u6b64\u5c06\u5176\u79fb\u5230\u53e6\u4e00\u7ebf\u7a0b\u5904\u7406\n        std::thread{std::bind(&amp;FibonacciActionServer::execute, this, _1), goal_handle}.detach();\n    }\n\n    \/\/\u5904\u7406\u5e76\u53cd\u9988\n    void execute(const std::shared_ptr&lt;GoalHandleFibonacci&gt; goal_handle)\n    {\n        RCLCPP_INFO(this-&gt;get_logger(), \"Executing goal\");\n\n        \/\/\u5904\u7406\u9891\u7387 1Hz\n        rclcpp::Rate loop_rate(1);\n\n        \/\/\u83b7\u53d6\u76ee\u6807\n        const auto goal = goal_handle-&gt;get_goal();\n\n        \/\/\u5b9a\u4e49\u53cd\u9988\n        auto feedback = std::make_shared&lt;Fibonacci::Feedback&gt;();\n        auto &amp; sequence = feedback-&gt;partial_sequence;\n        sequence.push_back(0);  \/\/\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u524d\u4e24\u4e2a\u503c\u662f 0 1  \u52a8\u4f5c\u670d\u52a1\u5668\u5c06\u4e0d\u65ad\u53cd\u9988\u5f53\u524d\u7684sequence\u5230\u5ba2\u6237\u7aef\n        sequence.push_back(1);\n\n        \/\/\u5b9a\u4e49\u7ed3\u679c\n        auto result = std::make_shared&lt;Fibonacci::Result&gt;();\n\n        \/\/\u8ba1\u7b97\n        for (int i = 1; (i &lt; goal-&gt;order) &amp;&amp; rclcpp::ok(); ++i) \n        {\n            \/\/\u68c0\u67e5\u76ee\u6807\u662f\u5426\u88ab\u53d6\u6d88\n            if (goal_handle-&gt;is_canceling()) \n            {\n                result-&gt;sequence = sequence;    \n                goal_handle-&gt;canceled(result);  \n                RCLCPP_INFO(this-&gt;get_logger(), \"Goal canceled\");\n                return;\n            }\n\n            \/\/\u66f4\u65b0sequence\n            sequence.push_back(sequence[i] + sequence[i - 1]);\n\n            \/\/\u53cd\u9988sequence\n            goal_handle-&gt;publish_feedback(feedback);\n            RCLCPP_INFO(this-&gt;get_logger(), \"Publish feedback\");\n\n            loop_rate.sleep();\n        }\n\n        \/\/\u76ee\u6807\u8fbe\u6210\n        if (rclcpp::ok()) \n        {\n            result-&gt;sequence = sequence;\n            goal_handle-&gt;succeed(result);\n            RCLCPP_INFO(this-&gt;get_logger(), \"Goal succeeded\");\n        }\n    }\n};  \/\/ class FibonacciActionServer\n\n}  \/\/ namespace action_tutorials_cpp\n\nRCLCPP_COMPONENTS_REGISTER_NODE(action_tutorials_cpp::FibonacciActionServer)<\/code><\/pre>\n<p><strong>\u6ce8<\/strong>\uff1a<br \/>\n<strong>1\u3001goal_handle<\/strong>\uff1a\u5173\u4e8e\u76ee\u6807\u53e5\u67c4goal_handle\uff08\u4e5f\u5c31\u662frclcpp_action::ServerGoalHandle\uff09\uff0c\u53ef\u4ee5\u770b\u4e0b\u5b98\u65b9\u7684<a href=\"http:\/\/docs.ros2.org\/latest\/api\/rclcpp_action\/classrclcpp__action_1_1ServerGoalHandle.html\">\u5e2e\u52a9\u6587\u6863<\/a>\uff0c\u8fd9\u91cc\u521a\u5b66\uff0c\u8fd8\u4e0d\u662f\u5f88\u7406\u89e3\uff0c\u5199\u591a\u4e86\u4e5f\u8bb8\u5c31\u660e\u767d\u4e86\u3002<br \/>\n<strong>2\u3001std::thread<\/strong>\uff1a\u770b<a href=\"https:\/\/blog.csdn.net\/oyoung_2012\/article\/details\/78958274\">\u8fd9\u91cc<\/a>\uff0c\u6709\u5173join\u548cdetach\u53ef\u4ee5\u77a7\u4e00\u77a7<a href=\"https:\/\/www.cnblogs.com\/zhanghu52030\/p\/9166526.html\">\u8fd9\u91cc<\/a>\u3002<br \/>\n3\u3001\u8fd9\u91cc\u6ca1\u6709\u4f7f\u7528main\u51fd\u6570\uff0c\u800c\u662f\u7528\u4e0b\u9762\u8fd9\u53e5\uff0c\u8868\u660e\u8be5\u8282\u70b9\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u52a0\u8f7d\uff0c\u770b\u770b\u5b98\u65b9<a href=\"http:\/\/docs.ros2.org\/latest\/api\/rclcpp_components\/\">\u5e2e\u52a9\u6587\u6863<\/a>\uff0c\u8fd9\u91cc\u4e5f\u4e0d\u662f\u5f88\u7406\u89e3\uff0c\u540e\u9762<strong>\u518d\u5b66\u4e60<\/strong>\u3002<\/p>\n<pre><code class=\"language-cpp\">RCLCPP_COMPONENTS_REGISTER_NODE(action_tutorials_cpp::FibonacciActionServer)<\/code><\/pre>\n<h2>1.3 \u52a8\u4f5c\u5ba2\u6237\u7aef action client<\/h2>\n<p>\u5728\u52a8\u4f5c\u529f\u80fd\u5305\u7684\/src\u76ee\u5f55\u4e0b\uff0c\u7f16\u5199\u52a8\u4f5c\u5ba2\u6237\u7aef\u8282\u70b9\uff0cfibonacci_action_client.cpp<\/p>\n<pre><code class=\"language-cpp\">#include &lt;functional&gt;\n#include &lt;future&gt;   \/\/C++11 \u5f02\u6b65\u901a\u4fe1\n#include &lt;memory&gt;\n#include &lt;string&gt;\n#include &lt;sstream&gt;  \/\/C++\u6807\u51c6\u5e93\u4e2d sstream \u63d0\u4f9b\u4e86\u6bd4ANSI C\u7684 stdio \u66f4\u9ad8\u7ea7\u7684\u4e00\u4e9b\u529f\u80fd\uff0c\u5373\u5355\u7eaf\u6027\u3001\u7c7b\u578b\u5b89\u5168\u548c\u53ef\u6269\u5c55\u6027\u3002\n\n#include \"action_tutorials_interfaces\/action\/fibonacci.hpp\"\n\n#include \"rclcpp\/rclcpp.hpp\"\n#include \"rclcpp_action\/rclcpp_action.hpp\"\n#include \"rclcpp_components\/register_node_macro.hpp\"\n\nnamespace action_tutorials_cpp\n{\nclass FibonacciActionClient : public rclcpp::Node\n{\npublic:\n    using Fibonacci = action_tutorials_interfaces::action::Fibonacci;\n    using GoalHandleFibonacci = rclcpp_action::ClientGoalHandle&lt;Fibonacci&gt;;\n\n    \/\/\u6784\u9020\u51fd\u6570\uff1a\n    \/\/1\u3001\u521d\u59cb\u5316\u52a8\u4f5c\u5ba2\u6237\u7aef\u8282\u70b9\u540d fibonacci_action_client\n    \/\/2\u3001\u521d\u59cb\u5316\u52a8\u4f5c\u5ba2\u6237\u7aef\uff1a\u52a8\u4f5c\u7c7b\u578bFibonacci\u3001\u6240\u5c5e\u8282\u70b9this\u3001\u52a8\u4f5c\u540dfibonacci\n    \/\/3\u3001\u521d\u59cb\u5316\u5b9a\u65f6\u5668\uff0c\u6bcf500ms\u53d1\u9001\u4e00\u6b21\u76ee\u6807\n    explicit FibonacciActionClient(const rclcpp::NodeOptions &amp; options) : Node(\"fibonacci_action_client\", options)\n    {\n        this-&gt;client_ptr_ = rclcpp_action::create_client&lt;Fibonacci&gt;(\n        this,\n        \"fibonacci\");\n\n        this-&gt;timer_ = this-&gt;create_wall_timer(std::chrono::milliseconds(500), std::bind(&amp;FibonacciActionClient::send_goal, this));\n    }\n\n    \/\/\u5b9a\u65f6\u5668\u4e2d\u65ad\u51fd\u6570\n    void send_goal()\n    {\n        using namespace std::placeholders;\n\n        \/\/\u53d6\u6d88\u4e86\u5b9a\u65f6\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b9a\u65f6\u5668\u4e2d\u65ad\u51fd\u6570\u53ea\u6267\u884c\u4e00\u6b21\n        this-&gt;timer_-&gt;cancel();\n\n        \/\/\u7136\u540e\uff0c\u770b\u770b\u6709\u6ca1\u6709\u5728\u7ebf\u7684\u540c\u540d\u52a8\u4f5c\u670d\u52a1\u5668\n        if (!this-&gt;client_ptr_-&gt;wait_for_action_server()) \n        {\n            RCLCPP_ERROR(this-&gt;get_logger(), \"Action server not available after waiting\");\n            rclcpp::shutdown();\n        }\n\n        \/\/\u5b9a\u4e49\u76ee\u6807\n        auto goal_msg = Fibonacci::Goal();\n        goal_msg.order = 10;\n\n        RCLCPP_INFO(this-&gt;get_logger(), \"Sending goal\");\n\n        \/\/\u786e\u5b9a\u56de\u8c03\u51fd\u6570\uff1a\u8bf7\u6c42\u540e\u7684\u54cd\u5e94\u3001\u53cd\u9988\u7684\u5904\u7406\u3001\u7ed3\u679c\u7684\u63a5\u6536\n        auto send_goal_options = rclcpp_action::Client&lt;Fibonacci&gt;::SendGoalOptions();\n\n        send_goal_options.goal_response_callback =\n        std::bind(&amp;FibonacciActionClient::goal_response_callback, this, _1);\n\n        send_goal_options.feedback_callback =\n        std::bind(&amp;FibonacciActionClient::feedback_callback, this, _1, _2);\n\n        send_goal_options.result_callback =\n        std::bind(&amp;FibonacciActionClient::result_callback, this, _1);\n\n        \/\/\u53d1\u9001\u76ee\u6807\u7ed9\u52a8\u4f5c\u670d\u52a1\u5668\n        this-&gt;client_ptr_-&gt;async_send_goal(goal_msg, send_goal_options);\n    }\n\nprivate:\n    rclcpp_action::Client&lt;Fibonacci&gt;::SharedPtr client_ptr_;\n    rclcpp::TimerBase::SharedPtr timer_;\n\n    \/\/\u8bf7\u6c42\u540e\u7684\u54cd\u5e94\n    void goal_response_callback(std::shared_future&lt;GoalHandleFibonacci::SharedPtr&gt; future)\n    {\n        auto goal_handle = future.get();\n        if (!goal_handle) \n        {\n            RCLCPP_ERROR(this-&gt;get_logger(), \"Goal was rejected by server\");\n        } \n        else \n        {\n            RCLCPP_INFO(this-&gt;get_logger(), \"Goal accepted by server, waiting for result\");\n        }\n    }\n\n    \/\/\u53cd\u9988\u7684\u5904\u7406\n    void feedback_callback(GoalHandleFibonacci::SharedPtr, const std::shared_ptr&lt;const Fibonacci::Feedback&gt; feedback)\n    {\n        std::stringstream ss;\n        ss &lt;&lt; \"Next number in sequence received: \";\n        for (auto number : feedback-&gt;partial_sequence) \n        {\n            ss &lt;&lt; number &lt;&lt; \" \";\n        }\n        RCLCPP_INFO(this-&gt;get_logger(), ss.str().c_str());\n    }\n\n    \/\/\u7ed3\u679c\u7684\u63a5\u6536\n    void result_callback(const GoalHandleFibonacci::WrappedResult &amp; result)\n    {\n        switch (result.code) \n        {\n            case rclcpp_action::ResultCode::SUCCEEDED:  break;\n            case rclcpp_action::ResultCode::ABORTED:    RCLCPP_ERROR(this-&gt;get_logger(), \"Goal was aborted\");   return;\n            case rclcpp_action::ResultCode::CANCELED:   RCLCPP_ERROR(this-&gt;get_logger(), \"Goal was canceled\");  return;\n            default:                    RCLCPP_ERROR(this-&gt;get_logger(), \"Unknown result code\");    return;\n        }\n        std::stringstream ss;\n        ss &lt;&lt; \"Result received: \";\n        for (auto number : result.result-&gt;sequence) \n        {\n            ss &lt;&lt; number &lt;&lt; \" \";\n        }\n        RCLCPP_INFO(this-&gt;get_logger(), ss.str().c_str());\n        rclcpp::shutdown();\n    }\n};  \/\/ class FibonacciActionClient\n\n}  \/\/ namespace action_tutorials_cpp\n\nRCLCPP_COMPONENTS_REGISTER_NODE(action_tutorials_cpp::FibonacciActionClient)<\/code><\/pre>\n<p><strong>\u6ce8 std::future<\/strong> \uff1a\u770b<a href=\"https:\/\/blog.csdn.net\/c_base_jin\/article\/details\/89761718\">\u8fd9\u91cc<\/a>\u3002std::thread\u662fC++11\u4e2d\u63d0\u4f9b\u5f02\u6b65\u521b\u5efa\u591a\u7ebf\u7a0b\u7684\u5de5\u5177\uff0c\u4f46\u60f3\u8981\u4ece\u7ebf\u7a0b\u4e2d\u8fd4\u56de\u5f02\u6b65\u4efb\u52a1\u7ed3\u679c\uff0c\u4e00\u822c\u9700\u8981\u4f9d\u9760\u5168\u5c40\u53d8\u91cf\uff0c\u4ece\u5b89\u5168\u89d2\u5ea6\u770b\uff0c\u6709\u4e9b\u4e0d\u59a5\uff0c\u4e3a\u6b64C++11\u63d0\u4f9b\u4e86std::future\u7c7b\u6a21\u677f\uff0cfuture\u5bf9\u8c61\u63d0\u4f9b\u8bbf\u95ee\u5f02\u6b65\u64cd\u4f5c\u7ed3\u679c\u7684\u673a\u5236\uff0c\u5f88\u8f7b\u677e\u89e3\u51b3\u4ece\u5f02\u6b65\u4efb\u52a1\u4e2d\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<h2>1.4 \u4fee\u6539CMakelists.txt<\/h2>\n<p>\u5728CMakelists.txt\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\u3002<\/p>\n<pre><code class=\"language-bash\"># \u8fd9\u91cc\u662f\u751f\u6210\u5171\u4eab\u5e93\nadd_library(action_server SHARED src\/fibonacci_action_server.cpp)\nadd_library(action_client SHARED src\/fibonacci_action_client.cpp)\n\ntarget_include_directories(action_server PRIVATE\n  $&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}\/include&gt;\n  $&lt;INSTALL_INTERFACE:include&gt;)\ntarget_include_directories(action_client PRIVATE\n  $&lt;BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}\/include&gt;\n  $&lt;INSTALL_INTERFACE:include&gt;)\n\ntarget_compile_definitions(action_server PRIVATE \"ACTION_TUTORIALS_CPP_BUILDING_DLL\")\ntarget_compile_definitions(action_client PRIVATE \"ACTION_TUTORIALS_CPP_BUILDING_DLL\")\n\nament_target_dependencies(action_server\n  \"action_tutorials_interfaces\"\n  \"rclcpp\"\n  \"rclcpp_action\"\n  \"rclcpp_components\")\nament_target_dependencies(action_client\n  \"action_tutorials_interfaces\"\n  \"rclcpp\"\n  \"rclcpp_action\"\n  \"rclcpp_components\")\n\nrclcpp_components_register_node(action_server PLUGIN \"action_tutorials_cpp::FibonacciActionServer\" EXECUTABLE fibonacci_action_server)\nrclcpp_components_register_node(action_client PLUGIN \"action_tutorials_cpp::FibonacciActionClient\" EXECUTABLE fibonacci_action_client)\n\ninstall(TARGETS\n  action_server\n  action_client\n  ARCHIVE DESTINATION lib\n  LIBRARY DESTINATION lib\n  RUNTIME DESTINATION bin)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u91cc\u9762\u7684\u4f8b\u5b50\u53c2\u8003\u5b98\u65b9\u6559\u7a0b\uff0c\u7136\u540e\u9644\u5e26\u4e00\u4e9b\u89e3\u91ca\u548c\u4e00\u4e9b\u63a8\u8350\u7684\u4fbf\u4e8e\u7406\u89e3\u7684\u6587\u7ae0\u3002 1 \u7f16\u5199 action \u65b0\u5efa\u5de5\u4f5c\u7a7a\u95f4\uff0c\u5e76\u65b0\u5efa\u4e24\u4e2a\u529f\u80fd\u5305\uff1a\u52a8\u4f5c\u6d88\u606f\u529f\u80fd\u5305\u548c\u52a8\u4f5c\u529f\u80fd\u5305\u3002\u52a8\u4f5c\u6d88\u606f\u529f\u80fd\u5305\u4e2d\u7f16\u5199action\u6587\u4ef6\uff0c\u7136\u540e\u5728\u52a8\u4f5c\u529f\u80fd\u5305\u4e2d\u4f7f\u7528\u524d\u8005\u7684action\u63a5\u53e3\u3002 1.1 \u81ea\u5b9a\u4e49\u6d88\u606f action 1\u3001\u521b\u5efa\uff1a\u5728\u52a8\u4f5c\u6d88\u606f\u529f\u80fd\u5305\uff08\u8fd9\u91cc\u5305\u540d\u662faction_tutorials_interfaces\uff09\u4e0b\uff0c\u521b\u5efa\/action\u6587\u4ef6\u5939\uff08\u4e0e\/src\u540c\u7ea7\uff09\uff0c\u5e76\u5728\u5176\u4e2d\u5efa\u7acb.action\u6d88\u606f\u6587\u4ef6\uff0c\u6709\u5173\u6d88\u606f\u5185\u5bb9\uff0c\u8be6\u89c1\u672c\u7cfb\u5217\u7b2c\u4e09\u7bc7\u300aROS2_Foxy\u5b66\u4e60\uff08\u4e09\uff09\u6838\u5fc3\u6982\u5ff5\u300b\u3002 Fibonacci.action int32 order &#8212; int32[] sequence &#8212; int32[] partial_sequence 2\u3001\u5728package.xml\u6587\u4ef6\u4e2d\u6dfb\u52a0 &lt;buildtool_depend&gt;rosidl_default_generators&lt;\/buildtool_depend&gt; &lt;depend&gt;action_msgs&lt;\/depend&gt; &lt;member_of_group&gt;rosidl_interface_packages&lt;\/member_of_group&gt; \u6ce8\uff1aaction_msgs\u5fc5\u8981\u3002 3\u3001\u5728CMakeLists.txt\u6587\u4ef6\u4e2d\u6dfb\u52a0 find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} &#8220;action\/Fibonacci.action&#8221; ) 4\u3001\u6d88\u606f\u6587\u4ef6\u6d4b\u8bd5 \u529f\u80fd\u5305\u7f16\u8bd1\u540e\uff08\u6ca1\u6709\u8282\u70b9\uff0c\u6ca1\u6709\u8fd0\u884c\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4ros2 interface show\u6765\u67e5\u770b\u6d88\u606f\u6587\u4ef6\u3002 $ ros2 interface show action_tutorials_interfaces\/action\/Fibonacci \u6ce8 \uff1a\u4e0b\u9762\u521b\u5efa\u7684\u52a8\u4f5c\u529f\u80fd\u5305\u540d\u662faction_tutorials_cpp\uff0c\u9644\u5e26\u4ee5\u4e0b\u4f9d\u8d56\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5c31\u662f\u521a\u521a\u521b\u5efa\u7684\u81ea\u5b9a\u4e49\u6d88\u606f\u529f\u80fd\u5305\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u5305\u65f6\uff0c\u901a\u8fc7\u53c2\u6570dependencies\u6dfb\u52a0\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u4fee\u6539package.xml\uff08&lt; depend &gt;\uff09\u548cCMakeLists.txt\uff08find_package\uff09\u3002 action_tutorials_interfaces rclcpp rclcpp_action rclcpp_components 1.2 \u52a8\u4f5c\u670d\u52a1\u7aef action server \u5728\u52a8\u4f5c\u529f\u80fd\u5305\u7684\/src\u76ee\u5f55\u4e0b\uff0c\u7f16\u5199\u52a8\u4f5c\u670d\u52a1\u7aef\u8282\u70b9\uff0cfibonacci_action_server.cpp #include &lt;functional&gt; #include &lt;memory&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts\/153"}],"collection":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":1,"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":154,"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/154"}],"wp:attachment":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}