Skip to content

fix: prevent ZeroDivisionError in IMU temperature interpolation on sensor stall#1678

Merged
amilcarlucas merged 1 commit into
ArduPilot:masterfrom
prajakta128:fix/tempcal-imu-division-by-zero
Jun 7, 2026
Merged

fix: prevent ZeroDivisionError in IMU temperature interpolation on sensor stall#1678
amilcarlucas merged 1 commit into
ArduPilot:masterfrom
prajakta128:fix/tempcal-imu-division-by-zero

Conversation

@prajakta128
Copy link
Copy Markdown

While reviewing the temperature calibration code, I found that
accel_at_temp and gyro_at_temp can crash with ZeroDivisionError
when an IMU sensor stalls during a flight log recording.

When a sensor stalls, it reports the same temperature for two
consecutive readings. This makes T[i] == T[i+1], so the
interpolation denominator becomes zero and the function crashes.

Changes made:

accel_at_temp (line 268):
Compute delta_t = T[i+1] - T[i] before dividing.
If delta_t is zero, return the current value instead of dividing.

gyro_at_temp (line 280):
Same fix applied for gyro interpolation.

I tested this by adding two IMU readings with identical temperatures
and confirmed the crash before the fix and no crash after.

Checklist

  • Verified by a human programmer
  • Code follows our coding standards
  • No breaking changes or properly documented

Testing

  • Unit tests pass

Added two regression tests to tests/test_tempcal_imu.py:

  • test_accel_at_temp_duplicate_temperature_does_not_crash
  • test_gyro_at_temp_duplicate_temperature_does_not_crash

All 18 tests pass locally.

@prajakta128 prajakta128 requested a review from amilcarlucas as a code owner June 7, 2026 18:30
@amilcarlucas amilcarlucas force-pushed the fix/tempcal-imu-division-by-zero branch 2 times, most recently from 8cd16d7 to 269e896 Compare June 7, 2026 21:16
amilcarlucas
amilcarlucas previously approved these changes Jun 7, 2026
…olation on sensor stall

- Fix accel_at_temp (line 268): when two consecutive temperature readings
  are identical (T[i] == T[i+1]), division by zero occurs in interpolation.
  Fixed by checking delta_t == 0.0 and returning current value.

- Fix gyro_at_temp (line 280): same ZeroDivisionError for gyro data.
  Fixed with identical guard.

This can happen in real flight logs when the IMU sensor stalls and
reports the same temperature for consecutive readings.

Add two regression tests covering each crash scenario.
@amilcarlucas amilcarlucas force-pushed the fix/tempcal-imu-division-by-zero branch from 269e896 to f053ee2 Compare June 7, 2026 21:45
@amilcarlucas amilcarlucas merged commit c893c4a into ArduPilot:master Jun 7, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants