diff --git a/localization/autoware_ndt_scan_matcher/CMakeLists.txt b/localization/autoware_ndt_scan_matcher/CMakeLists.txt index b63189d10c..dbbe71129f 100644 --- a/localization/autoware_ndt_scan_matcher/CMakeLists.txt +++ b/localization/autoware_ndt_scan_matcher/CMakeLists.txt @@ -47,6 +47,7 @@ ament_auto_add_library(${PROJECT_NAME} SHARED src/ndt_scan_matcher_core.cpp src/ndt_scan_matcher_helper.cpp src/particle.cpp + src/scan_match_diagnostics.cpp ) link_directories(${PCL_LIBRARY_DIRS}) @@ -116,6 +117,11 @@ if(BUILD_TESTING) ) target_link_libraries(test_ndt_scan_matcher_helper ${PROJECT_NAME}) + ament_auto_add_gtest(test_scan_match_diagnostics + test/test_scan_match_diagnostics.cpp + ) + target_link_libraries(test_scan_match_diagnostics ${PROJECT_NAME}) + # Unit tests for the map update module driven by an injected fake pcd loader (no ROS service). ament_auto_add_gtest(test_map_update_module test/test_map_update_module.cpp diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/diagnostics_report.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/diagnostics_report.hpp new file mode 100644 index 0000000000..54db6a8f06 --- /dev/null +++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/diagnostics_report.hpp @@ -0,0 +1,67 @@ +// Copyright 2026 Autoware Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef AUTOWARE__NDT_SCAN_MATCHER__DIAGNOSTICS_REPORT_HPP_ +#define AUTOWARE__NDT_SCAN_MATCHER__DIAGNOSTICS_REPORT_HPP_ + +#include +#include +#include +#include +#include + +namespace autoware::ndt_scan_matcher +{ + +// Severity of a diagnostics update. Mirrors diagnostic_msgs::msg::DiagnosticStatus levels so +// the core modules need no ROS diagnostics dependency. +enum class DiagnosticLevel : int8_t { OK = 0, WARN = 1, ERROR = 2, STALE = 3 }; + +// A single diagnostic key/value. The value keeps its type so the ROS node can format it exactly +// the way DiagnosticsInterface would (e.g. bool as "True"/"False"). +struct DiagnosticKeyValue +{ + std::string key; + std::variant value; +}; + +// Diagnostics accumulated by a core module: key/values plus an overall status (level + message). +// Returned to the ROS node, which forwards it to a DiagnosticsInterface. Plain data, kept ROS-free +// on purpose. +struct DiagnosticsReport +{ + DiagnosticLevel level{DiagnosticLevel::OK}; + std::string message; + std::vector key_values; + + void add_key_value(DiagnosticKeyValue key_value) { key_values.push_back(std::move(key_value)); } + + // Accumulates like DiagnosticsInterface: raises the level and appends the message. + void update_level_and_message(DiagnosticLevel new_level, const std::string & new_message) + { + if (static_cast(new_level) > static_cast(DiagnosticLevel::OK)) { + if (!message.empty()) { + message += "; "; + } + message += new_message; + } + if (static_cast(new_level) > static_cast(level)) { + level = new_level; + } + } +}; + +} // namespace autoware::ndt_scan_matcher + +#endif // AUTOWARE__NDT_SCAN_MATCHER__DIAGNOSTICS_REPORT_HPP_ diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp index 7956dac564..b2387d3666 100644 --- a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp +++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp @@ -15,6 +15,7 @@ #ifndef AUTOWARE__NDT_SCAN_MATCHER__MAP_UPDATE_MODULE_HPP_ #define AUTOWARE__NDT_SCAN_MATCHER__MAP_UPDATE_MODULE_HPP_ +#include "diagnostics_report.hpp" #include "guarded.hpp" #include "hyper_parameters.hpp" #include "ndt_omp/multigrid_ndt_omp.h" @@ -25,14 +26,12 @@ #include -#include #include #include #include #include #include #include -#include #include namespace autoware::ndt_scan_matcher @@ -57,43 +56,10 @@ class MapUpdateModule using PcdLoaderFunction = std::function; - // Severity of a diagnostics update. Mirrors diagnostic_msgs::msg::DiagnosticStatus levels so - // this module needs no ROS diagnostics dependency. - enum class DiagnosticLevel : int8_t { OK = 0, WARN = 1, ERROR = 2, STALE = 3 }; - - // A single diagnostic key/value. The value keeps its type so the ROS node can format it exactly - // the way DiagnosticsInterface would (e.g. bool as "True"/"False"). - struct DiagnosticKeyValue - { - std::string key; - std::variant value; - }; - - // Diagnostics accumulated while updating the map: key/values plus an overall status (level + - // message). Returned to the ROS node, which forwards it to a DiagnosticsInterface. Plain data, - // kept ROS-free on purpose. - struct DiagnosticsReport - { - DiagnosticLevel level{DiagnosticLevel::OK}; - std::string message; - std::vector key_values; - - void add_key_value(DiagnosticKeyValue key_value) { key_values.push_back(std::move(key_value)); } - - // Accumulates like DiagnosticsInterface: raises the level and appends the message. - void update_level_and_message(DiagnosticLevel new_level, const std::string & new_message) - { - if (static_cast(new_level) > static_cast(DiagnosticLevel::OK)) { - if (!message.empty()) { - message += "; "; - } - message += new_message; - } - if (static_cast(new_level) > static_cast(level)) { - level = new_level; - } - } - }; + // Kept as member aliases so existing MapUpdateModule::Diagnostic* references keep working. + using DiagnosticLevel = autoware::ndt_scan_matcher::DiagnosticLevel; + using DiagnosticKeyValue = autoware::ndt_scan_matcher::DiagnosticKeyValue; + using DiagnosticsReport = autoware::ndt_scan_matcher::DiagnosticsReport; // Result of a map update entry point: whether the NDT map changed, plus the diagnostics to // report for this call. diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp index 277dbdc416..96987fbbaa 100644 --- a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp +++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp @@ -21,6 +21,7 @@ #include "hyper_parameters.hpp" #include "map_update_module.hpp" #include "ndt_omp/multigrid_ndt_omp.h" +#include "scan_match_diagnostics.hpp" #include #include @@ -232,6 +233,7 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node std::atomic is_activated_; std::unique_ptr diagnostics_scan_points_; + std::unique_ptr scan_match_diagnostics_; std::unique_ptr diagnostics_initial_pose_; std::unique_ptr diagnostics_regularization_pose_; std::unique_ptr diagnostics_map_update_; diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/scan_match_diagnostics.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/scan_match_diagnostics.hpp new file mode 100644 index 0000000000..bad63d8790 --- /dev/null +++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/scan_match_diagnostics.hpp @@ -0,0 +1,66 @@ +// Copyright 2026 Autoware Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef AUTOWARE__NDT_SCAN_MATCHER__SCAN_MATCH_DIAGNOSTICS_HPP_ +#define AUTOWARE__NDT_SCAN_MATCHER__SCAN_MATCH_DIAGNOSTICS_HPP_ + +#include "diagnostics_report.hpp" + +#include + +namespace autoware::ndt_scan_matcher +{ + +struct ScanMatchDiagnosticsConfig +{ + double initial_to_result_distance_tolerance_m; + double critical_upper_bound_exe_time_ms; +}; + +// Values observed while matching one scan. The convergence verdicts (is_ok_*) are decided by the +// scan matching itself and are only reported here, never recomputed. +struct ScanMatchDiagnosticValues +{ + int iteration_num{}; + int maximum_iterations{}; + bool is_ok_iteration_num{}; + int oscillation_num{}; + bool is_local_optimal_solution_oscillation{}; + float transform_probability{}; + float nearest_voxel_transformation_likelihood{}; + std::vector transform_probability_array; + std::vector nearest_voxel_transformation_likelihood_array; + // When false, the scan was rejected at this point and the fields below are not reported. + bool is_known_converged_param_type{true}; + double score{}; + double score_threshold{}; + bool is_ok_score{}; + double distance_initial_to_result{}; + float execution_time_ms{}; +}; + +// Builds the scan matching part of the `scan_matching_status` diagnostics from the observed values. +class ScanMatchDiagnostics +{ +public: + explicit ScanMatchDiagnostics(const ScanMatchDiagnosticsConfig & config); + [[nodiscard]] DiagnosticsReport make_report(const ScanMatchDiagnosticValues & values) const; + +private: + ScanMatchDiagnosticsConfig config_; +}; + +} // namespace autoware::ndt_scan_matcher + +#endif // AUTOWARE__NDT_SCAN_MATCHER__SCAN_MATCH_DIAGNOSTICS_HPP_ diff --git a/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp b/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp index 4e87eb3c3e..c6c324df01 100644 --- a/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp +++ b/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp @@ -204,6 +204,10 @@ NDTScanMatcher::NDTScanMatcher(const rclcpp::NodeOptions & options) return this->get_differential_point_cloud_map(request); }); + scan_match_diagnostics_ = std::make_unique(ScanMatchDiagnosticsConfig{ + param_.validation.initial_to_result_distance_tolerance_m, + param_.validation.critical_upper_bound_exe_time_ms}); + diagnostics_scan_points_ = std::make_unique(this, "scan_matching_status"); diagnostics_initial_pose_ = std::make_unique(this, "initial_pose_subscriber_status"); @@ -557,37 +561,29 @@ bool NDTScanMatcher::callback_sensor_points_main( transformation_msg_array.push_back(pose_ros); } + ScanMatchDiagnosticValues diagnostic_values; + diagnostic_values.iteration_num = ndt_result.iteration_num; + diagnostic_values.maximum_iterations = ndt_ptr->getMaximumIterations(); + diagnostic_values.transform_probability = ndt_result.transform_probability; + diagnostic_values.nearest_voxel_transformation_likelihood = + ndt_result.nearest_voxel_transformation_likelihood; + diagnostic_values.transform_probability_array = ndt_result.transform_probability_array; + diagnostic_values.nearest_voxel_transformation_likelihood_array = + ndt_result.nearest_voxel_transformation_likelihood_array; + // check iteration_num - diagnostics_scan_points_->add_key_value("iteration_num", ndt_result.iteration_num); const bool is_ok_iteration_num = (ndt_result.iteration_num < ndt_ptr->getMaximumIterations()); - if (!is_ok_iteration_num) { - std::stringstream message; - message << "The number of iterations has reached its upper limit. The number of iterations: " - << ndt_result.iteration_num << ", Limit: " << ndt_ptr->getMaximumIterations() << "."; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::WARN, message.str()); - } + diagnostic_values.is_ok_iteration_num = is_ok_iteration_num; // check local_optimal_solution_oscillation_num constexpr int oscillation_num_threshold = 10; const int oscillation_num = count_oscillation(transformation_msg_array); - diagnostics_scan_points_->add_key_value( - "local_optimal_solution_oscillation_num", oscillation_num); const bool is_local_optimal_solution_oscillation = (oscillation_num > oscillation_num_threshold); - if (is_local_optimal_solution_oscillation) { - std::stringstream message; - message << "There is a possibility of oscillation in a local minimum"; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::WARN, message.str()); - } + diagnostic_values.oscillation_num = oscillation_num; + diagnostic_values.is_local_optimal_solution_oscillation = is_local_optimal_solution_oscillation; // check score - diagnostics_scan_points_->add_key_value( - "transform_probability", ndt_result.transform_probability); - diagnostics_scan_points_->add_key_value( - "nearest_voxel_transformation_likelihood", - ndt_result.nearest_voxel_transformation_likelihood); double score = 0.0; double score_threshold = 0.0; if (param_.score_estimation.converged_param_type == ConvergedParamType::TRANSFORM_PROBABILITY) { @@ -600,56 +596,21 @@ bool NDTScanMatcher::callback_sensor_points_main( score_threshold = param_.score_estimation.converged_param_nearest_voxel_transformation_likelihood; } else { - std::stringstream message; - message - << "Unknown converged param type. Please check `score_estimation.converged_param_type`"; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::ERROR, message.str()); + diagnostic_values.is_known_converged_param_type = false; + apply_diagnostics_update( + *diagnostics_scan_points_, scan_match_diagnostics_->make_report(diagnostic_values)); return false; } - // check score diff - const std::vector & tp_array = ndt_result.transform_probability_array; - if (static_cast(tp_array.size()) != ndt_result.iteration_num + 1) { - // only publish warning to /diagnostics, not skip publishing pose - std::stringstream message; - message << "transform_probability_array size is not equal to iteration_num + 1." - << " transform_probability_array size: " << tp_array.size() - << ", iteration_num: " << ndt_result.iteration_num; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::WARN, message.str()); - } else { - const float diff = tp_array.back() - tp_array.front(); - diagnostics_scan_points_->add_key_value("transform_probability_diff", diff); - diagnostics_scan_points_->add_key_value("transform_probability_before", tp_array.front()); - } - const std::vector & nvtl_array = - ndt_result.nearest_voxel_transformation_likelihood_array; - if (static_cast(nvtl_array.size()) != ndt_result.iteration_num + 1) { - // only publish warning to /diagnostics, not skip publishing pose - std::stringstream message; - message - << "nearest_voxel_transformation_likelihood_array size is not equal to iteration_num + 1." - << " nearest_voxel_transformation_likelihood_array size: " << nvtl_array.size() - << ", iteration_num: " << ndt_result.iteration_num; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::WARN, message.str()); - } else { - const float diff = nvtl_array.back() - nvtl_array.front(); - diagnostics_scan_points_->add_key_value("nearest_voxel_transformation_likelihood_diff", diff); - diagnostics_scan_points_->add_key_value( - "nearest_voxel_transformation_likelihood_before", nvtl_array.front()); - } - bool is_ok_score = (score > score_threshold); if (!is_ok_score) { - std::stringstream message; - message << "Score is below the threshold. Score: " << score - << ", Threshold: " << score_threshold; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::WARN, message.str()); - RCLCPP_WARN_STREAM_THROTTLE(this->get_logger(), *this->get_clock(), 1000, message.str()); + RCLCPP_WARN_STREAM_THROTTLE( + this->get_logger(), *this->get_clock(), 1000, + "Score is below the threshold. Score: " << score << ", Threshold: " << score_threshold); } + diagnostic_values.score = score; + diagnostic_values.score_threshold = score_threshold; + diagnostic_values.is_ok_score = is_ok_score; // check is_converged bool is_converged = @@ -681,31 +642,18 @@ bool NDTScanMatcher::callback_sensor_points_main( ndt_covariance[0 + 6 * 1] = estimated_covariance_2d_adj(0, 1); } - // check distance_initial_to_result - const auto distance_initial_to_result = static_cast(autoware::localization_util::norm( - interpolation_result.interpolated_pose.pose.pose.position, result_pose_msg.position)); - diagnostics_scan_points_->add_key_value( - "distance_initial_to_result", distance_initial_to_result); - if (distance_initial_to_result > param_.validation.initial_to_result_distance_tolerance_m) { - std::stringstream message; - message << "distance_initial_to_result is too large (" << distance_initial_to_result - << " [m])."; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::WARN, message.str()); - } + diagnostic_values.distance_initial_to_result = + static_cast(autoware::localization_util::norm( + interpolation_result.interpolated_pose.pose.pose.position, result_pose_msg.position)); - // check execution_time const auto exe_end_time = std::chrono::system_clock::now(); const auto duration_micro_sec = std::chrono::duration_cast(exe_end_time - exe_start_time).count(); const auto exe_time = static_cast(duration_micro_sec) / 1000.0f; - diagnostics_scan_points_->add_key_value("execution_time", exe_time); - if (exe_time > param_.validation.critical_upper_bound_exe_time_ms) { - std::stringstream message; - message << "NDT exe time is too long (took " << exe_time << " [ms])."; - diagnostics_scan_points_->update_level_and_message( - diagnostic_msgs::msg::DiagnosticStatus::WARN, message.str()); - } + diagnostic_values.execution_time_ms = exe_time; + + apply_diagnostics_update( + *diagnostics_scan_points_, scan_match_diagnostics_->make_report(diagnostic_values)); // publish initial_pose_with_covariance_pub_->publish(interpolation_result.interpolated_pose); @@ -1127,6 +1075,7 @@ void NDTScanMatcher::service_ndt_align_main( // estimate initial pose const auto [pose_with_covariance, score] = align_pose(initial_pose_msg_in_map_frame, *ndt_ptr); + diagnostics_ndt_align_->add_key_value("best_particle_score", score); // check reliability of initial pose result res->reliable = @@ -1242,7 +1191,6 @@ std::tuple NDTScanMatcher autoware::localization_util::output_pose_with_cov_to_log( get_logger(), "align_pose_output", result_pose_with_cov_msg); - diagnostics_ndt_align_->add_key_value("best_particle_score", best_particle_ptr->score); return std::make_tuple(result_pose_with_cov_msg, best_particle_ptr->score); } diff --git a/localization/autoware_ndt_scan_matcher/src/scan_match_diagnostics.cpp b/localization/autoware_ndt_scan_matcher/src/scan_match_diagnostics.cpp new file mode 100644 index 0000000000..74926fbc77 --- /dev/null +++ b/localization/autoware_ndt_scan_matcher/src/scan_match_diagnostics.cpp @@ -0,0 +1,115 @@ +// Copyright 2026 Autoware Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +namespace autoware::ndt_scan_matcher +{ +namespace +{ +// Reports the first-to-last change of a per-iteration score array, or warns when the array does +// not hold one entry per iteration plus the initial one. +void report_score_array( + const std::string & name, const std::vector & array, const int iteration_num, + DiagnosticsReport & report) +{ + if (static_cast(array.size()) != iteration_num + 1) { + // only publish warning to /diagnostics, not skip publishing pose + std::stringstream message; + message << name << "_array size is not equal to iteration_num + 1." + << " " << name << "_array size: " << array.size() + << ", iteration_num: " << iteration_num; + report.update_level_and_message(DiagnosticLevel::WARN, message.str()); + return; + } + const float diff = array.back() - array.front(); + report.add_key_value({name + "_diff", static_cast(diff)}); + report.add_key_value({name + "_before", static_cast(array.front())}); +} +} // namespace + +ScanMatchDiagnostics::ScanMatchDiagnostics(const ScanMatchDiagnosticsConfig & config) +: config_(config) +{ +} + +DiagnosticsReport ScanMatchDiagnostics::make_report(const ScanMatchDiagnosticValues & values) const +{ + DiagnosticsReport report; + + report.add_key_value({"iteration_num", static_cast(values.iteration_num)}); + if (!values.is_ok_iteration_num) { + std::stringstream message; + message << "The number of iterations has reached its upper limit. The number of iterations: " + << values.iteration_num << ", Limit: " << values.maximum_iterations << "."; + report.update_level_and_message(DiagnosticLevel::WARN, message.str()); + } + + report.add_key_value( + {"local_optimal_solution_oscillation_num", static_cast(values.oscillation_num)}); + if (values.is_local_optimal_solution_oscillation) { + report.update_level_and_message( + DiagnosticLevel::WARN, "There is a possibility of oscillation in a local minimum"); + } + + report.add_key_value( + {"transform_probability", static_cast(values.transform_probability)}); + report.add_key_value( + {"nearest_voxel_transformation_likelihood", + static_cast(values.nearest_voxel_transformation_likelihood)}); + + if (!values.is_known_converged_param_type) { + report.update_level_and_message( + DiagnosticLevel::ERROR, + "Unknown converged param type. Please check `score_estimation.converged_param_type`"); + return report; + } + + report_score_array( + "transform_probability", values.transform_probability_array, values.iteration_num, report); + report_score_array( + "nearest_voxel_transformation_likelihood", values.nearest_voxel_transformation_likelihood_array, + values.iteration_num, report); + + if (!values.is_ok_score) { + std::stringstream message; + message << "Score is below the threshold. Score: " << values.score + << ", Threshold: " << values.score_threshold; + report.update_level_and_message(DiagnosticLevel::WARN, message.str()); + } + + report.add_key_value({"distance_initial_to_result", values.distance_initial_to_result}); + if (values.distance_initial_to_result > config_.initial_to_result_distance_tolerance_m) { + std::stringstream message; + message << "distance_initial_to_result is too large (" << values.distance_initial_to_result + << " [m])."; + report.update_level_and_message(DiagnosticLevel::WARN, message.str()); + } + + report.add_key_value({"execution_time", static_cast(values.execution_time_ms)}); + if (values.execution_time_ms > config_.critical_upper_bound_exe_time_ms) { + std::stringstream message; + message << "NDT exe time is too long (took " << values.execution_time_ms << " [ms])."; + report.update_level_and_message(DiagnosticLevel::WARN, message.str()); + } + + return report; +} + +} // namespace autoware::ndt_scan_matcher diff --git a/localization/autoware_ndt_scan_matcher/test/test_scan_match_diagnostics.cpp b/localization/autoware_ndt_scan_matcher/test/test_scan_match_diagnostics.cpp new file mode 100644 index 0000000000..48c85bba82 --- /dev/null +++ b/localization/autoware_ndt_scan_matcher/test/test_scan_match_diagnostics.cpp @@ -0,0 +1,299 @@ +// Copyright 2026 Autoware Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "autoware/ndt_scan_matcher/scan_match_diagnostics.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +namespace +{ +using autoware::ndt_scan_matcher::DiagnosticLevel; +using autoware::ndt_scan_matcher::DiagnosticsReport; +using autoware::ndt_scan_matcher::ScanMatchDiagnostics; +using autoware::ndt_scan_matcher::ScanMatchDiagnosticsConfig; +using autoware::ndt_scan_matcher::ScanMatchDiagnosticValues; + +ScanMatchDiagnosticsConfig make_default_config() +{ + ScanMatchDiagnosticsConfig config{}; + config.initial_to_result_distance_tolerance_m = 3.0; + config.critical_upper_bound_exe_time_ms = 100.0; + return config; +} + +// A converged scan match that passes every check. +ScanMatchDiagnosticValues make_healthy_values() +{ + ScanMatchDiagnosticValues values; + values.iteration_num = 3; + values.maximum_iterations = 30; + values.is_ok_iteration_num = true; + values.oscillation_num = 0; + values.is_local_optimal_solution_oscillation = false; + values.transform_probability = 3.0f; + values.nearest_voxel_transformation_likelihood = 2.5f; + values.transform_probability_array = {2.0f, 2.5f, 2.75f, 3.0f}; + values.nearest_voxel_transformation_likelihood_array = {1.5f, 2.0f, 2.25f, 2.5f}; + values.is_known_converged_param_type = true; + values.score = 2.5; + values.score_threshold = 2.3; + values.is_ok_score = true; + values.distance_initial_to_result = 0.1; + values.execution_time_ms = 10.0f; + return values; +} + +std::vector keys_of(const DiagnosticsReport & report) +{ + std::vector keys; + for (const auto & key_value : report.key_values) { + keys.push_back(key_value.key); + } + return keys; +} + +// The value type decides how the node formats the value in /diagnostics (e.g. "3" vs "3.000000"). +std::vector> keys_and_types_of(const DiagnosticsReport & report) +{ + std::vector> keys_and_types; + for (const auto & key_value : report.key_values) { + const std::string type = std::visit( + [](const auto & value) -> std::string { + using T = std::decay_t; + if constexpr (std::is_same_v) return "bool"; + if constexpr (std::is_same_v) return "int64_t"; + if constexpr (std::is_same_v) return "double"; + return "string"; + }, + key_value.value); + keys_and_types.emplace_back(key_value.key, type); + } + return keys_and_types; +} + +double double_value_of(const DiagnosticsReport & report, const std::string & key) +{ + for (const auto & key_value : report.key_values) { + if (key_value.key == key) { + return std::get(key_value.value); + } + } + ADD_FAILURE() << "key not found: " << key; + return 0.0; +} +} // namespace + +TEST(ScanMatchDiagnostics, ReportsOkWhenAllChecksPass) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + + // Act + const DiagnosticsReport report = diagnostics.make_report(make_healthy_values()); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::OK); + EXPECT_TRUE(report.message.empty()); +} + +TEST(ScanMatchDiagnostics, ReportsAllScanMatchingKeysInOrderWithTheirValueTypes) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + + // Act + const DiagnosticsReport report = diagnostics.make_report(make_healthy_values()); + + // Assert + const std::vector> expected{ + {"iteration_num", "int64_t"}, + {"local_optimal_solution_oscillation_num", "int64_t"}, + {"transform_probability", "double"}, + {"nearest_voxel_transformation_likelihood", "double"}, + {"transform_probability_diff", "double"}, + {"transform_probability_before", "double"}, + {"nearest_voxel_transformation_likelihood_diff", "double"}, + {"nearest_voxel_transformation_likelihood_before", "double"}, + {"distance_initial_to_result", "double"}, + {"execution_time", "double"}}; + EXPECT_EQ(keys_and_types_of(report), expected); +} + +TEST(ScanMatchDiagnostics, WarnsWhenIterationNumReachesLimit) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.maximum_iterations = 3; // iteration_num (3) has reached the limit + values.is_ok_iteration_num = false; + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::WARN); + EXPECT_EQ( + report.message, + "The number of iterations has reached its upper limit. The number of iterations: 3, Limit: 3."); +} + +TEST(ScanMatchDiagnostics, WarnsWhenOscillationIsDetected) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.is_local_optimal_solution_oscillation = true; + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::WARN); + EXPECT_EQ(report.message, "There is a possibility of oscillation in a local minimum"); +} + +TEST(ScanMatchDiagnostics, WarnsWhenScoreIsBelowThreshold) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.score = 2.2; // below score_threshold (2.3) + values.is_ok_score = false; + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::WARN); + EXPECT_EQ(report.message, "Score is below the threshold. Score: 2.2, Threshold: 2.3"); +} + +TEST(ScanMatchDiagnostics, ReportsErrorWhenConvergedParamTypeIsUnknown) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.is_known_converged_param_type = false; + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::ERROR); + EXPECT_EQ( + report.message, + "Unknown converged param type. Please check `score_estimation.converged_param_type`"); +} + +TEST(ScanMatchDiagnostics, OmitsKeysAfterTheScoreCheckWhenConvergedParamTypeIsUnknown) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.is_known_converged_param_type = false; + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + // Only the keys added before the converged param type check; the score arrays, distance and + // execution time are skipped. + const std::vector expected{ + "iteration_num", "local_optimal_solution_oscillation_num", "transform_probability", + "nearest_voxel_transformation_likelihood"}; + EXPECT_EQ(keys_of(report), expected); +} + +TEST(ScanMatchDiagnostics, ReportsFirstToLastChangeOfTransformProbability) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + + // Act + const DiagnosticsReport report = diagnostics.make_report(make_healthy_values()); + + // Assert + EXPECT_DOUBLE_EQ(double_value_of(report, "transform_probability_diff"), 1.0); + EXPECT_DOUBLE_EQ(double_value_of(report, "transform_probability_before"), 2.0); +} + +TEST(ScanMatchDiagnostics, WarnsWhenScoreArraySizeDoesNotMatchIterationNum) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.transform_probability_array = {2.0f, 3.0f}; // iteration_num (3) + 1 entries expected + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::WARN); + EXPECT_EQ( + report.message, + "transform_probability_array size is not equal to iteration_num + 1. " + "transform_probability_array size: 2, iteration_num: 3"); +} + +TEST(ScanMatchDiagnostics, WarnsWhenDistanceInitialToResultExceedsTolerance) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.distance_initial_to_result = 3.5; // above the tolerance (3.0) + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::WARN); + EXPECT_EQ(report.message, "distance_initial_to_result is too large (3.5 [m])."); +} + +TEST(ScanMatchDiagnostics, ReportsOkWhenDistanceInitialToResultEqualsTolerance) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.distance_initial_to_result = 3.0; // equal to the tolerance (3.0) + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::OK); + EXPECT_TRUE(report.message.empty()); +} + +TEST(ScanMatchDiagnostics, WarnsWhenExecutionTimeExceedsUpperBound) +{ + // Arrange + const ScanMatchDiagnostics diagnostics(make_default_config()); + auto values = make_healthy_values(); + values.execution_time_ms = 150.0f; // above the upper bound (100 ms) + + // Act + const DiagnosticsReport report = diagnostics.make_report(values); + + // Assert + EXPECT_EQ(report.level, DiagnosticLevel::WARN); + EXPECT_EQ(report.message, "NDT exe time is too long (took 150 [ms])."); +}