message_filters做成员变量时,声明和初始化的方法

#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;
}
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值