#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
class LWD {
public:
void Init(ros::NodeHandle &n) {
color_sub_.subscribe(n, "/color/image_raw", 1);
depth_sub_.subscribe(n, "/depth/image_rect_raw", 1);
// 严格相同
sync_.connectInput(color_sub_, depth_sub_);
sync_.registerCallback(boost::bind(&LWD::cb, this, _1, _2));
// 近似相同
sync.connectInput(color_sub_, depth_sub_);
sync.registerCallback(boost::bind(&LWD::cb, this, _1, _2));
}
void dep2color(const sensor_msgs::ImageConstPtr& imag, const sensor_msgs::Image::ConstPtr& depth);
void cb(const sensor_msgs::ImageConstPtr& imag, const sensor_msgs::Image::ConstPtr& depth);
message_filters::Subscriber<sensor_msgs::Image> color_sub_;
message_filters::Subscriber<sensor_msgs::Image> depth_sub_;
// 严格相同
message_filters::TimeSynchronizer<sensor_msgs::Image, sensor_msgs::Image> sync_{10};
// 近似相同
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> MySyncPolicy;
message_filters::Synchronizer<MySyncPolicy> sync{MySyncPolicy(10)};
};
void LWD::dep2color(const sensor_msgs::ImageConstPtr& imag, const sensor_msgs::Image::ConstPtr& depth){}
void LWD::cb(const sensor_msgs::ImageConstPtr& imag, const sensor_msgs::Image::ConstPtr& depth) {
ROS_INFO("success");
}
int main(int argc, char** argv) {
ros::init(argc, argv, "listener");
ros::NodeHandle n;
LWD can;
can.Init(n);
ros::spin();
return 0;
}
message_filters做成员变量时,声明和初始化的方法
于 2020-11-04 14:17:34 首次发布