Skip to content

fix(autoware_ekf_localizer): gate on squared Mahalanobis distance of the innovation covariance - #1465

Open
JArmandoAnaya wants to merge 4 commits into
autowarefoundation:mainfrom
JArmandoAnaya:fix/ekf-mahalanobis-gate-1464
Open

JArmandoAnaya wants to merge 4 commits into
autowarefoundation:mainfrom
JArmandoAnaya:fix/ekf-mahalanobis-gate-1464

Conversation

@JArmandoAnaya

@JArmandoAnaya JArmandoAnaya commented Sep 22, 2026 •

Copy link
Copy Markdown

Description

Fixes the Mahalanobis gating in autoware_ekf_localizer for pose and twist measurements. This PR was originally two changes; the x/y process-noise change has been split out and will be proposed separately (see "Notes for reviewers").

1. Gate on the squared Mahalanobis distance (closes #1464)

pose_gate_dist and twist_gate_dist are chi-square quantiles on the squared Mahalanobis distance (e.g. 49.5 for 3 dof at significance 1e-10), but measurement_update_pose/measurement_update_twist compared the unsquared distance (mahalanobis(), the sqrt of the squared value) against them. The deployed gate therefore rejected only at about sqrt(49.5) ≈ 7.04x further out than the documented significance level implies.

The comparison now uses the squared distance against the unchanged config values. The unsquared distance is still used for the diagnostic field and the warning message, whose printed threshold is now sqrt(gate_dist) so both numbers share units. The same mismatch in the diagnostics array in ekf_localizer_node.cpp is fixed the same way.

2. Gate on the innovation covariance

The gate statistic used only the state covariance block P. The chi-square interpretation requires the innovation covariance S = C P C^T + R, with the same C and R that are passed to updateWithDelay. The gate now uses S, so the gate and the update agree. Without this, tightening the gate (fix 1) makes it reject measurements whose own noise explains the residual.

3. Documentation

The README "Tune gate parameters" section and the pose_gate_dist/twist_gate_dist schema descriptions now state that these limit the squared Mahalanobis distance, and that the diagnostics report the unsquared distance. No threshold values change.

Related links

Parent Issue:

How was this PR tested?

  • Built and ran the autoware_ekf_localizer gtests in an offline container (Jazzy, CUDA devel image) on this exact branch: all pass except xmllint, which needs network access to fetch its schema. The launch test was not run.
  • New tests: a measurement with large R that the state-only distance would reject is accepted, a tight-R measurement is rejected, and the diagnostics report the square root of the threshold. The existing RealisticGateRejects* tests fail without fix 1; removing R from the gate or reverting to the unsquared comparison makes the new tests fail.
  • Live CARLA + Autoware testing was done earlier for fix 1 only, on an older checkout without the innovation-covariance change: the gate rejects at about 7.04/6.79 instead of 49.5/46.1 and a spawn-to-goal drive completes without regression. The innovation-covariance change (fix 2) has not been live-tested.

Notes for reviewers

  • Fix 1 makes the effective gate roughly 7x tighter in distance terms, so genuine rejections become reachable in practice. Including R (fix 2) is what keeps the statistic bounded by the measurement noise.
  • The gate uses the latest P, whereas the update uses P at the delayed step; the two coincide only when there is no delay. Making them consistent needs a new getter in autoware_kalman_filter, so it is left out of this PR.
  • The x/y process-noise change (process_noise.proc_stddev_xy_c, which stops the stationary position covariance from collapsing) is intentionally not part of this PR. It adds a required parameter and needs a paired autoware_launch change, so it will be proposed on its own after this one.

Interface changes

No ROS parameter, topic or message changes.

Effects on system behavior

  • The pose and twist gates now reject at the documented chi-square thresholds on the squared innovation distance, using the full innovation covariance.
  • Diagnostic distances keep their units. The printed gate thresholds are now sqrt(gate_dist).

@github-actions

Copy link
Copy Markdown

Thank you for contributing to the Autoware project!

🚧 If your pull request is in progress, switch it to draft mode.

Please ensure:

@JArmandoAnaya
JArmandoAnaya marked this pull request as draft September 22, 2026 07:46
@JArmandoAnaya
JArmandoAnaya force-pushed the fix/ekf-mahalanobis-gate-1464 branch from 45e9b42 to cdc3b1b Compare September 22, 2026 07:49
@JArmandoAnaya
JArmandoAnaya marked this pull request as ready for review September 22, 2026 07:49
@JArmandoAnaya JArmandoAnaya changed the title fix(autoware_ekf_localizer): gate on squared Mahalanobis distance, not the unsquared value fix(autoware_ekf_localizer): gate on squared Mahalanobis distance and add x/y process noise Sep 22, 2026
@JArmandoAnaya
JArmandoAnaya force-pushed the fix/ekf-mahalanobis-gate-1464 branch 2 times, most recently from ba829cd to 4afce3e Compare September 22, 2026 21:38
@JArmandoAnaya
JArmandoAnaya marked this pull request as draft September 22, 2026 21:42
@JArmandoAnaya
JArmandoAnaya marked this pull request as ready for review September 22, 2026 22:06

Copy link
Copy Markdown

Reviewed 6c4ca45087328efb733bfdd8b2d37e2744017a54. The squared comparisons and the unsquared diagnostic distances/square-rooted diagnostic thresholds are internally consistent. I compiled the actual state-transition and Mahalanobis utility sources with Eigen 3.4.0 and ran their six existing GoogleTests: all passed. This did not exercise the ROS node, the full EKF test suite or vehicle logs.

One documentation correction: the README's “Tune gate parameters” section and the pose/twist schema descriptions still call these parameters limits on Mahalanobis distance. They now limit squared Mahalanobis distance. The README should also distinguish the unsquared diagnostic values from these configured thresholds. I have prepared a three-file wording-only patch; no threshold values or runtime behavior change.

I also checked the downstream launch config. The required proc_stddev_xy_c is still absent, consistent with your existing note. That file is managed by sync-params, so the release needs the coordinated generated-config update rather than a manual field addition.

AI assistance: OpenAI Codex investigated the code, ran the checks and drafted this review and wording patch.

@JArmandoAnaya
JArmandoAnaya force-pushed the fix/ekf-mahalanobis-gate-1464 branch 2 times, most recently from b9ccaac to 9b26bd1 Compare October 25, 2026 21:21
@JArmandoAnaya JArmandoAnaya changed the title fix(autoware_ekf_localizer): gate on squared Mahalanobis distance and add x/y process noise fix(autoware_ekf_localizer): gate on squared Mahalanobis distance of the innovation covariance Oct 25, 2026
@JArmandoAnaya

Copy link
Copy Markdown
Author

Thanks for the review. I've pushed an update (head 9b26bd10) that addresses the doc correction: the README "Tune gate parameters" section and the pose/twist schema descriptions now describe the gate parameters as limits on the squared Mahalanobis distance, and say the diagnostics report the unsquared distance.

The gate also now uses the full innovation covariance (C P Cᵀ + R) instead of the state covariance alone. The x/y process-noise change was split out of this PR, so proc_stddev_xy_c is no longer part of it and no launch config change is needed.

Could a maintainer add the run:build-and-test-differential label so the unit tests run? Thanks!

@DWatabe

DWatabe commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Without this, tightening the gate (fix 1) makes it reject measurements whose own noise explains the residual.

Some numbers that support doing fix 1 and fix 2 together. With the default parameters the two issues in the current gate happen to cancel, so the current gate width in metres is close to the intended one, and either fix alone moves it by a factor of about 7.

Size of a purely lateral position residual at which rejection starts, $\text{threshold} / \sqrt{u^\top M^{-1} u}$ ($u$: lateral unit vector in (x, y, yaw), $M$: the 3x3 matrix used by the gate). P was logged at the gate on the public sample rosbag (straight section, 8.9–13.0 m/s, default parameters); R is the one passed to the update (0.0225 m² × pose_smoothing_steps 5).

gate statistic threshold on the distance M lateral residual at which rejection starts
current (unsquared distance, P only) 49.5 P 2.4–3.0 m
fix 1 only √49.5 = 7.04 P 0.34–0.43 m
P + R, unsquared distance 49.5 P + R about 16.9 m
fix 1 + fix 2 (this PR) 7.04 P + R about 2.40 m

Direct check of the first row: for a 2.03 m lateral residual the current gate reported a distance of 41.2 against the limit 49.5, and none of the 333 gate evaluations rejected it.

Because the current gate uses P only, its width also moves with the tuning:

  • With proc_stddev_vx_c / proc_stddev_wz_c reduced from 10.0 / 5.0 to 0.316 / 0.158, the width became 1.8–1.9 m and a 2.0 m offset was rejected in 22 of 333 gate evaluations (during the first 0.54 s).
  • With the NDT pose covariance multiplied by 100 and 1000, rejection would start at about 10 m and 21 m.

With P + R the width was 2.38–2.41 m for all three process-noise settings.

Setup: main as of 2026-10-02 with logging added at the gate (gate logic unchanged), NDT output of the rosbag replay tutorial as input. The replay scripts are attached to #1548.

AI usage: I wrote this with Claude Code (Claude Fable 5.2), from my direction. The agent ran the replays, added the logging and computed the table. I read the comment and did not re-run ROS myself; a separate AI review (GPT-6.1-sol) re-computed the rejection counts and the first gate distance of each condition from the saved logs, and they matched.

…t the unsquared value

pose_gate_dist and twist_gate_dist are chi-square quantiles (e.g. 49.5
for 3 dof at significance 1e-10), which are thresholds on the squared
Mahalanobis distance. measurement_update_pose/measurement_update_twist
were comparing the unsquared distance (sqrt of the squared value)
directly against these thresholds, making the gate reject only at
~7x the documented distance (squared-distance acceptance region ~49.5x
larger than intended).

Fixes the comparison to use squared_mahalanobis() against the existing
config defaults, unchanged. distance (unsquared) is kept for the
diagnostic field and warning message, with the printed threshold
adjusted to sqrt() of the gate_dist param so both operands share units.
Same fix applied to the diagnostics array construction in
ekf_localizer_node.cpp, which reported the same unsquared/squared
mismatch.

Fixes autowarefoundation#1464

Signed-off-by: Jesus Armando Anaya <armando@robomous.ai>
…halanobis gate

The pose and twist gates normalized the innovation by the predicted state
covariance block only (H P H^T), while the Kalman update that follows uses
the innovation covariance S = H P H^T + R. The gate therefore ignored the
measurement's own uncertainty: a noisy measurement with a large reported
covariance could be rejected even though the filter would weight it lightly,
and the chi-square thresholds (which assume the innovation is distributed
with covariance S) were applied to the wrong statistic.

Build R once with pose_measurement_covariance/twist_measurement_covariance
(message covariance scaled by the smoothing steps), form S = C P C^T + R
from the same C and R passed to updateWithDelay, and gate on the squared
Mahalanobis distance under S. The gate and the update now agree on the
innovation covariance.

Signed-off-by: Jesus Armando Anaya <armando@robomous.ai>
…obis limits

pose_gate_dist and twist_gate_dist are chi-square quantiles and limit the
squared Mahalanobis distance of the innovation, normalized by the innovation
covariance (predicted state covariance plus measurement covariance). Update
the "Tune gate parameters" section and the schema descriptions accordingly,
and note that diagnostics report the unsquared distance against the square
root of the parameter. Wording only; threshold values are unchanged.

Signed-off-by: Jesus Armando Anaya <armando@robomous.ai>
… covariance and diagnostics threshold

The existing RejectsOnMahalanobisGate tests use gate distances (1e-6,
10000.0) extreme enough that neither squaring the distance nor including
the measurement covariance changes their verdict.

Add pose and twist tests at the shipped defaults (49.5, 46.1):
- RealisticGateRejects/Accepts: the unsquared distance under the innovation
  covariance lies between sqrt(gate_dist) and gate_dist, so only the
  squared comparison rejects it; the warning reports sqrt(gate_dist).
- GateAcceptsOffsetCoveredByMeasurementCovariance: an offset that a
  state-covariance-only gate rejects is accepted once the large measurement
  covariance R is included in S = H P H^T + R.
- GateRejectsSameOffsetWithTightMeasurementCovariance: the same offset with
  a tight R is rejected.
- Node integration: the published pose/twist mahalanobis_distance_threshold
  diagnostics equal sqrt(pose_gate_dist)/sqrt(twist_gate_dist).

Checked locally that the Realistic*/tight-R/diagnostics tests fail against
an unsquared comparison and that the Realistic*/large-R tests fail against
a gate built from the state covariance only.

Signed-off-by: Jesus Armando Anaya <armando@robomous.ai>
@JArmandoAnaya
JArmandoAnaya force-pushed the fix/ekf-mahalanobis-gate-1464 branch from 6cce6a0 to 466e380 Compare November 25, 2026 01:12
@JArmandoAnaya

Copy link
Copy Markdown
Author

Thanks @DWatabe for the analysis. Your table is independent evidence that the gate fix and the process-noise fix belong together: with default parameters the two bugs cancel, and fixing only the gate shrinks it to roughly 0.34–0.43 m. This PR is intentionally the gate fix only (squared distance on the innovation covariance S = C P C^T + R). The x/y process-noise change is kept separate and will be positioned against #1548 before it is proposed.

I rebased the branch onto current main (4 signed-off commits, code unchanged).

Could a maintainer add the run:build-and-test-differential label so build and tests run in CI? @YamatoAndo, a review would also be appreciated.

@YamatoAndo YamatoAndo added the run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci) label Nov 30, 2026
@github-actions

Copy link
Copy Markdown

This pull request comes from a fork, so its documentation site is built but not published.

  • Download the site of commit 466e380: docs-pr-1465. You must be signed in to GitHub. The site is kept for 90 days.
  • Follow the steps in the run summary to view it on your computer.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.32%. Comparing base (b46bc5f) to head (466e380).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1465      +/-   ##
==========================================
- Coverage   82.36%   82.32%   -0.05%     
==========================================
  Files         421      413       -8     
  Lines       23857    23230     -627     
  Branches      147        0     -147     
==========================================
- Hits        19650    19124     -526     
+ Misses       4177     4106      -71     
+ Partials       30        0      -30     
Flag Coverage Δ *Carryforward flag
daily ?
full-suite 82.29% <ø> (-0.02%) ⬇️ Carriedforward from b46bc5f

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

autoware_ekf_localizer: Mahalanobis gate compares unsquared distance against squared-distance (chi-square) thresholds

4 participants