Marcelo Mafalda
Marcelo Mafalda

Reputation: 1

ROS2 Callback not being called using Message Filters C++

I'm having some issues trying to implement message filters in my code. Using this simple example everything compiles but no callback function is called when it runs. Does anybody knows why this is happening? I already checked the QoS of both topics and they match so i dont know why the callback function isn't being called. I'm running ROS2 Galactic on Ubuntu 20.04. Any help will be appreciated!

#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;
}

Upvotes: 0

Views: 1753

Answers (1)

Hriday
Hriday

Reputation: 1

The reason is that the scope of your syncApproximate variable is local in the constructor and it gets out of scope as the constructor exits. You will need to declare syncApproximate as a member variable of your class MinimalSubscriber. Example:

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;
}


Upvotes: 0

Related Questions