当前位置:   article > 正文

ROS2 callback回调不成功问题_rclcpp::spin_until_future_complete

rclcpp::spin_until_future_complete

结论:

ROS2 的 node中使用的如果node不添加到spin中,则timer、service、action等的callback函数无法正常回调。

但是如果在service、action调用之后,调用如下函数则可正常回调。原因是spin_until_future_complete函数内部会创建一个executor,添加nh_这个Node

rclcpp::spin_until_future_complete(this->get_node_base_interface(), future_goal_handle, std::chrono::seconds(5));


文章目录
1.node的构成
2.executor
3.出错的程序
        3.1 node_main.cpp,正常执行
        3.2 照抄官方文档中client 的response报错
1.node的构成
rclcpp::Node的成员变量有一些interface,例如

node_base:可以记录node的基本状态,例如:
        node的name;
        node的call_back_group
        node的associated_with_executor_,即是否与executor绑定,以此确定只跟一个executor有关系
node_graph
node_services
node_topics:显而易见用来创建publisher,subscriber等
2.executor
用来在node 调用spin(内部循环)时,执行node内部的callback函数。
一个executor可以添加多个node,及为不同的node执行callback函数
executor分singleThreadExecutor和multiThreadExecutor。即单线程执行和多线程执行。
singleThreadExecutor添加多个node时,顺序执行call_back
multiThreadExecutor添加多个node时,多线程执行call_back
一个node只能被一个executor添加,
在executor.add(node)函数中会检查
node是否已经被其他executor添加(即查看node.node_base.associated_with_executor_是否为true)
或node是否已被此executor添加过
3.出错的程序
3.1 node_main.cpp,正常执行

  1. rclcpp::init(argc, argv);
  2. rclcpp::executors::SingleThreadedExecutor executor;
  3. auto node = std::make_shared<robotis::localization::LocalizationNode>(rclcpp::NodeOptions());
  4. executor.add_node(node);
  5. std::atomic_bool & has_executor = node->get_node_base_interface()->get_associated_with_executor_atomic();
  6. //此时会成功输出,因为上面的executor已成功添加node
  7. if (has_executor.exchange(true)) {
  8. printf("yes yes yes yes yes yes\n");
  9. }
  10. executor.spin();
  11. rclcpp::shutdown();
  12. return EXIT_SUCCESS;


3.2 照抄官方文档中client 的response报错
我的service定义

  1. string data
  2. ---
  3. Int32 num


接收到一个随便的std_msgs::msg::Int32数据后调用mySubCallback函数,在其中call service

  1. void mySubCallback(const std_msgs::msg::Int32::SharedPtr msg){
  2.     printf("recieve a msg:%d\n",msg->data);
  3.     std::string data =std::to_string(pose_msg->data);
  4.     auto request = std::make_shared<my_msgs::srv::MyService::Request>();
  5.     request->data = data;
  6.     while (!my_client_->wait_for_service(std::chrono::seconds(1))) {
  7.         if (!rclcpp::ok()) {
  8.             RCLCPP_ERROR(nh_->get_logger(), "Interrupted while waiting for the service. Exiting.");
  9.             return;
  10.         }
  11.         RCLCPP_INFO(nh_->get_logger(), "service not available, waiting again...");
  12.     }
  13.     auto result = my_client_->async_send_request(request);
  14.     //以下报错
  15.     if (rclcpp::spin_until_future_complete(nh_, result) == rclcpp::executor::FutureReturnCode::SUCCESS){
  16.         int num= int(result.get()->num);
  17.         printf("result:num:%d\n",num);
  18.     }
  19.     else {
  20.         RCLCPP_ERROR(nh_->get_logger(), "Failed to call service");
  21.     }
  22. }



执行到rclcpp::spin_until_future_complete(nh_, result)时会报错"Node has already been added to an executor"。原因是spin_until_future_complete函数内部会创建一个executor,添加nh_这个Node,但一个node不允许被多个executor添加,所以报了错。

由于这个Node已经在main函数中被添加,所以直接接收response即可

  1. void mySubCallback(const std_msgs::msg::Int32::SharedPtr msg){
  2.     printf("recieve a msg:%d\n",msg->data);
  3.     std::string data =std::to_string(pose_msg->data);
  4.     auto request = std::make_shared<my_msgs::srv::MyService::Request>();
  5.     request->data = data;
  6.     while (!my_client_->wait_for_service(std::chrono::seconds(1))) {
  7.         if (!rclcpp::ok()) {
  8.             RCLCPP_ERROR(nh_->get_logger(), "Interrupted while waiting for the service. Exiting.");
  9.             return;
  10.         }
  11.         RCLCPP_INFO(nh_->get_logger(), "service not available, waiting again...");
  12.     }
  13.     //成功执行
  14.     using ServiceResponseFuture =
  15.         rclcpp::Client<my_msgs::srv::MyService>::SharedFuture;
  16.       auto response_received_callback = [this](ServiceResponseFuture future) {
  17.           RCLCPP_INFO(nh_->get_logger(), "Got result: [%d]", future.get()->num);
  18.         };
  19.       auto future_result = my_client_->async_send_request(request, response_received_callback);
  20. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号