You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
imu: optimize imuMahonyAHRSupdate() hot path (5 micro-opts)
Five cycle-saving changes to imuMahonyAHRSupdate(), which accounts for
~205 µs (30%) of the PID loop on RP2350. Each change is safe on all
targets; the gains are proportionally smaller on F7/H7 where the function
already lives in ITCM RAM.
1. Replace quaternionRotateVector({0,0,1}) with rMat[2][*] reads.
imuComputeRotationMatrix() is called at the end of every invocation and
keeps rMat in sync with orientation. Rotating the constant gravity
vector EF→BF yields exactly the third row of rMat, so three float loads
replace ~56 floating-point multiply/add operations.
2. Eliminate sqrt() from the Taylor-series threshold.
Original: thetaMagnitudeSq < sqrt(24e-6).
Squaring both sides (both non-negative) gives the equivalent condition
thetaMagnitudeSq² < 24e-6 with no sqrt call.
3. First-order Newton quaternion renormalization replaces quaternionNormalize()
(sqrt + 4 divides, ~35 cycles) with scale = (3 - normSq) * 0.5 (~14 cycles).
At 1 kHz the per-step drift |ε| < 1e-6, making the O(ε²) error < 1e-12.
imuCheckAndResetOrientationQuaternion() remains as the catastrophic-failure
safety net.
4. Precompute the anti-windup i_limit in imuConfigure() (called only on
settings save). The hot path now reads a single float instead of
performing an add, a multiply, and a divide every PID cycle.
Adds dcm_i_limit to imuRuntimeConfig_t and imuConfigure().
5. Reduce prevOrientation snapshot frequency from every PID cycle to
every 100 cycles (~100 ms at 1 kHz). The snapshot is only used by
the fault-recovery path which should never fire in normal flight;
16 bytes of unnecessary SRAM write every ms is not justified.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
0 commit comments