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

本文介绍了一种使用ROS实现彩色图像与深度图像的时间同步方法。通过message_filters库中的TimeSynchronizer和ApproximateTime同步策略,确保了来自不同传感器的数据能够被准确配对处理。此方法对于机器人视觉应用尤为关键。
#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;
}
#include <message_filters/subscriber.h> #include <message_filters/synchronizer.h> #include <message_filters/sync_policies/approximate_time.h> #include <boost/thread/thread.hpp> using namespace message_filters; void imu_callback(const sensor_msgs::ImuConstPtr &imu_msg_accel, const sensor_msgs::ImuConstPtr &imu_msg_gyro) { double t = imu_msg_accel->header.stamp.toSec(); double dx = imu_msg_accel->linear_acceleration.x; double dy = imu_msg_accel->linear_acceleration.y; double dz = imu_msg_accel->linear_acceleration.z; double rx = imu_msg_gyro->angular_velocity.x; double ry = imu_msg_gyro->angular_velocity.y; double rz = imu_msg_gyro->angular_velocity.z; Vector3d gyr(rx, ry, rz); Vector3d acc(dx, dy, dz); /** 处理函数 ...... */ } int main(int argc, char** argv) { // 需要用message_filter容器对两个话题的数据发布进行初始化,这里不能指定回调函数 message_filters::Subscriber<sensor_msgs::Imu> sub_imu_accel(n,IMU_TOPIC_ACCEL,2000,ros::TransportHints().tcpNoDelay()); message_filters::Subscriber<sensor_msgs::Imu> sub_imu_gyro(n,IMU_TOPIC_GYRO,2000,ros::TransportHints().tcpNoDelay()); // 将两个话题的数据进行同步 typedef sync_policies::ApproximateTime<sensor_msgs::Imu, sensor_msgs::Imu> syncPolicy; Synchronizer<syncPolicy> sync(syncPolicy(10), sub_imu_accel, sub_imu_gyro); // 指定一个回调函数,就可以实现两个话题数据的同步获取 sync.registerCallback(boost::bind(&imu_callback, _1, _2)); ros::spin(); return 0; } ———————————————— 详细解释
最新发布
07-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

刀么克瑟拉莫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值