Skip to content
Open
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
6 changes: 4 additions & 2 deletions localization/autoware_ekf_localizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ Increasing the number will improve the smoothness of the estimation, but may hav

### 3. Tune gate parameters

EKF performs gating using Mahalanobis distance before updating by observation. The gate size is determined by the `pose_gate_dist` parameter and the `twist_gate_dist`. If the Mahalanobis distance is larger than this value, the observation is ignored.
EKF performs gating using Mahalanobis distance before updating by observation. The distance is computed from the innovation (the difference between the measurement and the predicted state) and its covariance, which is the sum of the predicted state covariance and the measurement covariance. The gate size is determined by the `pose_gate_dist` parameter and the `twist_gate_dist`. If the squared Mahalanobis distance is larger than this value, the observation is ignored.

This gating process is based on a statistical test using the chi-square distribution. As modeled, we assume that the Mahalanobis distance follows a chi-square distribution with 3 degrees of freedom for pose and 2 degrees of freedom for twist.
This gating process is based on a statistical test using the chi-square distribution. As modeled, we assume that the squared Mahalanobis distance follows a chi-square distribution with 3 degrees of freedom for pose and 2 degrees of freedom for twist.

The diagnostics report the unsquared Mahalanobis distance, and the reported threshold is the square root of `pose_gate_dist` or `twist_gate_dist`.

Currently, the accuracy of covariance estimation itself is not very good, so it is recommended to set the significance level to a very small value to reduce rejection due to false positives.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"pose_gate_dist": {
"type": "number",
"description": "Limit of Mahalanobis distance used for outliers detection",
"description": "Limit of the squared Mahalanobis distance of the innovation used for outliers detection (chi-square quantile). Diagnostics report the unsquared distance.",
"default": 49.5
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"twist_gate_dist": {
"type": "number",
"description": "Limit of Mahalanobis distance used for outliers detection",
"description": "Limit of the squared Mahalanobis distance of the innovation used for outliers detection (chi-square quantile). Diagnostics report the unsquared distance.",
"default": 46.1
}
},
Expand Down
34 changes: 18 additions & 16 deletions localization/autoware_ekf_localizer/src/ekf_localizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,16 +599,21 @@ bool EKFLocalizer::measurement_update_pose(
const Eigen::Vector3d y_ekf(
kalman_filter_.getXelement(delay_step * dim_x_ + IDX::X),
kalman_filter_.getXelement(delay_step * dim_x_ + IDX::Y), ekf_yaw);
const Eigen::Matrix<double, 3, 6> c = pose_measurement_matrix();
const Eigen::Matrix3d r =
pose_measurement_covariance(pose.pose.covariance, params_.pose_smoothing_steps);
const Eigen::MatrixXd p_curr = kalman_filter_.getLatestP();
const Eigen::MatrixXd p_y = p_curr.block(0, 0, dim_y, dim_y);
const Eigen::Matrix3d s = c * p_curr * c.transpose() + r;

const double distance = mahalanobis(y_ekf, y, p_y);
const double squared_distance = squared_mahalanobis(y_ekf, y, s);
const double distance = std::sqrt(squared_distance);
pose_diag_info.mahalanobis_distance = std::max(distance, pose_diag_info.mahalanobis_distance);
// is_passed_mahalanobis_gate is initialized true before the first calling measurement_update_pose
// every ekf_localizer call.
if (distance > params_.pose_gate_dist) {
if (squared_distance > params_.pose_gate_dist) {
pose_diag_info.is_passed_mahalanobis_gate = false;
warnings_out.push_back({mahalanobis_warning_message(distance, params_.pose_gate_dist), 2000});
warnings_out.push_back(
{mahalanobis_warning_message(distance, std::sqrt(params_.pose_gate_dist)), 2000});
warnings_out.push_back({"Ignore the measurement data.", 2000});
return false;
}
Expand All @@ -617,10 +622,6 @@ bool EKFLocalizer::measurement_update_pose(
DEBUG_PRINT_MAT(y_ekf.transpose());
DEBUG_PRINT_MAT((y - y_ekf).transpose());

const Eigen::Matrix<double, 3, 6> c = pose_measurement_matrix();
const Eigen::Matrix3d r =
pose_measurement_covariance(pose.pose.covariance, params_.pose_smoothing_steps);

kalman_filter_.updateWithDelay(y, c, r, static_cast<int>(delay_step));

// Update Simple 1D filter with considering change of roll, pitch and height (position z)
Expand Down Expand Up @@ -729,16 +730,21 @@ bool EKFLocalizer::measurement_update_twist(
const Eigen::Vector2d y_ekf(
kalman_filter_.getXelement(delay_step * dim_x_ + IDX::VX),
kalman_filter_.getXelement(delay_step * dim_x_ + IDX::WZ));
const Eigen::Matrix<double, 2, 6> c = twist_measurement_matrix();
const Eigen::Matrix2d r =
twist_measurement_covariance(twist.twist.covariance, params_.twist_smoothing_steps);
const Eigen::MatrixXd p_curr = kalman_filter_.getLatestP();
const Eigen::MatrixXd p_y = p_curr.block(4, 4, dim_y, dim_y);
const Eigen::Matrix2d s = c * p_curr * c.transpose() + r;

const double distance = mahalanobis(y_ekf, y, p_y);
const double squared_distance = squared_mahalanobis(y_ekf, y, s);
const double distance = std::sqrt(squared_distance);
twist_diag_info.mahalanobis_distance = std::max(distance, twist_diag_info.mahalanobis_distance);
// is_passed_mahalanobis_gate is initialized true before the first calling
// measurement_update_twist every ekf_localizer call.
if (distance > params_.twist_gate_dist) {
if (squared_distance > params_.twist_gate_dist) {
twist_diag_info.is_passed_mahalanobis_gate = false;
warnings_out.push_back({mahalanobis_warning_message(distance, params_.twist_gate_dist), 2000});
warnings_out.push_back(
{mahalanobis_warning_message(distance, std::sqrt(params_.twist_gate_dist)), 2000});
warnings_out.push_back({"Ignore the measurement data.", 2000});
return false;
}
Expand All @@ -747,10 +753,6 @@ bool EKFLocalizer::measurement_update_twist(
DEBUG_PRINT_MAT(y_ekf.transpose());
DEBUG_PRINT_MAT((y - y_ekf).transpose());

const Eigen::Matrix<double, 2, 6> c = twist_measurement_matrix();
const Eigen::Matrix2d r =
twist_measurement_covariance(twist.twist.covariance, params_.twist_smoothing_steps);

kalman_filter_.updateWithDelay(y, c, r, static_cast<int>(delay_step));

last_angular_velocity_ = tf2::Vector3(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void EKFLocalizerNode::timer_callback()
update_result.pose_diag_info.delay_time, update_result.pose_diag_info.delay_time_threshold));
diag_status_array.push_back(check_measurement_mahalanobis_gate(
"pose", update_result.pose_diag_info.is_passed_mahalanobis_gate,
update_result.pose_diag_info.mahalanobis_distance, params_.pose_gate_dist));
update_result.pose_diag_info.mahalanobis_distance, std::sqrt(params_.pose_gate_dist)));

// Add twist-related diagnostics after twist processing
diag_status_array.push_back(check_measurement_updated(
Expand All @@ -188,7 +188,7 @@ void EKFLocalizerNode::timer_callback()
update_result.twist_diag_info.delay_time, update_result.twist_diag_info.delay_time_threshold));
diag_status_array.push_back(check_measurement_mahalanobis_gate(
"twist", update_result.twist_diag_info.is_passed_mahalanobis_gate,
update_result.twist_diag_info.mahalanobis_distance, params_.twist_gate_dist));
update_result.twist_diag_info.mahalanobis_distance, std::sqrt(params_.twist_gate_dist)));

diag_status_array.push_back(check_covariance_ellipse(
"cov_ellipse_long_axis", update_result.ellipse_long_radius, params_.warn_ellipse_size,
Expand Down
163 changes: 163 additions & 0 deletions localization/autoware_ekf_localizer/test/test_ekf_localizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "src/ekf_localizer.hpp"
#include "utils/hyper_parameters.hpp"
#include "utils/state_index.hpp"
#include "utils/warning_message.hpp"

#include <autoware_utils_geometry/msg/covariance.hpp>
#include <rclcpp/rclcpp.hpp>
Expand Down Expand Up @@ -626,6 +627,89 @@ TEST_F(MeasurementUpdatePose, RejectsOnMahalanobisGate)
EXPECT_GT(diag.mahalanobis_distance, 0.0);
}

TEST_F(MeasurementUpdatePose, RealisticGateRejectsOutsideChiSquareBound)
{
// Distance lies between sqrt(pose_gate_dist) and pose_gate_dist, so only a squared comparison
// rejects it.
params_.pose_gate_dist = 49.5;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto pose = make_pose(49.0, 0.0, 0.0, "map", t_curr);

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_pose(pose, t_curr.seconds(), diag, warnings);

EXPECT_FALSE(ok);
EXPECT_TRUE(diag.is_passed_delay_gate);
EXPECT_FALSE(diag.is_passed_mahalanobis_gate);
EXPECT_GT(diag.mahalanobis_distance, 15.0);
EXPECT_LT(diag.mahalanobis_distance, 25.0);
ASSERT_FALSE(warnings.empty());
EXPECT_EQ(
warnings.front().text,
mahalanobis_warning_message(diag.mahalanobis_distance, std::sqrt(params_.pose_gate_dist)));
}

TEST_F(MeasurementUpdatePose, RealisticGateAcceptsInsideChiSquareBound)
{
params_.pose_gate_dist = 49.5;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto pose = make_pose(5.0, 0.0, 0.0, "map", t_curr);

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_pose(pose, t_curr.seconds(), diag, warnings);

EXPECT_TRUE(ok);
EXPECT_TRUE(diag.is_passed_mahalanobis_gate);
EXPECT_GT(diag.mahalanobis_distance, 1.0);
EXPECT_LT(diag.mahalanobis_distance, 3.0);
}

TEST_F(MeasurementUpdatePose, GateAcceptsOffsetCoveredByMeasurementCovariance)
{
using COV_IDX = autoware_utils_geometry::xyzrpy_covariance_index::XYZRPY_COV_IDX;
params_.pose_gate_dist = 49.5;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto pose = make_pose(10.0, 0.0, 0.0, "map", t_curr);
pose.pose.covariance[COV_IDX::X_X] = 20.0;
pose.pose.covariance[COV_IDX::Y_Y] = 20.0;

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_pose(pose, t_curr.seconds(), diag, warnings);

EXPECT_TRUE(ok);
EXPECT_TRUE(diag.is_passed_mahalanobis_gate);
EXPECT_LT(diag.mahalanobis_distance, 1.5);
}

TEST_F(MeasurementUpdatePose, GateRejectsSameOffsetWithTightMeasurementCovariance)
{
using COV_IDX = autoware_utils_geometry::xyzrpy_covariance_index::XYZRPY_COV_IDX;
params_.pose_gate_dist = 49.5;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto pose = make_pose(10.0, 0.0, 0.0, "map", t_curr);
pose.pose.covariance[COV_IDX::X_X] = 1e-4;
pose.pose.covariance[COV_IDX::Y_Y] = 1e-4;

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_pose(pose, t_curr.seconds(), diag, warnings);

EXPECT_FALSE(ok);
EXPECT_FALSE(diag.is_passed_mahalanobis_gate);
EXPECT_GT(diag.mahalanobis_distance, 9.0);
}

// ---------------------------------------------------------------------------
// measurement_update_twist: success and safety-critical rejection branches
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -748,4 +832,83 @@ TEST_F(MeasurementUpdateTwist, RejectsOnMahalanobisGate)
EXPECT_GT(diag.mahalanobis_distance, 0.0);
}

TEST_F(MeasurementUpdateTwist, RealisticGateRejectsOutsideChiSquareBound)
{
params_.twist_gate_dist = 46.1;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto twist = make_twist(29.0, 0.0, "base_link", t_curr);

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_twist(twist, t_curr.seconds(), diag, warnings);

EXPECT_FALSE(ok);
EXPECT_TRUE(diag.is_passed_delay_gate);
EXPECT_FALSE(diag.is_passed_mahalanobis_gate);
EXPECT_GT(diag.mahalanobis_distance, 15.0);
EXPECT_LT(diag.mahalanobis_distance, 25.0);
ASSERT_FALSE(warnings.empty());
EXPECT_EQ(
warnings.front().text,
mahalanobis_warning_message(diag.mahalanobis_distance, std::sqrt(params_.twist_gate_dist)));
}

TEST_F(MeasurementUpdateTwist, RealisticGateAcceptsInsideChiSquareBound)
{
params_.twist_gate_dist = 46.1;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto twist = make_twist(3.0, 0.0, "base_link", t_curr);

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_twist(twist, t_curr.seconds(), diag, warnings);

EXPECT_TRUE(ok);
EXPECT_TRUE(diag.is_passed_mahalanobis_gate);
EXPECT_GT(diag.mahalanobis_distance, 1.0);
EXPECT_LT(diag.mahalanobis_distance, 3.0);
}

TEST_F(MeasurementUpdateTwist, GateAcceptsOffsetCoveredByMeasurementCovariance)
{
using COV_IDX = autoware_utils_geometry::xyzrpy_covariance_index::XYZRPY_COV_IDX;
params_.twist_gate_dist = 46.1;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto twist = make_twist(2.0, 0.0, "base_link", t_curr);
twist.twist.covariance[COV_IDX::X_X] = 10.0;

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_twist(twist, t_curr.seconds(), diag, warnings);

EXPECT_TRUE(ok);
EXPECT_TRUE(diag.is_passed_mahalanobis_gate);
EXPECT_LT(diag.mahalanobis_distance, 1.0);
}

TEST_F(MeasurementUpdateTwist, GateRejectsSameOffsetWithTightMeasurementCovariance)
{
using COV_IDX = autoware_utils_geometry::xyzrpy_covariance_index::XYZRPY_COV_IDX;
params_.twist_gate_dist = 46.1;
reset_ekf_localizer();

const rclcpp::Time t_curr(100, 0, RCL_ROS_TIME);
auto twist = make_twist(2.0, 0.0, "base_link", t_curr);
twist.twist.covariance[COV_IDX::X_X] = 1e-4;

std::vector<CoreWarning> warnings;
EKFDiagnosticInfo diag;
const bool ok = ekf_localizer_->measurement_update_twist(twist, t_curr.seconds(), diag, warnings);

EXPECT_FALSE(ok);
EXPECT_FALSE(diag.is_passed_mahalanobis_gate);
EXPECT_GT(diag.mahalanobis_distance, 7.0);
}

} // namespace autoware::ekf_localizer
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ class EKFLocalizerIntegrationHarness : public ::testing::Test
return false;
}

std::string find_diagnostic_value(const std::string & key)
{
if (!latest_diag_) return "";
for (const auto & status : latest_diag_->status) {
for (const auto & kv : status.values) {
if (kv.key == key) return kv.value;
}
}
return "";
}

// Tick some, wait for diagnostics containing certain substring
bool poll_for_diagnostic(const std::string & substr, int max_ticks)
{
Expand Down Expand Up @@ -356,4 +367,22 @@ TEST_F(EKFLocalizerIntegrationHarness, PoseAndTwistMeasurementUpdates)
EXPECT_GT(latest_odom_->twist.twist.linear.x, 0.0);
}

TEST_F(EKFLocalizerIntegrationHarness, MahalanobisGateDiagnosticsReportSqrtThreshold)
{
trigger_node();
pub_initial_pose_->publish(make_pose(0.0, 0.0));
spin_once_tick_once();

std::string pose_threshold;
std::string twist_threshold;
for (int i = 0; i < 20 && (pose_threshold.empty() || twist_threshold.empty()); ++i) {
step_time(0.02);
pose_threshold = find_diagnostic_value("pose_mahalanobis_distance_threshold");
twist_threshold = find_diagnostic_value("twist_mahalanobis_distance_threshold");
}

EXPECT_EQ(pose_threshold, std::to_string(std::sqrt(49.5)));
EXPECT_EQ(twist_threshold, std::to_string(std::sqrt(46.1)));
}

} // namespace autoware::ekf_localizer
Loading