| 20 | class StereoSync : public rclcpp::Node { |
| 21 | public: |
| 22 | explicit StereoSync(const rclcpp::NodeOptions& options) : Node("stereo_sync", options) { |
| 23 | previous_stamp_ = rclcpp::Time(0, 0); |
| 24 | |
| 25 | this->get_parameter("left_img_topic", left_img_topic_); |
| 26 | this->get_parameter("right_img_topic", right_img_topic_); |
| 27 | this->get_parameter("compressed", compressed_image_); |
| 28 | this->get_parameter("config_filename", config_file_); |
| 29 | |
| 30 | cv::FileStorage fsSettings(config_file_, cv::FileStorage::READ); |
| 31 | |
| 32 | if (!fsSettings.isOpened()) { |
| 33 | RCLCPP_FATAL_STREAM(this->get_logger(), "ERROR: Wrong path to settings\n"); |
| 34 | } |
| 35 | |
| 36 | float frame_rate = static_cast<float>(fsSettings["camera_params"]["camera_rate"]); |
| 37 | float max_time_diff_between_msgs = 0.5f / frame_rate; |
| 38 | RCLCPP_INFO_STREAM(this->get_logger(), "Setting Age Penalty to: " << max_time_diff_between_msgs); |
| 39 | rclcpp::QoS qos(10); |
| 40 | auto rmw_qos_profile = qos.get_rmw_qos_profile(); |
| 41 | static constexpr size_t kImageSynchronizerQueueSize = 10u; |
| 42 | |
| 43 | if (compressed_image_) { |
| 44 | left_compressed_img_topic_ = left_img_topic_ + "/compressed"; |
| 45 | right_compressed_img_topic_ = right_img_topic_ + "/compressed"; |
| 46 | |
| 47 | left_compressed_img_sub_.subscribe(this, left_compressed_img_topic_, rmw_qos_profile); |
| 48 | right_compressed_img_sub_.subscribe(this, right_compressed_img_topic_, rmw_qos_profile); |
| 49 | compressed_image_synchronizer_ = std::make_unique<message_filters::Synchronizer<compressed_image_sync_policy>>( |
| 50 | compressed_image_sync_policy(kImageSynchronizerQueueSize), |
| 51 | left_compressed_img_sub_, |
| 52 | right_compressed_img_sub_); |
| 53 | compressed_image_synchronizer_->registerCallback( |
| 54 | std::bind(&StereoSync::compressedImageCallback, this, std::placeholders::_1, std::placeholders::_2)); |
| 55 | compressed_image_synchronizer_->setAgePenalty(max_time_diff_between_msgs); |
| 56 | } else { |
| 57 | left_img_sub_.subscribe(this, left_img_topic_, rmw_qos_profile); |
| 58 | right_img_sub_.subscribe(this, right_img_topic_, rmw_qos_profile); |
| 59 | image_synchonizer_ = std::make_unique<message_filters::Synchronizer<image_sync_policy>>( |
| 60 | image_sync_policy(kImageSynchronizerQueueSize), left_img_sub_, right_img_sub_); |
| 61 | image_synchonizer_->registerCallback( |
| 62 | std::bind(&StereoSync::imageCallback, this, std::placeholders::_1, std::placeholders::_2)); |
| 63 | image_synchonizer_->setAgePenalty(max_time_diff_between_msgs); |
| 64 | } |
| 65 | |
| 66 | left_img_pub_ = this->create_publisher<sensor_msgs::msg::Image>("/cam0/image_raw", 100); |
| 67 | right_img_pub_ = this->create_publisher<sensor_msgs::msg::Image>("/cam1/image_raw", 100); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | std::string left_img_topic_{}, right_img_topic_{}; |