Current Behavior
A mathematical and dimensional mismatch was detected in the IMU update logic within src/main/flight/imu.c.
The variable thetaMagnitudeSq represents the squared norm of the rotation vector (with the dimension of rad²). However, it is being compared against fast_fsqrtf(24.0f * 1e-6f). The square root operation produces a linear scalar threshold (dimension of rad).
Comparing a squared value directly against a linearly scaled square-root value causes a residual power mismatch, leading to an incorrect threshold evaluation.
Code Evidence:
Location: src/main/flight/imu.c (around line 578)
Permalink:
|
if (thetaMagnitudeSq * thetaMagnitudeSq < 24.0e-6f) { |
Steps to Reproduce
- This is not a runtime bug reproducible via a specific flight configuration, but a mathematical mismatch identified via static code analysis.
- Inspect
src/main/flight/imu.c at line 578.
- Observe the direct comparison between
thetaMagnitudeSq (a squared magnitude) and fast_fsqrtf(...) (a linear threshold).
Expected behavior
Let the threshold constant be T = 24.0 * 1e-6.
The current logic evaluates: θ² (rad²) < √T (rad)
This is physically and mathematically inconsistent. Usually, to avoid the computational cost of calculating θ = √θ², algorithms compare squared magnitudes against squared thresholds. If T is the intended squared threshold, it should be compared directly. If √T is the intended linear threshold, the right side should be squared.
Suggested solution(s)
Depending on what the exact intended threshold is, the fix should align the dimensions.
Option 1 (If 24.0f * 1e-6f is the intended squared threshold):
Remove the fast_fsqrtf to compare rad² with rad², which logically fixes the unit and saves CPU cycles:
if (thetaMagnitudeSq < (24.0f * 1e-6f)) {
Option 2 (If 24.0f * 1e-6f is a linear threshold variance that needs to be squared):
if (thetaMagnitudeSq < sq(24.0f * 1e-6f)) {
Additional context
This issue was automatically flagged by our experimental LLM-assisted dimensional analysis tool and manuallyverified.
Current Behavior
A mathematical and dimensional mismatch was detected in the IMU update logic within
src/main/flight/imu.c.The variable
thetaMagnitudeSqrepresents the squared norm of the rotation vector (with the dimension of rad²). However, it is being compared againstfast_fsqrtf(24.0f * 1e-6f). The square root operation produces a linear scalar threshold (dimension of rad).Comparing a squared value directly against a linearly scaled square-root value causes a residual power mismatch, leading to an incorrect threshold evaluation.
Code Evidence:
Location:
src/main/flight/imu.c(around line 578)Permalink:
inav/src/main/flight/imu.c
Line 578 in 4939a7f
Steps to Reproduce
src/main/flight/imu.cat line 578.thetaMagnitudeSq(a squared magnitude) andfast_fsqrtf(...)(a linear threshold).Expected behavior
Let the threshold constant be
T = 24.0 * 1e-6.The current logic evaluates:
θ² (rad²) < √T (rad)This is physically and mathematically inconsistent. Usually, to avoid the computational cost of calculating
θ = √θ², algorithms compare squared magnitudes against squared thresholds. IfTis the intended squared threshold, it should be compared directly. If√Tis the intended linear threshold, the right side should be squared.Suggested solution(s)
Depending on what the exact intended threshold is, the fix should align the dimensions.
Option 1 (If
24.0f * 1e-6fis the intended squared threshold):Remove the
fast_fsqrtfto compare rad² with rad², which logically fixes the unit and saves CPU cycles:Option 2 (If
24.0f * 1e-6fis a linear threshold variance that needs to be squared):Additional context
This issue was automatically flagged by our experimental LLM-assisted dimensional analysis tool and manuallyverified.