关键词ros2、zed、时间同步、ApproximateTime
问题1.怎么写的都不调用回调函数,
排查方向,回调函数的参数列表和输入是否一致,话题对应的数据类型和subscribe定义的是否一致,这两个是最重要的
其次还有容错时间、话题是否有效等等粗心大意的问题都是排查方向,最后我的问题是应该在头文件里的类里就定义
message_filters::Subscriber<sensor_msgs::msg::Image> left_sub_m;
message_filters::Subscriber<sensor_msgs::msg::Image> depth_sub_m;
一开始把订阅者在实现中定义而不是在头文件声明中定义
message_filters::Subscriber<sensor_msgs::msg::Image> left_sub_m;
message_filters::Subscriber<sensor_msgs::msg::Image> depth_sub_m;
left_sub_m.subscribe(this, "/zed/zed_node/left/image_rect_color");
depth_sub_m.subscribe(this, "/zed/zed_node/depth/depth_registered");
//使用 message_filters 中的 ApproximateTime 策略进行左右图像和深度图像的近似时间同步,该策略将根据消息的时间戳匹配近似时间的左右图像和深度图像
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::msg::Image, sensor_msgs::msg::Image> approximate_policy;
//使用 Synchronizer 类创建一个同步器,并传入 ApproximateTime 策略和左右图像的订阅器允许消息在时间上有一定的误差(这里设置为100毫秒)
static message_filters::Synchronizer<approximate_policy> syncApproximate(approximate_policy(100), left_sub_m, depth_sub_m);
//通过syncApproximate.setMaxIntervalDuration方法设置同步器的最大时间间隔,这里设置为3秒
// syncApproximate.setMaxIntervalDuration(rclcpp::Duration(3,0));
//使用 registerCallback 方法注册一个回调函数,该回调函数会在近似时间同步成功后被调用。在回调函数中调用 GrabDepth 方法处理左右图像和深度图像数据
syncApproximate.registerCallback(std::bind(&ObjectDetectionNode::GrabDepth_callback, this,std::placeholders::_1, std::placeholders::_2));
我的解决方法仅供参考
https://wiki.ros.org/message_filters
https://www.cnblogs.com/RobustFresher/p/13182645.html