【问题标题】:ROS2 Callback not being called using Message Filters C++未使用消息过滤器 C++ 调用 ROS2 回调
【发布时间】:2023-01-05 16:51:46
【问题描述】:

我在尝试在我的代码中实现消息过滤器时遇到了一些问题。使用这个简单的例子,一切都会编译,但在运行时不会调用回调函数。有谁知道为什么会这样?我已经检查了两个主题的 QoS,它们匹配,所以我不知道为什么没有调用回调函数。我在 Ubuntu 20.04 上运行 ROS2 Galactic。任何帮助将不胜感激!

#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "sensor_msgs/msg/point_cloud2.hpp"
#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>

//using std::placeholders::_1;

class MinimalSubscriber : public rclcpp::Node
{
public:

message_filters::Subscriber<sensor_msgs::msg::Image> image_sub_;
message_filters::Subscriber<sensor_msgs::msg::PointCloud2> cloud_sub_;

MinimalSubscriber()
: Node("minimal_subscriber_left")
{
  
  image_sub_.subscribe(this, "/left/image_raw");
  cloud_sub_.subscribe(this, "/model/prius_hybrid/laserscan/points");
  
  typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::msg::Image, sensor_msgs::msg::PointCloud2> approximate_policy;
  message_filters::Synchronizer<approximate_policy>syncApproximate(approximate_policy(10), image_sub_, cloud_sub_);
  syncApproximate.registerCallback(&MinimalSubscriber::topic_callback, this); 
  
};

public:
    void topic_callback(const sensor_msgs::msg::Image::SharedPtr image, const 
    sensor_msgs::msg::PointCloud2::SharedPtr cloud2)
    { 
      std::cout<<"Hello messages are being received";
      RCLCPP_INFO(this->get_logger(), "Publishing");    
    }; 
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalSubscriber>());
  rclcpp::shutdown();
  return 0;
}

【问题讨论】:

    标签: ros2


    【解决方案1】:

    原因是您的 syncApproximate 变量的范围在构造函数中是局部的,并且在构造函数退出时超出范围。您需要将syncApproximate 声明为类MinimalSubscriber 的成员变量。例子:

    class MinimalSubscriber : public rclcpp::Node
    {
    public:
    
    message_filters::Subscriber<sensor_msgs::msg::Image> image_sub_;
    message_filters::Subscriber<sensor_msgs::msg::PointCloud2> cloud_sub_;
    typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::msg::Image, sensor_msgs::msg::PointCloud2> approximate_policy;
    message_filters::Synchronizer<approximate_policy>syncApproximate;
    
    MinimalSubscriber()
    : Node("minimal_subscriber_left")
    {
      
      image_sub_.subscribe(this, "/left/image_raw");
      cloud_sub_.subscribe(this, "/model/prius_hybrid/laserscan/points");
      syncApproximate(approximate_policy(10), image_sub_, cloud_sub_);
      syncApproximate.registercallback(&MinimalSubscriber::topic_callback, this);
    }
    
    public:
        void topic_callback(const sensor_msgs::msg::Image::SharedPtr image, const 
        sensor_msgs::msg::PointCloud2::SharedPtr cloud2)
        { 
          std::cout<<"Hello messages are being received";
          RCLCPP_INFO(this->get_logger(), "Publishing");    
        }; 
    };
    
    int main(int argc, char * argv[])
    {
      rclcpp::init(argc, argv);
      rclcpp::spin(std::make_shared<MinimalSubscriber>());
      rclcpp::shutdown();
      return 0;
    }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多