From 562accbf955910a7989c7006c0ee87dc28e37a66 Mon Sep 17 00:00:00 2001 From: Krzysztof Wojciechowski <49921081+Kotochleb@users.noreply.github.com> Date: Wed, 29 May 2024 18:04:37 +0200 Subject: [PATCH] [rolling] image_publisher: Fix loading of the camera info parameters on startup (#983) As described in https://github.com/ros-perception/image_pipeline/issues/965 camera info is not loaded from the file on node initialization, but only when the parameter is reloaded. This PR resolves this issue and should be straightforward to port it to `Humble`, `Iron` and `Jazzy`. (cherry picked from commit 847920b8ee0d0ed6937d029d80799a061e307ab8) # Conflicts: # image_publisher/src/image_publisher.cpp --- image_publisher/src/image_publisher.cpp | 34 ++++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/image_publisher/src/image_publisher.cpp b/image_publisher/src/image_publisher.cpp index dd90c6f2a..ad81becf3 100644 --- a/image_publisher/src/image_publisher.cpp +++ b/image_publisher/src/image_publisher.cpp @@ -68,7 +68,12 @@ ImagePublisher::ImagePublisher(const rclcpp::NodeOptions & options) auto param_change_callback = [this](std::vector parameters) -> rcl_interfaces::msg::SetParametersResult { +<<<<<<< HEAD RCLCPP_INFO(get_logger(), "param_change_callback"); +======= + bool call_init = false; + bool call_reconfigure = false; +>>>>>>> 847920b ([rolling] image_publisher: Fix loading of the camera info parameters on startup (#983)) auto result = rcl_interfaces::msg::SetParametersResult(); result.successful = true; @@ -76,35 +81,42 @@ ImagePublisher::ImagePublisher(const rclcpp::NodeOptions & options) if (parameter.get_name() == "filename") { filename_ = parameter.as_string(); RCLCPP_INFO(get_logger(), "Reset filename as '%s'", filename_.c_str()); - ImagePublisher::onInit(); - return result; + call_init = true; } else if (parameter.get_name() == "field_of_view") { field_of_view_ = parameter.as_double(); RCLCPP_INFO(get_logger(), "Reset field_of_view as '%f'", field_of_view_); - ImagePublisher::onInit(); - return result; + call_init = true; } else if (parameter.get_name() == "flip_horizontal") { flip_horizontal_ = parameter.as_bool(); RCLCPP_INFO(get_logger(), "Reset flip_horizontal as '%d'", flip_horizontal_); - ImagePublisher::onInit(); - return result; + call_init = true; } else if (parameter.get_name() == "flip_vertical") { flip_vertical_ = parameter.as_bool(); RCLCPP_INFO(get_logger(), "Reset flip_vertical as '%d'", flip_vertical_); - ImagePublisher::onInit(); - return result; + call_init = true; } else if (parameter.get_name() == "frame_id") { frame_id_ = parameter.as_string(); RCLCPP_INFO(get_logger(), "Reset frame_id as '%s'", frame_id_.c_str()); } else if (parameter.get_name() == "publish_rate") { publish_rate_ = parameter.as_double(); RCLCPP_INFO(get_logger(), "Reset publish_rate as '%lf'", publish_rate_); + call_reconfigure = true; } else if (parameter.get_name() == "camera_info_url") { camera_info_url_ = parameter.as_string(); RCLCPP_INFO(get_logger(), "Reset camera_info_rul as '%s'", camera_info_url_.c_str()); +<<<<<<< HEAD +======= + call_reconfigure = true; +>>>>>>> 847920b ([rolling] image_publisher: Fix loading of the camera info parameters on startup (#983)) } } - ImagePublisher::reconfigureCallback(); + // reconfigureCallback() is called within onInit() so there is no need to call it twice + if (call_reconfigure && !call_init) { + ImagePublisher::reconfigureCallback(); + } else if (call_init) { + ImagePublisher::onInit(); + } + return result; }; on_set_parameters_callback_handle_ = this->add_on_set_parameters_callback(param_change_callback); @@ -234,9 +246,7 @@ void ImagePublisher::onInit() camera_info_.p = {f_approx, 0, static_cast(camera_info_.width / 2), 0, 0, f_approx, static_cast(camera_info_.height / 2), 0, 0, 0, 1, 0}; - timer_ = this->create_wall_timer( - std::chrono::milliseconds(static_cast(1000 / publish_rate_)), - std::bind(&ImagePublisher::doWork, this)); + ImagePublisher::reconfigureCallback(); } } // namespace image_publisher