-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(localization_util): add covariance_ellipse to localization_u…
…til (#7706) Added covariance_ellipse to localization_util Signed-off-by: Shintaro Sakoda <[email protected]>
- Loading branch information
1 parent
b261aec
commit 10c1a85
Showing
6 changed files
with
143 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
localization/localization_util/include/localization_util/covariance_ellipse.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 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 LOCALIZATION_UTIL__COVARIANCE_ELLIPSE_HPP_ | ||
#define LOCALIZATION_UTIL__COVARIANCE_ELLIPSE_HPP_ | ||
|
||
#include <Eigen/Dense> | ||
|
||
#include <geometry_msgs/msg/pose_with_covariance.hpp> | ||
#include <visualization_msgs/msg/marker_array.hpp> | ||
|
||
namespace autoware::localization_util | ||
{ | ||
|
||
struct Ellipse | ||
{ | ||
double long_radius; | ||
double short_radius; | ||
double yaw; | ||
Eigen::Matrix2d P; | ||
double size_lateral_direction; | ||
}; | ||
|
||
Ellipse calculate_xy_ellipse( | ||
const geometry_msgs::msg::PoseWithCovariance & pose_with_covariance, const double scale); | ||
|
||
visualization_msgs::msg::Marker create_ellipse_marker( | ||
const Ellipse & ellipse, const std_msgs::msg::Header & header, | ||
const geometry_msgs::msg::PoseWithCovariance & pose_with_covariance); | ||
|
||
} // namespace autoware::localization_util | ||
|
||
#endif // LOCALIZATION_UTIL__COVARIANCE_ELLIPSE_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2024 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 "localization_util/covariance_ellipse.hpp" | ||
|
||
#include <tf2/utils.h> | ||
#ifdef ROS_DISTRO_GALACTIC | ||
#include <tf2_geometry_msgs/tf2_geometry_msgs.h> | ||
#else | ||
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp> | ||
#endif | ||
|
||
namespace autoware::localization_util | ||
{ | ||
|
||
Ellipse calculate_xy_ellipse( | ||
const geometry_msgs::msg::PoseWithCovariance & pose_with_covariance, const double scale) | ||
{ | ||
// input geometry_msgs::PoseWithCovariance contain 6x6 matrix | ||
Eigen::Matrix2d xy_covariance; | ||
const auto cov = pose_with_covariance.covariance; | ||
xy_covariance(0, 0) = cov[0 * 6 + 0]; | ||
xy_covariance(0, 1) = cov[0 * 6 + 1]; | ||
xy_covariance(1, 0) = cov[1 * 6 + 0]; | ||
xy_covariance(1, 1) = cov[1 * 6 + 1]; | ||
|
||
Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> eigensolver(xy_covariance); | ||
|
||
Ellipse ellipse; | ||
|
||
// eigen values and vectors are sorted in ascending order | ||
ellipse.long_radius = scale * std::sqrt(eigensolver.eigenvalues()(1)); | ||
ellipse.short_radius = scale * std::sqrt(eigensolver.eigenvalues()(0)); | ||
|
||
// principal component vector | ||
const Eigen::Vector2d pc_vector = eigensolver.eigenvectors().col(1); | ||
ellipse.yaw = std::atan2(pc_vector.y(), pc_vector.x()); | ||
|
||
// ellipse size along lateral direction (body-frame) | ||
ellipse.P = xy_covariance; | ||
const double yaw_vehicle = tf2::getYaw(pose_with_covariance.pose.orientation); | ||
const Eigen::Matrix2d & p_inv = ellipse.P.inverse(); | ||
Eigen::MatrixXd e(2, 1); | ||
e(0, 0) = std::cos(yaw_vehicle); | ||
e(1, 0) = std::sin(yaw_vehicle); | ||
const double d = std::sqrt((e.transpose() * p_inv * e)(0, 0) / p_inv.determinant()); | ||
ellipse.size_lateral_direction = scale * d; | ||
|
||
return ellipse; | ||
} | ||
|
||
visualization_msgs::msg::Marker create_ellipse_marker( | ||
const Ellipse & ellipse, const std_msgs::msg::Header & header, | ||
const geometry_msgs::msg::PoseWithCovariance & pose_with_covariance) | ||
{ | ||
tf2::Quaternion quat; | ||
quat.setEuler(0, 0, ellipse.yaw); | ||
|
||
const double ellipse_long_radius = std::min(ellipse.long_radius, 30.0); | ||
const double ellipse_short_radius = std::min(ellipse.short_radius, 30.0); | ||
visualization_msgs::msg::Marker marker; | ||
marker.header = header; | ||
marker.ns = "error_ellipse"; | ||
marker.id = 0; | ||
marker.type = visualization_msgs::msg::Marker::SPHERE; | ||
marker.action = visualization_msgs::msg::Marker::ADD; | ||
marker.pose = pose_with_covariance.pose; | ||
marker.pose.orientation = tf2::toMsg(quat); | ||
marker.scale.x = ellipse_long_radius * 2; | ||
marker.scale.y = ellipse_short_radius * 2; | ||
marker.scale.z = 0.01; | ||
marker.color.a = 0.1; | ||
marker.color.r = 0.0; | ||
marker.color.g = 0.0; | ||
marker.color.b = 1.0; | ||
return marker; | ||
} | ||
|
||
} // namespace autoware::localization_util |