fix(Point-LIO): 信息形式分支把 m_noise 当精度用,与协方差分支不一致
update_iterated_dyn_share_modified() 的两个分支对同一量语义不同: m_noise 是方差(M_Noise = laser_point_cov),信息形式里精度应是 1/m_noise。 原来直接乘 m_noise,导致「追加零信息行反而缩放后验」,与协方差分支结果分歧。 改为 inv_noise = 1/m_noise 后,两式与 K = (P^-1 + (1/r) H^T H)^-1 H^T (1/r) 一致。同时暴露三个只读诊断字段(state dof / effective m / 走了哪个分支)。 回归:docs/diagnostics/esekfom_update_regression/ 直接 #include 生产头, 用生产 init_dyn_share_modified_2h 入口驱动,参考解为稠密 Kalman(LLT 求解, 不用伪逆),全部通过;Point-LIO 重编通过(6 min 44 s)。
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
# esekfom 更新分支回归(离线)
|
||||
|
||||
对应 `CODEX_REVIEW_POINT_LIO.md` 的优先项:`esekfom.hpp` 中
|
||||
`update_iterated_dyn_share_modified()` 两个分支的噪声语义是否一致。
|
||||
|
||||
**当前状态:修复已应用到生产头,回归直接测试该头,全部通过。**
|
||||
`Point-LIO` 已重新构建成功(6 min 44 s)。
|
||||
|
||||
## 怎么测才算"测真实实现"
|
||||
|
||||
- 直接 `#include <common_lib.h>`,使用生产代码的 `state_output`(DOF=30)、
|
||||
`input_ikfom` 与 `esekfom::esekf<state_output, 30, input_ikfom>`,不是复刻公式。
|
||||
- 编译参数取自工作区 `build/compile_commands.json` 中 `laserMapping.cpp` 的真实条目
|
||||
(`extract_flags.py`)。这一步是必需的:`MTK_BUILD_MANIFOLD` 依赖 PCL/ROS 头
|
||||
引入的 boost 预处理配置,手工拼 `-I` 会直接编译失败。
|
||||
- 通过生产入口 `init_dyn_share_modified_2h` → `update_iterated_dyn_share_modified()`
|
||||
驱动,`maximum_iter` 保持模板初始化的 1,与运行态一致。
|
||||
- 参考解由测试内的稠密 Eigen 标准 Kalman 现场计算;**新息矩阵用 Cholesky(LLT)求解**,
|
||||
不再用伪逆——见下方"关于伪逆的更正"。
|
||||
|
||||
## 运行
|
||||
|
||||
```bash
|
||||
cd minco_ws/docs/diagnostics/esekfom_update_regression
|
||||
./build_and_run.sh # 需要先构建过 minco_ws(提供 compile_commands.json)
|
||||
```
|
||||
|
||||
脚本编译**生产头**并运行,同时把当前应用在该头上的改动导出到
|
||||
`build/applied_change.diff`。退出码即测试状态。
|
||||
|
||||
⚠️ 本机 `/tmp` 只有 10 MB(tmpfs),编译大 TU 会因写汇编临时文件失败。
|
||||
构建 Point-LIO 需指定:`TMPDIR=/home/xie/.cache/tmp`。
|
||||
|
||||
## 关于伪逆的更正
|
||||
|
||||
上一版参考解用 `completeOrthogonalDecomposition().pseudoInverse()`,并注释说
|
||||
"追加零信息行需要伪逆"。**这个说法是错的**:`S = H P Hᵀ + r I`,只要 `r > 0`,
|
||||
即使某行 `h` 全零(该行只给 `H P Hᵀ` 贡献零),`S` 仍因 `r I` 而是正定的。
|
||||
因此普通正定分解即可。现在用 `LLT`,顺带把"正定性"本身作为断言。
|
||||
|
||||
## 结果
|
||||
|
||||
### 修复前(记录,供对照)
|
||||
|
||||
见 `build/` 外的历史输出;关键值如下:
|
||||
|
||||
| 观测数 | dx 误差 | P 误差 |
|
||||
|---|---|---|
|
||||
| m=30 | 1.24 | 21.5 |
|
||||
| m=45 | 1.42 | 16.9 |
|
||||
|
||||
反例:P=I、r=0.01、仅首行观测位置 x,29 行给 dx[0]=0.099010;
|
||||
追加一行零信息观测后变成 0.000990,正确值仍应是 0.099010。
|
||||
|
||||
### 修复后(当前生产头)
|
||||
|
||||
```
|
||||
=== A. counterexample from the review (P = I, r = 0.01) ===
|
||||
29 informative rows : dx0=0.0990099009900990 (reference 同值)
|
||||
30 rows (last zero) : dx0=0.0990099009900990 (reference 同值)
|
||||
finite=1 invariant_within_tol=1
|
||||
appended-row invariance: HOLDS
|
||||
|
||||
=== B. random priors, measurement count < / = / > state dim ===
|
||||
covariance m= 1 dx_err=8.812e-16 P_err=7.105e-15 cond=1.0 finite=1 PASS
|
||||
covariance m= 5 dx_err=3.997e-15 P_err=1.421e-14 cond=6.4 finite=1 PASS
|
||||
covariance m= 12 dx_err=4.219e-15 P_err=2.842e-14 cond=19.5 finite=1 PASS
|
||||
covariance m= 29 dx_err=2.711e-12 P_err=6.544e-11 cond=21905.3 finite=1 PASS
|
||||
information m= 30 dx_err=1.695e-12 P_err=4.912e-13 cond=23433.7 finite=1 PASS
|
||||
information m= 45 dx_err=2.145e-12 P_err=3.393e-13 cond=33029.6 finite=1 PASS
|
||||
|
||||
=== C. rank-deficient prior ===
|
||||
m=12 dx_err=2.602e-17 P_err=1.332e-15 ran=1 finite=1 PASS
|
||||
|
||||
ALL PASS (0 failures)
|
||||
```
|
||||
|
||||
三组都是**真实断言**,不是打印:A 判不变性,B 判误差+有限值,C 判秩亏先验下
|
||||
仍与参考一致(`S` 因 `r>0` 依旧正定,`K = P Hᵀ S⁻¹` 不需要 `P⁻¹`)。
|
||||
`finite` 列覆盖状态、协方差与派生误差;NaN 不会被 `maxCoeff()` 比较吞掉。
|
||||
|
||||
观测行取自 12 维子空间的随机正交基(m>12 时循环),避免随机病态矩阵把
|
||||
"条件数问题"混进"分支代数问题";`cond` 列为此提供旁证。
|
||||
|
||||
## 已应用的改动
|
||||
|
||||
```diff
|
||||
--- a/include/IKFoM/IKFoM_toolkit/esekfom/esekfom.hpp
|
||||
+++ b/include/IKFoM/IKFoM_toolkit/esekfom/esekfom.hpp
|
||||
@@ -232,11 +232,18 @@
|
||||
} else {
|
||||
- Matrix<scalar_type, 12, 12> HTH = m_noise * h_x.transpose() * h_x;
|
||||
+ // Information form. m_noise is a VARIANCE (M_Noise = laser_point_cov),
|
||||
+ // so it enters as a precision 1/m_noise in both places:
|
||||
+ // K = (P^-1 + (1/r) H^T H)^-1 H^T (1/r)
|
||||
+ // Passing r instead of 1/r here made this branch disagree with the
|
||||
+ // covariance branch above: appending a zero-information row rescaled the
|
||||
+ // posterior. Regression: docs/diagnostics/esekfom_update_regression.
|
||||
+ const scalar_type inv_noise = scalar_type(1) / m_noise;
|
||||
+ Matrix<scalar_type, 12, 12> HTH = inv_noise * h_x.transpose() * h_x;
|
||||
Matrix<scalar_type, n, n> P_inv = P_.inverse();
|
||||
P_inv.template block<12, 12>(0, 0) += HTH;
|
||||
P_inv = P_inv.inverse();
|
||||
- K_ = P_inv.template block<n, 12>(0, 0) * h_x.transpose() * m_noise;
|
||||
+ K_ = P_inv.template block<n, 12>(0, 0) * h_x.transpose() * inv_noise;
|
||||
}
|
||||
```
|
||||
|
||||
语义:`M_Noise = laser_point_cov` 是**方差**,信息形式两处都应为精度 `1/r`。
|
||||
|
||||
## 这个回归能证明什么、不能证明什么
|
||||
|
||||
能:
|
||||
- 两分支在此前不等价,差异是代数错误而非数值噪声(修复后 O(1) → 1e-12 量级)。
|
||||
- 追加零信息观测曾改变后验;修复后恢复不变性。
|
||||
- 生产模板在 m ≤ 45 范围内与标准 Kalman 一致到 1e-11;秩亏先验下亦一致。
|
||||
|
||||
不能:
|
||||
- **不能证明**该分支在实车被触发过。`dof_Measurement = h_x.rows()` 是该批有效点数,
|
||||
本包未记录,无法判断是否出现过 `m ≥ 30`。
|
||||
- **不能证明**它与观测到的位姿抖动有关。本回归只检验更新步的状态修正与后验协方差,
|
||||
不含传播、不含真实观测序列。
|
||||
- 不涉及噪声取值、重力、外参、自身过滤是否合理。
|
||||
|
||||
---
|
||||
|
||||
# 最小分支观测(runtime_update_trace)
|
||||
|
||||
默认关闭,参数 `runtime_update_trace_enable`(`false`)。开启后写入
|
||||
`runtime_log_path` 目录下的 `runtime_update_trace.csv`。
|
||||
|
||||
## 设计
|
||||
|
||||
- **关闭时**:`record()` 只做一次 `relaxed` 原子读后返回。不格式化、不开文件、
|
||||
不建目录、不分配内存。
|
||||
- **有界队列 + 异步写**:默认容量 4096,写线程独立。队列满时丢弃并计数,
|
||||
滤波器不阻塞。
|
||||
- **写盘失败**:只关闭 trace,滤波器继续;错误通过 sink 上报一次
|
||||
(`laserMapping.cpp` 里接的是 `RCLCPP_ERROR`)。
|
||||
- **数值不变**:包装器在关闭时直接调用原函数;`esekfom` 里新增的两个
|
||||
`last_update_*` 成员只在更新循环结束后写入,不参与任何计算。
|
||||
|
||||
## CSV 字段(27 列,version 1)
|
||||
|
||||
```
|
||||
trace_version, seq, scan_id, batch_id, event, t_before, t_after, dt, ok,
|
||||
pos_before_x/y/z, pos_after_x/y/z, vel_before_x/y/z, vel_after_x/y/z,
|
||||
frame, state_dof, effective_m, branch, predict_state, prop_cov
|
||||
```
|
||||
|
||||
- `frame` 恒为 `world`(`camera_init`),位置与速度都是世界系。
|
||||
- `event` ∈ `predict / imu_update / lidar_update / publish`;`publish` 只在
|
||||
限流器放行、消息真的发出后记录。
|
||||
- `branch`:0 = 协方差形式,1 = 信息形式;`effective_m` 为该次更新的实际点数。
|
||||
- `predict_state / prop_cov`:仅 `predict` 事件有意义,其余写 `-1`。
|
||||
- `scan_id` 每次扫描递增;`batch_id` 为该扫描内的 `time_seq` 批次序号。
|
||||
|
||||
## 测试
|
||||
|
||||
`test_runtime_update_trace.cpp`,与回归同一套编译参数:
|
||||
|
||||
| 用例 | 断言 |
|
||||
|---|---|
|
||||
| 关闭 | 不建目录、不建文件、`written==0` |
|
||||
| 开启 | 27 列、三种事件的每个字段值、`frame=world`、`branch` 与 `effective_m` |
|
||||
| 有界队列 | 容量 2 灌 2 万条:`dropped>0` 且 `written + dropped == attempted` |
|
||||
| 目录不可写 | `initialize` 返回 false、`enabled()==false`、错误 sink 触发一次,随后参考解仍正确 |
|
||||
| 包装不改数值 | 同一更新带/不带 trace,位置、速度、协方差**逐位相同**(gap 0.000e+00) |
|
||||
|
||||
## 已知边界
|
||||
|
||||
- 运行时**写盘中途失败**(如磁盘写满)这条路径没有在测试里模拟,只测了
|
||||
"打不开目录"。写失败的处理逻辑在写线程里(`stream_.good()` 检查后关闭 trace
|
||||
并计数),未被执行覆盖。
|
||||
- `effective_m` 只在 `lidar_update` 事件里有真实值;其余事件为 `-1`。
|
||||
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regression for esekfom::esekf::update_iterated_dyn_share_modified().
|
||||
#
|
||||
# ./build_and_run.sh
|
||||
#
|
||||
# Compiles against Point-LIO's REAL include tree using the exact flags recorded
|
||||
# for laserMapping.cpp in the workspace's compile_commands.json, so the
|
||||
# boost/preprocessor configuration that MTK_BUILD_MANIFOLD depends on is
|
||||
# identical to production. Nothing is copied or patched here: the header under
|
||||
# test is the one the navigation stack compiles.
|
||||
#
|
||||
# Exit status is the test's own status (0 == all cases pass).
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "$0")" && pwd)"
|
||||
WS="/home/xie/RC2027_BR_code/minco_ws"
|
||||
POINT_LIO="$WS/src/perception/Point-LIO"
|
||||
HEADER_REL="include/IKFoM/IKFoM_toolkit/esekfom/esekfom.hpp"
|
||||
COMPILE_COMMANDS="$WS/build/compile_commands.json"
|
||||
|
||||
if [[ ! -f "$COMPILE_COMMANDS" ]]; then
|
||||
echo "error: $COMPILE_COMMANDS not found; build minco_ws first" >&2
|
||||
exit 3
|
||||
fi
|
||||
if [[ ! -d "$POINT_LIO/include" ]]; then
|
||||
echo "error: $POINT_LIO/include not found" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
mkdir -p "$HERE/build"
|
||||
|
||||
# 1. Flags of the production translation unit.
|
||||
/usr/bin/python3 "$HERE/extract_flags.py" "$COMPILE_COMMANDS" \
|
||||
> "$HERE/build/flags_current.txt"
|
||||
|
||||
# 2. Record the change currently applied to the header under test.
|
||||
git -C "$POINT_LIO" diff -- "$HEADER_REL" > "$HERE/build/applied_change.diff" || true
|
||||
|
||||
# 3. Build and run.
|
||||
g++ @"$HERE/build/flags_current.txt" -O1 "$HERE/test_esekfom_update.cpp" \
|
||||
-o "$HERE/build/regression"
|
||||
g++ @"$HERE/build/flags_current.txt" -O1 "$HERE/test_runtime_update_trace.cpp" \
|
||||
-o "$HERE/build/trace_tests" -pthread
|
||||
|
||||
set +e
|
||||
echo "########## esekfom update branches ##########"
|
||||
"$HERE/build/regression"
|
||||
status=$?
|
||||
echo
|
||||
echo "########## runtime update trace ##########"
|
||||
"$HERE/build/trace_tests"
|
||||
trace_status=$?
|
||||
set -e
|
||||
|
||||
echo
|
||||
echo "header under test : $POINT_LIO/$HEADER_REL"
|
||||
echo "applied change : build/applied_change.diff"
|
||||
exit $((status + trace_status))
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/python3
|
||||
"""Emit the compile flags used for Point-LIO's own TU, for the regression build.
|
||||
|
||||
The regression must compile the same headers with the same flags as the real
|
||||
translation unit, including the boost/preprocessor configuration that the
|
||||
MTK_BUILD_MANIFOLD macros depend on. Re-deriving a flag list by hand is how a
|
||||
harness silently drifts away from production.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) != 2:
|
||||
print(f"usage: {sys.argv[0]} <compile_commands.json>", file=sys.stderr)
|
||||
return 2
|
||||
db = json.loads(Path(sys.argv[1]).read_text())
|
||||
entry = next((e for e in db if e["file"].endswith("laserMapping.cpp")), None)
|
||||
if entry is None:
|
||||
print("laserMapping.cpp not found in compile_commands.json", file=sys.stderr)
|
||||
return 3
|
||||
|
||||
tokens = entry["command"].split()
|
||||
# The first token is the compiler itself; passing it through an @file makes
|
||||
# g++ treat /usr/bin/c++ as an input file.
|
||||
if tokens and tokens[0].endswith(("c++", "g++", "cc", "gcc", "clang++")):
|
||||
tokens = tokens[1:]
|
||||
keep = []
|
||||
i = 0
|
||||
while i < len(tokens):
|
||||
token = tokens[i]
|
||||
if token in ("-o", "-c"):
|
||||
i += 2
|
||||
continue
|
||||
if token.endswith((".cpp", ".cc", ".cxx", ".o")):
|
||||
i += 1
|
||||
continue
|
||||
keep.append(token)
|
||||
i += 1
|
||||
|
||||
print(" ".join(keep))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,316 @@
|
||||
// Offline regression for esekfom::esekf::update_iterated_dyn_share_modified().
|
||||
//
|
||||
// This compiles the PRODUCTION filter template and the PRODUCTION state type:
|
||||
// it includes <common_lib.h> from Point-LIO, so `state_output`, `input_ikfom`
|
||||
// and the 30-dimension / 12-column h_x layout are the real ones, not a replica.
|
||||
// The build reuses the exact flags recorded in the workspace's
|
||||
// compile_commands.json for laserMapping.cpp (see extract_flags.py), because the
|
||||
// MTK_BUILD_MANIFOLD macros are sensitive to the boost/preprocessor
|
||||
// configuration that the PCL and ROS headers establish.
|
||||
//
|
||||
// The harness then drives the same entry point the navigation stack calls and
|
||||
// compares the result against a textbook Kalman update computed here with dense
|
||||
// Eigen. No runtime parameter, header or behaviour is modified.
|
||||
|
||||
#include <common_lib.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
|
||||
namespace {
|
||||
|
||||
typedef esekfom::esekf<state_output, 30, input_ikfom> production_ekf;
|
||||
|
||||
static_assert(state_output::DOF == 30, "state_output DOF changed");
|
||||
static_assert(state_output::DIM == 30, "state_output DIM changed");
|
||||
|
||||
// ---- data handed to the measurement callback ------------------------------
|
||||
Eigen::MatrixXd g_hx; // measurement rows x 12
|
||||
Eigen::VectorXd g_z; // measurement rows
|
||||
double g_noise = 0.01; // dyn_share.M_Noise == laser_point_cov (a variance)
|
||||
|
||||
void measurement_cb(state_output &, Eigen::Matrix3d, Eigen::Matrix3d,
|
||||
esekfom::dyn_share_modified<double> & share)
|
||||
{
|
||||
share.valid = true;
|
||||
share.z = g_z;
|
||||
share.h_x = g_hx;
|
||||
share.M_Noise = g_noise;
|
||||
}
|
||||
|
||||
// Process model is never invoked by an update-only test, but the template needs
|
||||
// both pointers set before init_dyn_share_modified_2h() returns.
|
||||
Eigen::Matrix<double, 30, 1> process_dummy(state_output &, const input_ikfom &)
|
||||
{
|
||||
return Eigen::Matrix<double, 30, 1>::Zero();
|
||||
}
|
||||
|
||||
Eigen::Matrix<double, 30, 30> process_jacobian_dummy(state_output &, const input_ikfom &)
|
||||
{
|
||||
return Eigen::Matrix<double, 30, 30>::Identity();
|
||||
}
|
||||
|
||||
// ---- reference: textbook Kalman ------------------------------------------
|
||||
struct Reference
|
||||
{
|
||||
Eigen::VectorXd dx;
|
||||
Eigen::MatrixXd P_post;
|
||||
};
|
||||
|
||||
Reference kalman_reference(const Eigen::MatrixXd & P, const Eigen::MatrixXd & H,
|
||||
const Eigen::VectorXd & z, double r, bool & ok)
|
||||
{
|
||||
const int m = static_cast<int>(H.rows());
|
||||
// With r > 0 the innovation covariance S = H P H^T + r I is positive definite
|
||||
// even when a measurement row carries no information at all: that row only
|
||||
// contributes zeros to H P H^T and is regularised by the r I term. So no
|
||||
// pseudo-inverse is needed, and an earlier comment claiming otherwise was
|
||||
// wrong. A Cholesky solve is used, which also proves positive definiteness.
|
||||
Eigen::MatrixXd S = H * P * H.transpose() + r * Eigen::MatrixXd::Identity(m, m);
|
||||
Eigen::LLT<Eigen::MatrixXd> llt(S);
|
||||
if (llt.info() != Eigen::Success) {
|
||||
ok = false;
|
||||
return Reference{};
|
||||
}
|
||||
// K = P H^T S^-1 without forming S^-1: solve S X = (H P) and transpose.
|
||||
// S is symmetric, so (S^-1 (H P))^T == P H^T S^-1.
|
||||
Eigen::MatrixXd K = llt.solve(H * P).transpose();
|
||||
|
||||
Reference out;
|
||||
out.dx = K * z;
|
||||
out.P_post = P - K * H * P;
|
||||
out.P_post = 0.5 * (out.P_post + out.P_post.transpose());
|
||||
ok = out.dx.allFinite() && out.P_post.allFinite();
|
||||
return out;
|
||||
}
|
||||
|
||||
Eigen::MatrixXd embed(const Eigen::MatrixXd & h_x, int state_dim)
|
||||
{
|
||||
Eigen::MatrixXd H = Eigen::MatrixXd::Zero(h_x.rows(), state_dim);
|
||||
H.leftCols(h_x.cols()) = h_x;
|
||||
return H;
|
||||
}
|
||||
|
||||
state_output make_state()
|
||||
{
|
||||
// MTK::vect defaults to zero and MTK::SO3 to identity; individual elements are
|
||||
// assigned the way the production code assigns them.
|
||||
state_output s;
|
||||
s.pos = vect3();
|
||||
s.rot = SO3();
|
||||
s.offset_R_L_I = SO3();
|
||||
s.offset_T_L_I = vect3();
|
||||
s.offset_T_L_I(0) = -0.011;
|
||||
s.offset_T_L_I(1) = -0.02329;
|
||||
s.offset_T_L_I(2) = 0.04412;
|
||||
s.vel = vect3();
|
||||
s.omg = vect3();
|
||||
s.acc = vect3();
|
||||
s.acc(2) = 9.81;
|
||||
s.gravity = vect3();
|
||||
s.gravity(2) = -9.81;
|
||||
s.bg = vect3();
|
||||
s.ba = vect3();
|
||||
return s;
|
||||
}
|
||||
|
||||
struct Outcome
|
||||
{
|
||||
double dx_err = std::numeric_limits<double>::quiet_NaN();
|
||||
double P_err = std::numeric_limits<double>::quiet_NaN();
|
||||
double dx0_actual = 0.0;
|
||||
double dx0_reference = 0.0;
|
||||
bool ran = false;
|
||||
bool finite = false;
|
||||
};
|
||||
|
||||
// Drive the production template once and compare with the reference.
|
||||
Outcome run_case(const Eigen::MatrixXd & P0, const Eigen::MatrixXd & h_x,
|
||||
const Eigen::VectorXd & z, double r)
|
||||
{
|
||||
Outcome out;
|
||||
g_hx = h_x;
|
||||
g_z = z;
|
||||
g_noise = r;
|
||||
|
||||
state_output x0 = make_state();
|
||||
// change_P() takes a non-const reference to the fixed-size covariance type,
|
||||
// so an lvalue of exactly that type is required.
|
||||
production_ekf::cov P_cov = P0;
|
||||
production_ekf ekf(x0, P_cov);
|
||||
ekf.init_dyn_share_modified_2h(process_dummy, process_jacobian_dummy,
|
||||
measurement_cb);
|
||||
ekf.change_x(x0);
|
||||
ekf.change_P(P_cov);
|
||||
|
||||
out.ran = ekf.update_iterated_dyn_share_modified();
|
||||
if (!out.ran) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Manifold difference between prior and posterior: the correction the
|
||||
// template actually applied.
|
||||
Eigen::Matrix<double, 30, 1> dx_actual;
|
||||
dx_actual.setZero();
|
||||
ekf.get_x().boxminus(dx_actual, x0);
|
||||
|
||||
const Eigen::MatrixXd H = embed(h_x, 30);
|
||||
bool reference_ok = true;
|
||||
Reference ref = kalman_reference(P0, H, z, r, reference_ok);
|
||||
|
||||
const Eigen::MatrixXd P_after = ekf.get_P();
|
||||
// Both the produced state/covariance and the derived errors must be finite;
|
||||
// a NaN would otherwise sail through a maxCoeff() comparison.
|
||||
out.finite = reference_ok && dx_actual.allFinite() && P_after.allFinite();
|
||||
if (!out.finite) {
|
||||
return out;
|
||||
}
|
||||
out.dx_err = (dx_actual - ref.dx).cwiseAbs().maxCoeff();
|
||||
out.P_err = (P_after - ref.P_post).cwiseAbs().maxCoeff();
|
||||
out.finite = out.finite && std::isfinite(out.dx_err) && std::isfinite(out.P_err);
|
||||
out.dx0_actual = dx_actual(0);
|
||||
out.dx0_reference = ref.dx(0);
|
||||
return out;
|
||||
}
|
||||
|
||||
Eigen::MatrixXd random_positive_definite(std::mt19937 & rng, int dim)
|
||||
{
|
||||
std::normal_distribution<double> normal(0.0, 1.0);
|
||||
Eigen::MatrixXd A(dim, dim);
|
||||
for (int i = 0; i < dim; ++i) {
|
||||
for (int j = 0; j < dim; ++j) {
|
||||
A(i, j) = normal(rng);
|
||||
}
|
||||
}
|
||||
Eigen::MatrixXd P = A * A.transpose();
|
||||
P += 0.1 * Eigen::MatrixXd::Identity(dim, dim);
|
||||
return 0.5 * (P + P.transpose());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
const double r = 0.01; // lidar_meas_cov / laser_point_cov, a variance
|
||||
const double tol = 1e-6;
|
||||
int failures = 0;
|
||||
|
||||
std::printf("=== A. counterexample from the review (P = I, r = 0.01) ===\n");
|
||||
{
|
||||
Eigen::MatrixXd h_x = Eigen::MatrixXd::Zero(29, 12);
|
||||
h_x(0, 0) = 1.0; // observes position x only
|
||||
Eigen::VectorXd z = Eigen::VectorXd::Zero(29);
|
||||
z(0) = 0.1;
|
||||
const Eigen::MatrixXd P = Eigen::MatrixXd::Identity(30, 30);
|
||||
|
||||
Outcome a = run_case(P, h_x, z, r);
|
||||
std::printf(" 29 informative rows : dx0=%.16f (reference %.16f)\n",
|
||||
a.dx0_actual, a.dx0_reference);
|
||||
|
||||
// Same problem plus one row that carries no information at all. A correct
|
||||
// update cannot change the posterior.
|
||||
Eigen::MatrixXd h_x30 = Eigen::MatrixXd::Zero(30, 12);
|
||||
h_x30.topRows(29) = h_x;
|
||||
Eigen::VectorXd z30 = Eigen::VectorXd::Zero(30);
|
||||
z30.head(29) = z;
|
||||
|
||||
Outcome b = run_case(P, h_x30, z30, r);
|
||||
std::printf(" 30 rows (last zero) : dx0=%.16f (reference %.16f)\n",
|
||||
b.dx0_actual, b.dx0_reference);
|
||||
|
||||
const bool finite_ok = a.ran && a.finite && b.ran && b.finite;
|
||||
const bool invariant = finite_ok &&
|
||||
std::fabs(b.dx0_actual - a.dx0_actual) < tol &&
|
||||
std::fabs(a.dx0_actual - a.dx0_reference) < tol &&
|
||||
std::fabs(b.dx0_reference - a.dx0_reference) < tol;
|
||||
std::printf(" finite=%d invariant_within_tol=%d\n",
|
||||
static_cast<int>(finite_ok), static_cast<int>(invariant));
|
||||
std::printf(" appended-row invariance: %s\n", invariant ? "HOLDS" : "BROKEN");
|
||||
if (!invariant) {
|
||||
++failures;
|
||||
}
|
||||
}
|
||||
|
||||
std::printf("\n=== B. random priors, measurement count < / = / > state dim ===\n");
|
||||
{
|
||||
std::mt19937 rng(20261103);
|
||||
std::normal_distribution<double> normal(0.0, 1.0);
|
||||
for (int m : {1, 5, 12, 29, 30, 45}) {
|
||||
double worst_dx = 0.0;
|
||||
double worst_P = 0.0;
|
||||
double worst_cond = 0.0;
|
||||
bool all_ran = true;
|
||||
bool all_finite = true;
|
||||
for (int trial = 0; trial < 20; ++trial) {
|
||||
Eigen::MatrixXd P0 = random_positive_definite(rng, 30);
|
||||
// Rows are taken from a random orthonormal basis of the 12-column
|
||||
// measurement subspace. Fully random rows occasionally make
|
||||
// H P H^T nearly singular, which would exercise matrix conditioning
|
||||
// rather than the branch algebra under test.
|
||||
Eigen::MatrixXd raw(12, 12);
|
||||
for (int i = 0; i < 12; ++i) {
|
||||
for (int j = 0; j < 12; ++j) {
|
||||
raw(i, j) = normal(rng);
|
||||
}
|
||||
}
|
||||
Eigen::HouseholderQR<Eigen::MatrixXd> qr(raw);
|
||||
const Eigen::MatrixXd basis = qr.householderQ() * Eigen::MatrixXd::Identity(12, 12);
|
||||
// For m > 12 the basis is cycled: the measurement subspace keeps rank
|
||||
// 12 while the row count grows, which is what the branch condition
|
||||
// looks at. topRows(m) would read past the end of a 12-row matrix.
|
||||
Eigen::MatrixXd h_x(m, 12);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
h_x.row(i) = basis.row(i % 12);
|
||||
}
|
||||
Eigen::VectorXd z(m);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
z(i) = normal(rng);
|
||||
}
|
||||
Outcome o = run_case(P0, h_x, z, r);
|
||||
all_ran = all_ran && o.ran;
|
||||
all_finite = all_finite && o.finite;
|
||||
worst_dx = std::max(worst_dx, o.dx_err);
|
||||
worst_P = std::max(worst_P, o.P_err);
|
||||
const Eigen::MatrixXd S = embed(h_x, 30) * P0 * embed(h_x, 30).transpose() +
|
||||
r * Eigen::MatrixXd::Identity(m, m);
|
||||
const Eigen::JacobiSVD<Eigen::MatrixXd> svd(S);
|
||||
worst_cond = std::max(worst_cond, svd.singularValues()(0) /
|
||||
svd.singularValues()(m - 1));
|
||||
}
|
||||
const bool pass = all_ran && all_finite && worst_dx < tol && worst_P < tol;
|
||||
if (!pass) {
|
||||
++failures;
|
||||
}
|
||||
std::printf("%-18s m=%3d dx_err=%.3e P_err=%.3e cond=%.1f finite=%d %s\n",
|
||||
m < 30 ? "covariance" : "information", m,
|
||||
worst_dx, worst_P, worst_cond, static_cast<int>(all_finite),
|
||||
pass ? "PASS" : "FAIL");
|
||||
}
|
||||
}
|
||||
|
||||
std::printf("\n=== C. rank-deficient prior ===\n");
|
||||
{
|
||||
Eigen::MatrixXd P = Eigen::MatrixXd::Identity(30, 30) * 0.5;
|
||||
P(0, 1) = P(1, 0) = 0.5; // singular
|
||||
Eigen::MatrixXd h_x = Eigen::MatrixXd::Zero(12, 12);
|
||||
h_x.setIdentity();
|
||||
Eigen::VectorXd z = Eigen::VectorXd::Ones(12) * 0.01;
|
||||
Outcome o = run_case(P, h_x, z, r);
|
||||
// A singular prior must still produce a finite, reference-matching update:
|
||||
// S = H P H^T + r I stays positive definite because r > 0, and the gain
|
||||
// K = P H^T S^-1 never needs P^-1.
|
||||
const bool pass = o.ran && o.finite && o.dx_err < tol && o.P_err < tol;
|
||||
if (!pass) {
|
||||
++failures;
|
||||
}
|
||||
std::printf(" m=12 dx_err=%.3e P_err=%.3e ran=%d finite=%d %s\n",
|
||||
o.dx_err, o.P_err, static_cast<int>(o.ran),
|
||||
static_cast<int>(o.finite), pass ? "PASS" : "FAIL");
|
||||
}
|
||||
|
||||
std::printf("\n%s (%d failure%s)\n", failures == 0 ? "ALL PASS" : "FAILURES",
|
||||
failures, failures == 1 ? "" : "s");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -202,6 +202,9 @@ public:
|
||||
state x_propagated = x_;
|
||||
int dof_Measurement;
|
||||
double m_noise;
|
||||
last_update_state_dof_ = -1;
|
||||
last_update_effective_m_ = -1;
|
||||
last_update_information_branch_ = false;
|
||||
for (int i = 0; i < maximum_iter; i++) {
|
||||
dyn_share.valid = true;
|
||||
h_dyn_share_modified_1(x_, P_.template block<3, 3>(0, 0), P_.template block<3, 3>(3, 3), dyn_share);
|
||||
@@ -232,11 +235,18 @@ public:
|
||||
}
|
||||
K_ = PHT * HPHT.inverse();
|
||||
} else {
|
||||
Matrix<scalar_type, 12, 12> HTH = m_noise * h_x.transpose() * h_x;
|
||||
// Information form. m_noise is a VARIANCE (M_Noise = laser_point_cov),
|
||||
// so it enters as a precision 1/m_noise in both places:
|
||||
// K = (P^-1 + (1/r) H^T H)^-1 H^T (1/r)
|
||||
// Passing r instead of 1/r here made this branch disagree with the
|
||||
// covariance branch above: appending a zero-information row rescaled the
|
||||
// posterior. Regression: docs/diagnostics/esekfom_update_regression.
|
||||
const scalar_type inv_noise = scalar_type(1) / m_noise;
|
||||
Matrix<scalar_type, 12, 12> HTH = inv_noise * h_x.transpose() * h_x;
|
||||
Matrix<scalar_type, n, n> P_inv = P_.inverse();
|
||||
P_inv.template block<12, 12>(0, 0) += HTH;
|
||||
P_inv = P_inv.inverse();
|
||||
K_ = P_inv.template block<n, 12>(0, 0) * h_x.transpose() * m_noise;
|
||||
K_ = P_inv.template block<n, 12>(0, 0) * h_x.transpose() * inv_noise;
|
||||
}
|
||||
Matrix<scalar_type, n, 1> dx_ =
|
||||
K_ * z; // - h) + (K_x - Matrix<scalar_type, n, n>::Identity()) * dx_new;
|
||||
@@ -246,6 +256,9 @@ public:
|
||||
{
|
||||
P_ = P_ - K_ * h_x * P_.template block<12, n>(0, 0);
|
||||
}
|
||||
last_update_state_dof_ = static_cast<int>(n);
|
||||
last_update_effective_m_ = dof_Measurement;
|
||||
last_update_information_branch_ = !(n > dof_Measurement);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -304,8 +317,18 @@ public:
|
||||
|
||||
const state & get_x() const { return x_; }
|
||||
const cov & get_P() const { return P_; }
|
||||
|
||||
// Diagnostics for the most recent update_iterated_dyn_share_modified() call.
|
||||
// Written after the update completes; read-only from outside. These do not
|
||||
// participate in any computation.
|
||||
int last_update_state_dof() const { return last_update_state_dof_; }
|
||||
int last_update_effective_m() const { return last_update_effective_m_; }
|
||||
bool last_update_information_branch() const { return last_update_information_branch_; }
|
||||
cov P_;
|
||||
state x_;
|
||||
int last_update_state_dof_ = -1;
|
||||
int last_update_effective_m_ = -1;
|
||||
bool last_update_information_branch_ = false;
|
||||
|
||||
private:
|
||||
measurement m_;
|
||||
|
||||
Reference in New Issue
Block a user