Skip to content

Dimensional mismatch in IMU: Comparing squared angle (thetaMagnitudeSq) with linear threshold #11671

Description

@Tarkiya

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

  1. This is not a runtime bug reproducible via a specific flight configuration, but a mathematical mismatch identified via static code analysis.
  2. Inspect src/main/flight/imu.c at line 578.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions