fix: prevent ZeroDivisionError in IMU temperature interpolation on sensor stall#1678
Merged
amilcarlucas merged 1 commit intoJun 7, 2026
Conversation
8cd16d7 to
269e896
Compare
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.
269e896 to
f053ee2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Testing
Added two regression tests to tests/test_tempcal_imu.py:
All 18 tests pass locally.