使用机器人操作系统ROS 2和仿真软件Gazebo 9行动进阶实战(九)- mobot区域巡逻

举报
zhangrelay 发表于 2021/07/15 03:29:53 2021/07/15
【摘要】 行动(action)比服务更为灵活和复杂。在给出行动具体说明之前,先简要复习一下: 主题-服务-行动: 场合   具体细节   服务/行动对比 从上面可以非常明显的看出,服务和行动的差异。 自动驾驶mobot 那么实践任务如下: 用行动实现第8讲中,第三种服务的功能...

行动(action)比服务更为灵活和复杂。在给出行动具体说明之前,先简要复习一下:

主题-服务-行动:

 

 

从上面可以非常明显的看出,服务和行动的差异。

那么实践任务如下:

  • 用行动实现第8讲中,第三种服务的功能,单目标点多参数;
  • 用行动实现mobot在室内环境各房间的巡逻,多目标点多参数;
  • 用行动实现mobot在室外跑道的巡逻,多目标点多参数不确定。

由此,需要融合OpenAI,OpenCV和BT。从机器人主题,服务过渡到行为。

然而行为的组合,不同状态行为的切换,构成了新的挑战,进一步学习:

熟练掌握相应算法进一步可扩展:

  • 物流机器人(点点轨迹,任务调度)
  • 清扫机器人(区域覆盖,协作协同)

行为(action)基础复习:

行为服务器端:

  • 非推荐,ros1代码风格

  
  1. #include <inttypes.h>
  2. #include <memory>
  3. #include "example_interfaces/action/fibonacci.hpp"
  4. #include "rclcpp/rclcpp.hpp"
  5. // TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
  6. #include "rclcpp_action/rclcpp_action.hpp"
  7. using Fibonacci = example_interfaces::action::Fibonacci;
  8. using GoalHandleFibonacci = rclcpp_action::ServerGoalHandle<Fibonacci>;
  9. rclcpp_action::GoalResponse handle_goal(
  10. const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const Fibonacci::Goal> goal)
  11. {
  12. RCLCPP_INFO(rclcpp::get_logger("server"), "Got goal request with order %d", goal->order);
  13. (void)uuid;
  14. // Let's reject sequences that are over 9000
  15. if (goal->order > 9000) {
  16. return rclcpp_action::GoalResponse::REJECT;
  17. }
  18. return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
  19. }
  20. rclcpp_action::CancelResponse handle_cancel(
  21. const std::shared_ptr<GoalHandleFibonacci> goal_handle)
  22. {
  23. RCLCPP_INFO(rclcpp::get_logger("server"), "Got request to cancel goal");
  24. (void)goal_handle;
  25. return rclcpp_action::CancelResponse::ACCEPT;
  26. }
  27. void execute(
  28. const std::shared_ptr<GoalHandleFibonacci> goal_handle)
  29. {
  30. RCLCPP_INFO(rclcpp::get_logger("server"), "Executing goal");
  31. rclcpp::Rate loop_rate(1);
  32. const auto goal = goal_handle->get_goal();
  33. auto feedback = std::make_shared<Fibonacci::Feedback>();
  34. auto & sequence = feedback->sequence;
  35. sequence.push_back(0);
  36. sequence.push_back(1);
  37. auto result = std::make_shared<Fibonacci::Result>();
  38. for (int i = 1; (i < goal->order) && rclcpp::ok(); ++i) {
  39. // Check if there is a cancel request
  40. if (goal_handle->is_canceling()) {
  41. result->sequence = sequence;
  42. goal_handle->canceled(result);
  43. RCLCPP_INFO(rclcpp::get_logger("server"), "Goal Canceled");
  44. return;
  45. }
  46. // Update sequence
  47. sequence.push_back(sequence[i] + sequence[i - 1]);
  48. // Publish feedback
  49. goal_handle->publish_feedback(feedback);
  50. RCLCPP_INFO(rclcpp::get_logger("server"), "Publish Feedback");
  51. loop_rate.sleep();
  52. }
  53. // Check if goal is done
  54. if (rclcpp::ok()) {
  55. result->sequence = sequence;
  56. goal_handle->succeed(result);
  57. RCLCPP_INFO(rclcpp::get_logger("server"), "Goal Succeeded");
  58. }
  59. }
  60. void handle_accepted(const std::shared_ptr<GoalHandleFibonacci> goal_handle)
  61. {
  62. // this needs to return quickly to avoid blocking the executor, so spin up a new thread
  63. std::thread{execute, goal_handle}.detach();
  64. }
  65. int main(int argc, char ** argv)
  66. {
  67. rclcpp::init(argc, argv);
  68. auto node = rclcpp::Node::make_shared("minimal_action_server");
  69. // Create an action server with three callbacks
  70. // 'handle_goal' and 'handle_cancel' are called by the Executor (rclcpp::spin)
  71. // 'execute' is called whenever 'handle_goal' returns by accepting a goal
  72. // Calls to 'execute' are made in an available thread from a pool of four.
  73. auto action_server = rclcpp_action::create_server<Fibonacci>(
  74. node,
  75. "fibonacci",
  76. handle_goal,
  77. handle_cancel,
  78. handle_accepted);
  79. rclcpp::spin(node);
  80. rclcpp::shutdown();
  81. return 0;
  82. }
  • 推荐,ros2新风格

  
  1. #include <inttypes.h>
  2. #include <memory>
  3. #include "example_interfaces/action/fibonacci.hpp"
  4. #include "rclcpp/rclcpp.hpp"
  5. // TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
  6. #include "rclcpp_action/rclcpp_action.hpp"
  7. class MinimalActionServer : public rclcpp::Node
  8. {
  9. public:
  10. using Fibonacci = example_interfaces::action::Fibonacci;
  11. using GoalHandleFibonacci = rclcpp_action::ServerGoalHandle<Fibonacci>;
  12. explicit MinimalActionServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions())
  13. : Node("minimal_action_server", options)
  14. {
  15. using namespace std::placeholders;
  16. this->action_server_ = rclcpp_action::create_server<Fibonacci>(
  17. this->get_node_base_interface(),
  18. this->get_node_clock_interface(),
  19. this->get_node_logging_interface(),
  20. this->get_node_waitables_interface(),
  21. "fibonacci",
  22. std::bind(&MinimalActionServer::handle_goal, this, _1, _2),
  23. std::bind(&MinimalActionServer::handle_cancel, this, _1),
  24. std::bind(&MinimalActionServer::handle_accepted, this, _1));
  25. }
  26. private:
  27. rclcpp_action::Server<Fibonacci>::SharedPtr action_server_;
  28. rclcpp_action::GoalResponse handle_goal(
  29. const rclcpp_action::GoalUUID & uuid,
  30. std::shared_ptr<const Fibonacci::Goal> goal)
  31. {
  32. RCLCPP_INFO(this->get_logger(), "Received goal request with order %d", goal->order);
  33. (void)uuid;
  34. // Let's reject sequences that are over 9000
  35. if (goal->order > 9000) {
  36. return rclcpp_action::GoalResponse::REJECT;
  37. }
  38. return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
  39. }
  40. rclcpp_action::CancelResponse handle_cancel(
  41. const std::shared_ptr<GoalHandleFibonacci> goal_handle)
  42. {
  43. RCLCPP_INFO(this->get_logger(), "Received request to cancel goal");
  44. (void)goal_handle;
  45. return rclcpp_action::CancelResponse::ACCEPT;
  46. }
  47. void execute(const std::shared_ptr<GoalHandleFibonacci> goal_handle)
  48. {
  49. RCLCPP_INFO(this->get_logger(), "Executing goal");
  50. rclcpp::Rate loop_rate(1);
  51. const auto goal = goal_handle->get_goal();
  52. auto feedback = std::make_shared<Fibonacci::Feedback>();
  53. auto & sequence = feedback->sequence;
  54. sequence.push_back(0);
  55. sequence.push_back(1);
  56. auto result = std::make_shared<Fibonacci::Result>();
  57. for (int i = 1; (i < goal->order) && rclcpp::ok(); ++i) {
  58. // Check if there is a cancel request
  59. if (goal_handle->is_canceling()) {
  60. result->sequence = sequence;
  61. goal_handle->canceled(result);
  62. RCLCPP_INFO(this->get_logger(), "Goal Canceled");
  63. return;
  64. }
  65. // Update sequence
  66. sequence.push_back(sequence[i] + sequence[i - 1]);
  67. // Publish feedback
  68. goal_handle->publish_feedback(feedback);
  69. RCLCPP_INFO(this->get_logger(), "Publish Feedback");
  70. loop_rate.sleep();
  71. }
  72. // Check if goal is done
  73. if (rclcpp::ok()) {
  74. result->sequence = sequence;
  75. goal_handle->succeed(result);
  76. RCLCPP_INFO(this->get_logger(), "Goal Suceeded");
  77. }
  78. }
  79. void handle_accepted(const std::shared_ptr<GoalHandleFibonacci> goal_handle)
  80. {
  81. using namespace std::placeholders;
  82. // this needs to return quickly to avoid blocking the executor, so spin up a new thread
  83. std::thread{std::bind(&MinimalActionServer::execute, this, _1), goal_handle}.detach();
  84. }
  85. }; // class MinimalActionServer
  86. int main(int argc, char ** argv)
  87. {
  88. rclcpp::init(argc, argv);
  89. auto action_server = std::make_shared<MinimalActionServer>();
  90. rclcpp::spin(action_server);
  91. rclcpp::shutdown();
  92. return 0;
  93. }

行为客户端:

  • 非推荐,ros1风格

  
  1. #include <inttypes.h>
  2. #include <memory>
  3. #include "example_interfaces/action/fibonacci.hpp"
  4. #include "rclcpp/rclcpp.hpp"
  5. // TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
  6. #include "rclcpp_action/rclcpp_action.hpp"
  7. using Fibonacci = example_interfaces::action::Fibonacci;
  8. int main(int argc, char ** argv)
  9. {
  10. rclcpp::init(argc, argv);
  11. auto node = rclcpp::Node::make_shared("minimal_action_client");
  12. auto action_client = rclcpp_action::create_client<Fibonacci>(node, "fibonacci");
  13. if (!action_client->wait_for_action_server(std::chrono::seconds(20))) {
  14. RCLCPP_ERROR(node->get_logger(), "Action server not available after waiting");
  15. return 1;
  16. }
  17. // Populate a goal
  18. auto goal_msg = Fibonacci::Goal();
  19. goal_msg.order = 10;
  20. RCLCPP_INFO(node->get_logger(), "Sending goal");
  21. // Ask server to achieve some goal and wait until it's accepted
  22. auto goal_handle_future = action_client->async_send_goal(goal_msg);
  23. if (rclcpp::spin_until_future_complete(node, goal_handle_future) !=
  24. rclcpp::executor::FutureReturnCode::SUCCESS)
  25. {
  26. RCLCPP_ERROR(node->get_logger(), "send goal call failed :(");
  27. return 1;
  28. }
  29. rclcpp_action::ClientGoalHandle<Fibonacci>::SharedPtr goal_handle = goal_handle_future.get();
  30. if (!goal_handle) {
  31. RCLCPP_ERROR(node->get_logger(), "Goal was rejected by server");
  32. return 1;
  33. }
  34. // Wait for the server to be done with the goal
  35. auto result_future = goal_handle->async_result();
  36. RCLCPP_INFO(node->get_logger(), "Waiting for result");
  37. if (rclcpp::spin_until_future_complete(node, result_future) !=
  38. rclcpp::executor::FutureReturnCode::SUCCESS)
  39. {
  40. RCLCPP_ERROR(node->get_logger(), "get result call failed :(");
  41. return 1;
  42. }
  43. rclcpp_action::ClientGoalHandle<Fibonacci>::WrappedResult wrapped_result = result_future.get();
  44. switch (wrapped_result.code) {
  45. case rclcpp_action::ResultCode::SUCCEEDED:
  46. break;
  47. case rclcpp_action::ResultCode::ABORTED:
  48. RCLCPP_ERROR(node->get_logger(), "Goal was aborted");
  49. return 1;
  50. case rclcpp_action::ResultCode::CANCELED:
  51. RCLCPP_ERROR(node->get_logger(), "Goal was canceled");
  52. return 1;
  53. default:
  54. RCLCPP_ERROR(node->get_logger(), "Unknown result code");
  55. return 1;
  56. }
  57. RCLCPP_INFO(node->get_logger(), "result received");
  58. for (auto number : wrapped_result.result->sequence) {
  59. RCLCPP_INFO(node->get_logger(), "%" PRId64, number);
  60. }
  • 推荐:ros2风格

  
  1. #include <inttypes.h>
  2. #include <memory>
  3. #include <string>
  4. #include <iostream>
  5. #include "example_interfaces/action/fibonacci.hpp"
  6. #include "rclcpp/rclcpp.hpp"
  7. // TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
  8. #include "rclcpp_action/rclcpp_action.hpp"
  9. class MinimalActionClient : public rclcpp::Node
  10. {
  11. public:
  12. using Fibonacci = example_interfaces::action::Fibonacci;
  13. using GoalHandleFibonacci = rclcpp_action::ClientGoalHandle<Fibonacci>;
  14. explicit MinimalActionClient(const rclcpp::NodeOptions & node_options = rclcpp::NodeOptions())
  15. : Node("minimal_action_client", node_options), goal_done_(false)
  16. {
  17. this->client_ptr_ = rclcpp_action::create_client<Fibonacci>(
  18. this->get_node_base_interface(),
  19. this->get_node_graph_interface(),
  20. this->get_node_logging_interface(),
  21. this->get_node_waitables_interface(),
  22. "fibonacci");
  23. this->timer_ = this->create_wall_timer(
  24. std::chrono::milliseconds(500),
  25. std::bind(&MinimalActionClient::send_goal, this));
  26. }
  27. bool is_goal_done() const
  28. {
  29. return this->goal_done_;
  30. }
  31. void send_goal()
  32. {
  33. using namespace std::placeholders;
  34. this->timer_->cancel();
  35. this->goal_done_ = false;
  36. if (!this->client_ptr_) {
  37. RCLCPP_ERROR(this->get_logger(), "Action client not initialized");
  38. }
  39. if (!this->client_ptr_->wait_for_action_server(std::chrono::seconds(10))) {
  40. RCLCPP_ERROR(this->get_logger(), "Action server not available after waiting");
  41. this->goal_done_ = true;
  42. return;
  43. }
  44. auto goal_msg = Fibonacci::Goal();
  45. goal_msg.order = 10;
  46. RCLCPP_INFO(this->get_logger(), "Sending goal");
  47. auto send_goal_options = rclcpp_action::Client<Fibonacci>::SendGoalOptions();
  48. send_goal_options.goal_response_callback =
  49. std::bind(&MinimalActionClient::goal_response_callback, this, _1);
  50. send_goal_options.feedback_callback =
  51. std::bind(&MinimalActionClient::feedback_callback, this, _1, _2);
  52. send_goal_options.result_callback =
  53. std::bind(&MinimalActionClient::result_callback, this, _1);
  54. auto goal_handle_future = this->client_ptr_->async_send_goal(goal_msg, send_goal_options);
  55. }
  56. private:
  57. rclcpp_action::Client<Fibonacci>::SharedPtr client_ptr_;
  58. rclcpp::TimerBase::SharedPtr timer_;
  59. bool goal_done_;
  60. void goal_response_callback(std::shared_future<GoalHandleFibonacci::SharedPtr> future)
  61. {
  62. auto goal_handle = future.get();
  63. if (!goal_handle) {
  64. RCLCPP_ERROR(this->get_logger(), "Goal was rejected by server");
  65. } else {
  66. RCLCPP_INFO(this->get_logger(), "Goal accepted by server, waiting for result");
  67. }
  68. }
  69. void feedback_callback(
  70. GoalHandleFibonacci::SharedPtr,
  71. const std::shared_ptr<const Fibonacci::Feedback> feedback)
  72. {
  73. RCLCPP_INFO(
  74. this->get_logger(),
  75. "Next number in sequence received: %" PRId64,
  76. feedback->sequence.back());
  77. }
  78. void result_callback(const GoalHandleFibonacci::WrappedResult & result)
  79. {
  80. this->goal_done_ = true;
  81. switch (result.code) {
  82. case rclcpp_action::ResultCode::SUCCEEDED:
  83. break;
  84. case rclcpp_action::ResultCode::ABORTED:
  85. RCLCPP_ERROR(this->get_logger(), "Goal was aborted");
  86. return;
  87. case rclcpp_action::ResultCode::CANCELED:
  88. RCLCPP_ERROR(this->get_logger(), "Goal was canceled");
  89. return;
  90. default:
  91. RCLCPP_ERROR(this->get_logger(), "Unknown result code");
  92. return;
  93. }
  94. RCLCPP_INFO(this->get_logger(), "Result received");
  95. for (auto number : result.result->sequence) {
  96. RCLCPP_INFO(this->get_logger(), "%" PRId64, number);
  97. }
  98. }
  99. }; // class MinimalActionClient
  100. int main(int argc, char ** argv)
  101. {
  102. rclcpp::init(argc, argv);
  103. auto action_client = std::make_shared<MinimalActionClient>();
  104. while (!action_client->is_goal_done()) {
  105. rclcpp::spin_some(action_client);
  106. }
  107. rclcpp::shutdown();
  108. return 0;
  109. }

 

 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/106228468

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。