Skip to content

Commit 1ebba96

Browse files
liuXinGangChinapre-commit-ci[bot]SakodaShintaroyoutalk
authored
feat: migrate autoware_localization_util from universe to core (#150)
Signed-off-by: liuXinGangChina <lxg19892021@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: SakodaShintaro <shintaro.sakoda@tier4.jp> Co-authored-by: Yutaka Kondo <yutaka.kondo@youtalk.jp>
1 parent e415623 commit 1ebba96

14 files changed

+1412
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(autoware_localization_util)
3+
4+
find_package(autoware_cmake REQUIRED)
5+
autoware_package()
6+
7+
include_directories(
8+
include
9+
)
10+
11+
ament_auto_add_library(${PROJECT_NAME} SHARED
12+
src/util_func.cpp
13+
src/smart_pose_buffer.cpp
14+
src/tree_structured_parzen_estimator.cpp
15+
src/covariance_ellipse.cpp
16+
)
17+
18+
if(BUILD_TESTING)
19+
find_package(ament_cmake_gtest REQUIRED)
20+
ament_auto_add_gtest(test_smart_pose_buffer
21+
test/test_smart_pose_buffer.cpp
22+
src/smart_pose_buffer.cpp
23+
)
24+
25+
ament_auto_add_gtest(test_tpe
26+
test/test_tpe.cpp
27+
src/tree_structured_parzen_estimator.cpp
28+
)
29+
endif()
30+
31+
ament_auto_package(
32+
INSTALL_TO_SHARE
33+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# autoware_localization_util
2+
3+
## Overview
4+
5+
`autoware_localization_util` is a collection of localization utility packages. It contains 5 individual library that used by autoware localization nodes.
6+
7+
- `covariance_ellipse` 2d covariance visualization wrapper.
8+
- `smart_pose_buffer` pose buffer management library which contains interpolate and data validation.
9+
- `tree_structured_parzen_estimator` A Tree Structured Parzen Estimator library.
10+
- `util_func` A tool library which contains several function for localization nodes.
11+
12+
## Design
13+
14+
- `covariance_ellipse` Translate `geometry_msgs::msg::PoseWithCovariance` message into ellipse visual marker to demonstrate covariance distribution.
15+
- `smart_pose_buffer` A buffer library which implements pose message buffering, pose interpolate and pose validation.
16+
- `tree_structured_parzen_estimator` A Probability Estimator library that includes estimator and log likelihood ratio calculation.
17+
- `util_func` Tool function collection.
18+
19+
## Usage
20+
21+
### covariance_ellipse
22+
23+
Include header file to use:
24+
25+
```cpp
26+
#include "autoware/localization_util/covariance_ellipse.hpp"
27+
```
28+
29+
calculate ellipse and visualize
30+
31+
```cpp
32+
autoware::localization_util::Ellipse ellipse_ = autoware::localization_util::calculate_xy_ellipse(input_msg->pose, scale_);
33+
34+
const auto ellipse_marker = autoware::localization_util::create_ellipse_marker(
35+
ellipse_, input_msg->header, input_msg->pose);
36+
```
37+
38+
### smart_pose_buffer
39+
40+
buffer init
41+
42+
```cpp
43+
#include "autoware/localization_util/smart_pose_buffer.hpp"
44+
45+
using autoware::localization_util::SmartPoseBuffer;
46+
47+
std::unique_ptr<autoware::localization_util::SmartPoseBuffer> initial_pose_buffer_;
48+
initial_pose_buffer_ = std::make_unique<SmartPoseBuffer>(
49+
this->get_logger(), param_.validation.initial_pose_timeout_sec,
50+
param_.validation.initial_pose_distance_tolerance_m);
51+
```
52+
53+
interpolate and pop out old pose message
54+
55+
```cpp
56+
std::optional<SmartPoseBuffer::InterpolateResult> interpolation_result_opt =
57+
initial_pose_buffer_->interpolate(sensor_ros_time);
58+
59+
...
60+
61+
initial_pose_buffer_->pop_old(sensor_ros_time);
62+
const SmartPoseBuffer::InterpolateResult & interpolation_result =
63+
interpolation_result_opt.value();
64+
```
65+
66+
clear buffer
67+
68+
```cpp
69+
initial_pose_buffer_->clear();
70+
```
71+
72+
### tree_structured_parzen_estimator
73+
74+
init the estimator.
75+
n_startup_trials -- The number of initial random trials in the TPE (Tree-Structured Parzen Estimator). This value should be equal to or less than 'initial_estimate_particles_num' and more than 0. If it is equal to 'initial_estimate_particles_num', the search will be the same as a full random search.
76+
77+
```cpp
78+
#include "autoware/localization_util/tree_structured_parzen_estimator.hpp"
79+
80+
using autoware::localization_util::TreeStructuredParzenEstimator;
81+
82+
TreeStructuredParzenEstimator tpe(
83+
TreeStructuredParzenEstimator::Direction::MAXIMIZE,
84+
param_.initial_pose_estimation.n_startup_trials, sample_mean, sample_stddev);
85+
```
86+
87+
get estimation result
88+
89+
```cpp
90+
const TreeStructuredParzenEstimator::Input input = tpe.get_next_input();
91+
```
92+
93+
add new data to the estimator
94+
95+
```cpp
96+
TreeStructuredParzenEstimator::Input result(6);
97+
result[0] = pose.position.x;
98+
result[1] = pose.position.y;
99+
result[2] = pose.position.z;
100+
result[3] = rpy.x;
101+
result[4] = rpy.y;
102+
result[5] = rpy.z;
103+
tpe.add_trial(TreeStructuredParzenEstimator::Trial{result, ndt_result.transform_probability});
104+
```
105+
106+
### util_func
107+
108+
include header file to use
109+
110+
```cpp
111+
#include "autoware/localization_util/util_func.hpp"
112+
113+
using autoware::localization_util::exchange_color_crc;
114+
using autoware::localization_util::matrix4f_to_pose;
115+
using autoware::localization_util::point_to_vector3d;
116+
using autoware::localization_util::pose_to_matrix4f;
117+
```
118+
119+
list of useful function
120+
121+
```cpp
122+
std_msgs::msg::ColorRGBA exchange_color_crc(double x);
123+
double calc_diff_for_radian(const double lhs_rad, const double rhs_rad);
124+
geometry_msgs::msg::Vector3 get_rpy(const geometry_msgs::msg::Pose & pose);
125+
geometry_msgs::msg::Vector3 get_rpy(const geometry_msgs::msg::PoseStamped & pose);
126+
geometry_msgs::msg::Vector3 get_rpy(const geometry_msgs::msg::PoseWithCovarianceStamped & pose);
127+
geometry_msgs::msg::Quaternion rpy_rad_to_quaternion(
128+
const double r_rad, const double p_rad, const double y_rad);
129+
geometry_msgs::msg::Quaternion rpy_deg_to_quaternion(
130+
const double r_deg, const double p_deg, const double y_deg);
131+
geometry_msgs::msg::Twist calc_twist(
132+
const geometry_msgs::msg::PoseStamped & pose_a, const geometry_msgs::msg::PoseStamped & pose_b);
133+
geometry_msgs::msg::PoseStamped interpolate_pose(
134+
const geometry_msgs::msg::PoseStamped & pose_a, const geometry_msgs::msg::PoseStamped & pose_b,
135+
const rclcpp::Time & time_stamp);
136+
geometry_msgs::msg::PoseStamped interpolate_pose(
137+
const geometry_msgs::msg::PoseWithCovarianceStamped & pose_a,
138+
const geometry_msgs::msg::PoseWithCovarianceStamped & pose_b, const rclcpp::Time & time_stamp);
139+
Eigen::Affine3d pose_to_affine3d(const geometry_msgs::msg::Pose & ros_pose);
140+
Eigen::Matrix4f pose_to_matrix4f(const geometry_msgs::msg::Pose & ros_pose);
141+
geometry_msgs::msg::Pose matrix4f_to_pose(const Eigen::Matrix4f & eigen_pose_matrix);
142+
Eigen::Vector3d point_to_vector3d(const geometry_msgs::msg::Point & ros_pos);
143+
template <class T>
144+
T transform(const T & input, const geometry_msgs::msg::TransformStamped & transform);double norm(const geometry_msgs::msg::Point & p1, const geometry_msgs::msg::Point & p2);
145+
146+
void output_pose_with_cov_to_log(
147+
const rclcpp::Logger & logger, const std::string & prefix,
148+
const geometry_msgs::msg::PoseWithCovarianceStamped & pose_with_cov);
149+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2024 Autoware Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE__LOCALIZATION_UTIL__COVARIANCE_ELLIPSE_HPP_
16+
#define AUTOWARE__LOCALIZATION_UTIL__COVARIANCE_ELLIPSE_HPP_
17+
18+
#include <Eigen/Dense>
19+
20+
#include <geometry_msgs/msg/pose_with_covariance.hpp>
21+
#include <visualization_msgs/msg/marker_array.hpp>
22+
23+
#include <vector>
24+
25+
namespace autoware::localization_util
26+
{
27+
28+
struct Ellipse
29+
{
30+
double long_radius;
31+
double short_radius;
32+
double yaw;
33+
Eigen::Matrix2d P;
34+
double size_lateral_direction;
35+
};
36+
37+
Ellipse calculate_xy_ellipse(
38+
const geometry_msgs::msg::PoseWithCovariance & pose_with_covariance, const double scale);
39+
40+
visualization_msgs::msg::Marker create_ellipse_marker(
41+
const Ellipse & ellipse, const std_msgs::msg::Header & header,
42+
const geometry_msgs::msg::PoseWithCovariance & pose_with_covariance);
43+
44+
} // namespace autoware::localization_util
45+
46+
#endif // AUTOWARE__LOCALIZATION_UTIL__COVARIANCE_ELLIPSE_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2021 TierIV
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE__LOCALIZATION_UTIL__MATRIX_TYPE_HPP_
16+
#define AUTOWARE__LOCALIZATION_UTIL__MATRIX_TYPE_HPP_
17+
18+
#include <Eigen/Core>
19+
20+
namespace autoware::localization_util
21+
{
22+
using Matrix6d = Eigen::Matrix<double, 6, 6>;
23+
using RowMatrixXd = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
24+
} // namespace autoware::localization_util
25+
26+
#endif // AUTOWARE__LOCALIZATION_UTIL__MATRIX_TYPE_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2015-2019 Autoware Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE__LOCALIZATION_UTIL__SMART_POSE_BUFFER_HPP_
16+
#define AUTOWARE__LOCALIZATION_UTIL__SMART_POSE_BUFFER_HPP_
17+
18+
#include "autoware/localization_util/util_func.hpp"
19+
20+
#include <rclcpp/rclcpp.hpp>
21+
22+
#include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
23+
24+
#include <deque>
25+
26+
namespace autoware::localization_util
27+
{
28+
class SmartPoseBuffer
29+
{
30+
private:
31+
using PoseWithCovarianceStamped = geometry_msgs::msg::PoseWithCovarianceStamped;
32+
33+
public:
34+
struct InterpolateResult
35+
{
36+
PoseWithCovarianceStamped old_pose;
37+
PoseWithCovarianceStamped new_pose;
38+
PoseWithCovarianceStamped interpolated_pose;
39+
};
40+
41+
SmartPoseBuffer() = delete;
42+
SmartPoseBuffer(
43+
const rclcpp::Logger & logger, const double & pose_timeout_sec,
44+
const double & pose_distance_tolerance_meters);
45+
46+
std::optional<InterpolateResult> interpolate(const rclcpp::Time & target_ros_time);
47+
48+
void push_back(const PoseWithCovarianceStamped::ConstSharedPtr & pose_msg_ptr);
49+
50+
void pop_old(const rclcpp::Time & target_ros_time);
51+
52+
void clear();
53+
54+
private:
55+
rclcpp::Logger logger_;
56+
std::deque<PoseWithCovarianceStamped::ConstSharedPtr> pose_buffer_;
57+
std::mutex mutex_; // This mutex is for pose_buffer_
58+
59+
const double pose_timeout_sec_;
60+
const double pose_distance_tolerance_meters_;
61+
62+
[[nodiscard]] bool validate_time_stamp_difference(
63+
const rclcpp::Time & target_time, const rclcpp::Time & reference_time,
64+
const double time_tolerance_sec) const;
65+
[[nodiscard]] bool validate_position_difference(
66+
const geometry_msgs::msg::Point & target_point,
67+
const geometry_msgs::msg::Point & reference_point, const double distance_tolerance_m_) const;
68+
};
69+
} // namespace autoware::localization_util
70+
71+
#endif // AUTOWARE__LOCALIZATION_UTIL__SMART_POSE_BUFFER_HPP_

0 commit comments

Comments
 (0)