Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions localization/autoware_ndt_scan_matcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ include_directories(${PCL_INCLUDE_DIRS})
ament_auto_add_library(multigrid_ndt_omp SHARED
src/ndt_omp/multi_voxel_grid_covariance_omp.cpp
src/ndt_omp/multigrid_ndt_omp.cpp
src/ndt_omp/estimate_covariance.cpp
)
target_link_libraries(multigrid_ndt_omp ${PCL_LIBRARIES})

Expand All @@ -42,16 +41,19 @@ else()
message(WARNING "OpenMP not found")
endif()

ament_auto_add_library(${PROJECT_NAME} SHARED
# NO_TARGET_LINK_LIBRARIES: the node does not use ndt_omp. Only the backend "ndt_omp" does.
ament_auto_add_library(${PROJECT_NAME} SHARED NO_TARGET_LINK_LIBRARIES
src/map_update_module.cpp
src/ndt_backend_loader.cpp
src/ndt_omp/estimate_covariance.cpp
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})
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES} multigrid_ndt_omp)
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES})

autoware_agnocast_wrapper_register_node(${PROJECT_NAME}
PLUGIN "autoware::ndt_scan_matcher::NDTScanMatcher"
Expand All @@ -60,7 +62,52 @@ autoware_agnocast_wrapper_register_node(${PROJECT_NAME}
AGNOCAST_EXECUTOR AgnocastOnlyCallbackIsolatedExecutor
)

# The NDT backend "ndt_omp". The node loads it with pluginlib, so no other library may link it. It
# is a plain library: ament_auto exports every library that ament_auto_add_library() adds, and it
# links them into the later libraries and tests of this package and into the packages that depend on
# it.
add_library(${PROJECT_NAME}_omp_backend SHARED
src/omp_ndt_backend.cpp
)
target_link_libraries(${PROJECT_NAME}_omp_backend multigrid_ndt_omp ${PCL_LIBRARIES})
ament_target_dependencies(${PROJECT_NAME}_omp_backend autoware_ndt_scan_matcher_common pluginlib)
install(TARGETS ${PROJECT_NAME}_omp_backend
LIBRARY DESTINATION lib
)
pluginlib_export_plugin_description_file(autoware_ndt_scan_matcher_common plugins.xml)

if(BUILD_TESTING)
# Three test backends in an install prefix inside the build directory, for the tests of the
# error messages. pluginlib finds them only in a test that has this prefix in AMENT_PREFIX_PATH.
set(test_backend_prefix ${CMAKE_CURRENT_BINARY_DIR}/test_ndt_backend_prefix)
set(test_backend_index ${test_backend_prefix}/share/ament_index/resource_index)
file(WRITE ${test_backend_index}/packages/test_ndt_backend "")
file(WRITE
${test_backend_index}/autoware_ndt_scan_matcher_common__pluginlib__plugin/test_ndt_backend
"share/test_ndt_backend/test_ndt_backend_plugins.xml\n")
file(WRITE ${test_backend_prefix}/share/test_ndt_backend/package.xml
"<package format=\"3\"><name>test_ndt_backend</name></package>\n")
# configure_file() copies the file again when it changes.
# cspell: ignore COPYONLY
configure_file(test/test_ndt_backend_plugins.xml
${test_backend_prefix}/share/test_ndt_backend/test_ndt_backend_plugins.xml COPYONLY)
foreach(variant constructor_throws initialize_throws other_abi)
add_library(test_ndt_backend_${variant} MODULE test/test_ndt_backend.cpp)
ament_target_dependencies(test_ndt_backend_${variant}
autoware_ndt_scan_matcher_common pluginlib)
# $<1:...> keeps a multi-config generator from adding a directory for each configuration.
set_target_properties(test_ndt_backend_${variant} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY $<1:${test_backend_prefix}/lib>)
endforeach()
target_compile_definitions(test_ndt_backend_constructor_throws PRIVATE
TEST_NDT_BACKEND_CONSTRUCTOR_THROWS)
target_compile_definitions(test_ndt_backend_initialize_throws PRIVATE
TEST_NDT_BACKEND_INITIALIZE_THROWS)
# The other heap allocator of Eigen. test_ndt_backend_loader skips its test in a build where
# this definition does not change the allocator.
target_compile_definitions(test_ndt_backend_other_abi PRIVATE
EIGEN_MALLOC_ALREADY_ALIGNED=0)

if(NOT (DEFINED ENV{ENABLE_AGNOCAST} AND "$ENV{ENABLE_AGNOCAST}" STREQUAL "1"))
add_launch_test(
test/test_ndt_scan_matcher_launch.py
Expand Down Expand Up @@ -100,7 +147,9 @@ if(BUILD_TESTING)
ament_add_ros_isolated_gtest(test_ndt_scan_matcher_characteristics
test/test_ndt_scan_matcher_characteristics.cpp
TIMEOUT "600"
APPEND_ENV AMENT_PREFIX_PATH=${test_backend_prefix}
)
add_dependencies(test_ndt_scan_matcher_characteristics test_ndt_backend_initialize_throws)
target_link_libraries(test_ndt_scan_matcher_characteristics ${PROJECT_NAME})
ament_target_dependencies(test_ndt_scan_matcher_characteristics
ament_index_cpp rcl_yaml_param_parser)
Expand All @@ -110,7 +159,7 @@ if(BUILD_TESTING)
ament_auto_add_gtest(test_estimate_covariance
test/test_estimate_covariance.cpp
)
target_link_libraries(test_estimate_covariance multigrid_ndt_omp ${PCL_LIBRARIES})
target_link_libraries(test_estimate_covariance ${PROJECT_NAME} ${PCL_LIBRARIES})

ament_auto_add_gtest(test_ndt_scan_matcher_helper
test/test_ndt_scan_matcher_helper.cpp
Expand All @@ -126,7 +175,23 @@ if(BUILD_TESTING)
ament_auto_add_gtest(test_map_update_module
test/test_map_update_module.cpp
)
target_link_libraries(test_map_update_module ${PROJECT_NAME} multigrid_ndt_omp ${PCL_LIBRARIES})
target_link_libraries(test_map_update_module ${PROJECT_NAME} ${PCL_LIBRARIES})

# The OMP backend against ndt_omp, and its copies.
ament_auto_add_gtest(test_omp_ndt_backend
test/test_omp_ndt_backend.cpp
)
target_include_directories(test_omp_ndt_backend PRIVATE src)
target_link_libraries(test_omp_ndt_backend ${PROJECT_NAME}_omp_backend ${PCL_LIBRARIES})

# pluginlib loads the backend library here. The test does not link it.
ament_auto_add_gtest(test_ndt_backend_loader
test/test_ndt_backend_loader.cpp
APPEND_ENV AMENT_PREFIX_PATH=${test_backend_prefix}
)
target_link_libraries(test_ndt_backend_loader ${PROJECT_NAME} ${CMAKE_DL_LIBS})
add_dependencies(test_ndt_backend_loader
test_ndt_backend_constructor_throws test_ndt_backend_other_abi)
endif()

autoware_ament_auto_package(
Expand Down
8 changes: 8 additions & 0 deletions localization/autoware_ndt_scan_matcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ One optional function is regularization. Please see the regularization chapter i

{{ json_to_markdown("localization/autoware_ndt_scan_matcher/schema/sub/covariance.json") }}

## NDT backends

The node does the NDT computations through a backend, which it loads with [pluginlib](https://github.com/ros/pluginlib). The parameter `ndt.backend` sets the name of the backend. This package has one backend, `ndt_omp`, which is ndt_omp on the CPU. The parameter file of this package sets `ndt_omp`.

A backend is a class that implements `autoware::ndt_scan_matcher::NdtBackend` from [autoware_ndt_scan_matcher_common](../autoware_ndt_scan_matcher_common/README.md). That README explains how another package adds a backend.

If the node cannot load the backend, the node throws an exception at startup. For an unknown name, the message lists the available backends.

## Regularization

### Abstract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
#ifndef AUTOWARE__NDT_SCAN_MATCHER__HYPER_PARAMETERS_HPP_
#define AUTOWARE__NDT_SCAN_MATCHER__HYPER_PARAMETERS_HPP_

#include "ndt_omp/multigrid_ndt_omp.h"
#include "autoware/ndt_scan_matcher_common/ndt_types.hpp"

#include <autoware/agnocast_wrapper/node.hpp>
#include <rclcpp/rclcpp.hpp>

#include <algorithm>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

Expand Down Expand Up @@ -55,7 +56,8 @@ struct HyperParameters
double required_distance{};
} sensor_points{};

pclomp::NdtParams ndt{};
NdtParams ndt{};
std::string ndt_backend{};
bool ndt_regularization_enable{};

struct InitialPoseEstimation
Expand Down Expand Up @@ -124,6 +126,14 @@ struct HyperParameters
ndt.max_iterations = static_cast<int>(node->declare_parameter<int64_t>("ndt.max_iterations"));
ndt.num_threads = static_cast<int>(node->declare_parameter<int64_t>("ndt.num_threads"));
ndt.num_threads = std::max(ndt.num_threads, 1);
try {
ndt_backend = node->declare_parameter<std::string>("ndt.backend");
} catch (const rclcpp::ParameterTypeException &) {
// The parameter is new, so a parameter file from before it does not have it.
throw std::runtime_error(
"The parameter 'ndt.backend' is not set. Add the line `backend: \"ndt_omp\"` under `ndt:` "
"in the parameter file of autoware_ndt_scan_matcher.");
}
ndt_regularization_enable = node->declare_parameter<bool>("ndt.regularization.enable");
ndt.regularization_scale_factor =
static_cast<float>(node->declare_parameter<float>("ndt.regularization.scale_factor"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#ifndef AUTOWARE__NDT_SCAN_MATCHER__MAP_UPDATE_MODULE_HPP_
#define AUTOWARE__NDT_SCAN_MATCHER__MAP_UPDATE_MODULE_HPP_

#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"
#include "diagnostics_report.hpp"
#include "guarded.hpp"
#include "hyper_parameters.hpp"
#include "ndt_omp/multigrid_ndt_omp.h"

#include <autoware_map_msgs/srv/get_differential_point_cloud_map.hpp>
#include <geometry_msgs/msg/point.hpp>
Expand Down Expand Up @@ -46,8 +46,8 @@ class MapUpdateModule
public:
using PointSource = pcl::PointXYZ;
using PointTarget = pcl::PointXYZ;
using NdtType = pclomp::MultiGridNormalDistributionsTransform<PointSource, PointTarget>;
using NdtPtrType = std::shared_ptr<NdtType>;
using NdtType = NdtBackend;
using NdtPtrType = NdtBackend::SharedPtr;

using GetDifferentialPointCloudMap = autoware_map_msgs::srv::GetDifferentialPointCloudMap;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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__NDT_BACKEND_LOADER_HPP_
#define AUTOWARE__NDT_SCAN_MATCHER__NDT_BACKEND_LOADER_HPP_

#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"

#include <string>

namespace autoware::ndt_scan_matcher
{

/// Makes the NDT backend with this pluginlib lookup name. Throws std::runtime_error if the backend
/// cannot be made. For an unknown name, the message lists the declared backends.
///
/// A backend library stays loaded until the process ends, so instances that a backend makes
/// without pluginlib (for example, in clone()) stay valid.
NdtBackend::SharedPtr load_ndt_backend(const std::string & backend_name);

} // namespace autoware::ndt_scan_matcher

#endif // AUTOWARE__NDT_SCAN_MATCHER__NDT_BACKEND_LOADER_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef AUTOWARE__NDT_SCAN_MATCHER__NDT_OMP__ESTIMATE_COVARIANCE_HPP_
#define AUTOWARE__NDT_SCAN_MATCHER__NDT_OMP__ESTIMATE_COVARIANCE_HPP_

#include "multigrid_ndt_omp.h"
#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"

#include <Eigen/Core>

Expand All @@ -31,21 +31,21 @@ struct ResultOfMultiNdtCovarianceEstimation
Eigen::Vector2d mean;
Eigen::Matrix2d covariance;
std::vector<Eigen::Matrix4f> ndt_initial_poses;
std::vector<NdtResult> ndt_results;
std::vector<autoware::ndt_scan_matcher::NdtResult> ndt_results;
};

/** \brief Estimate functions */
Eigen::Matrix2d estimate_xy_covariance_by_laplace_approximation(
const Eigen::Matrix<double, 6, 6> & hessian);
ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt(
const NdtResult & ndt_result,
pclomp::MultiGridNormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ> & ndt_ref,
const autoware::ndt_scan_matcher::NdtResult & ndt_result,
autoware::ndt_scan_matcher::NdtBackend & ndt_ref,

const std::vector<Eigen::Matrix4f> & poses_to_search,
const pcl::shared_ptr<const pcl::PointCloud<pcl::PointXYZ>> & source);
ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
const NdtResult & ndt_result,
pclomp::MultiGridNormalDistributionsTransform<pcl::PointXYZ, pcl::PointXYZ> & ndt_ref,
const autoware::ndt_scan_matcher::NdtResult & ndt_result,
autoware::ndt_scan_matcher::NdtBackend & ndt_ref,
const std::vector<Eigen::Matrix4f> & poses_to_search,
const pcl::shared_ptr<const pcl::PointCloud<pcl::PointXYZ>> & source, const double temperature);

Expand All @@ -55,7 +55,7 @@ ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
* (3) Propose search points by adding offset_x and offset_y to the center_pose
*/
std::vector<Eigen::Matrix4f> propose_poses_to_search(
const NdtResult & ndt_result, const std::vector<double> & offset_x,
const autoware::ndt_scan_matcher::NdtResult & ndt_result, const std::vector<double> & offset_x,
const std::vector<double> & offset_y);

/** \brief Calculate weights by exponential */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

#define FMT_HEADER_ONLY

#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"
#include "guarded.hpp"
#include "hyper_parameters.hpp"
#include "map_update_module.hpp"
#include "ndt_omp/multigrid_ndt_omp.h"
#include "scan_match_diagnostics.hpp"

#include <autoware/agnocast_wrapper/node.hpp>
Expand Down Expand Up @@ -73,8 +73,6 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
{
using PointSource = pcl::PointXYZ;
using PointTarget = pcl::PointXYZ;
using NormalDistributionsTransform =
pclomp::MultiGridNormalDistributionsTransform<PointSource, PointTarget>;

public:
explicit NDTScanMatcher(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
Expand Down Expand Up @@ -121,7 +119,7 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node

std::tuple<geometry_msgs::msg::PoseWithCovarianceStamped, double> align_pose(
const geometry_msgs::msg::PoseWithCovarianceStamped & initial_pose_with_cov,
NormalDistributionsTransform & ndt_ref);
NdtBackend & ndt_ref);

void transform_sensor_measurement(
const std::string & source_frame, const std::string & target_frame,
Expand All @@ -138,7 +136,7 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
const pcl::shared_ptr<pcl::PointCloud<PointSource>> & sensor_points_in_map_ptr);
void publish_marker(
const rclcpp::Time & sensor_ros_time, const std::vector<geometry_msgs::msg::Pose> & pose_array,
NormalDistributionsTransform & ndt_ref);
NdtBackend & ndt_ref);
void publish_initial_to_result(
const rclcpp::Time & sensor_ros_time, const geometry_msgs::msg::Pose & result_pose_msg,
const geometry_msgs::msg::PoseWithCovarianceStamped & initial_pose_cov_msg,
Expand All @@ -150,15 +148,16 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
static int count_oscillation(const std::vector<geometry_msgs::msg::Pose> & result_pose_msg_array);

Eigen::Matrix2d estimate_covariance(
const pclomp::NdtResult & ndt_result, const Eigen::Matrix4f & initial_pose_matrix,
const rclcpp::Time & sensor_ros_time, NormalDistributionsTransform & ndt_ref);
const NdtResult & ndt_result, const Eigen::Matrix4f & initial_pose_matrix,
const rclcpp::Time & sensor_ros_time, NdtBackend & ndt_ref);

pcl::PointCloud<pcl::PointXYZRGB>::Ptr visualize_point_score(
const pcl::shared_ptr<pcl::PointCloud<PointSource>> & sensor_points_in_map_ptr,
const float & lower_nvs, const float & upper_nvs, NormalDistributionsTransform & ndt_ref);
const float & lower_nvs, const float & upper_nvs, NdtBackend & ndt_ref);

void add_regularization_pose(
const rclcpp::Time & sensor_ros_time, NormalDistributionsTransform & ndt_ref);
void add_regularization_pose(const rclcpp::Time & sensor_ros_time, NdtBackend & ndt_ref);

NdtBackend::SharedPtr create_ndt_backend(const std::string & backend_name);

// Performs the pcd_loader service call for MapUpdateModule, returning nullptr on failure.
MapUpdateModule::GetDifferentialPointCloudMap::Response::ConstSharedPtr
Expand Down Expand Up @@ -219,8 +218,7 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node

rclcpp::CallbackGroup::SharedPtr timer_callback_group_;

Guarded<std::shared_ptr<NormalDistributionsTransform>> ndt_ptr_{
std::make_shared<NormalDistributionsTransform>()};
Guarded<NdtBackend::SharedPtr> ndt_ptr_;

pcl::shared_ptr<pcl::PointCloud<PointSource>> sensor_points_in_baselink_frame_;

Expand Down
3 changes: 3 additions & 0 deletions localization/autoware_ndt_scan_matcher/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<depend>autoware_internal_localization_msgs</depend>
<depend>autoware_localization_util</depend>
<depend>autoware_map_msgs</depend>
<depend>autoware_ndt_scan_matcher_common</depend>
<depend>autoware_qos_utils</depend>
<depend>autoware_utils_diagnostics</depend>
<depend>autoware_utils_geometry</depend>
Expand All @@ -34,6 +35,7 @@
<depend>libpcl-all</depend>
<depend>nav_msgs</depend>
<depend>pcl_conversions</depend>
<depend>pluginlib</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
<depend>sensor_msgs</depend>
Expand All @@ -51,6 +53,7 @@
<test_depend>ament_cmake_ros</test_depend>
<test_depend>ament_index_cpp</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>console_bridge_vendor</test_depend>
<test_depend>launch_testing_ament_cmake</test_depend>
<test_depend>rcl_yaml_param_parser</test_depend>
<test_depend>ros_testing</test_depend>
Expand Down
5 changes: 5 additions & 0 deletions localization/autoware_ndt_scan_matcher/plugins.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<library path="autoware_ndt_scan_matcher_omp_backend">
<class name="ndt_omp" type="autoware::ndt_scan_matcher::OmpNdtBackend" base_class_type="autoware::ndt_scan_matcher::NdtBackend">
<description>NDT on the CPU with OpenMP (ndt_omp).</description>
</class>
</library>
Loading
Loading