Skip to content

Commit cd0a4ce

Browse files
committed
ism330dl: Add two-point temperature calibration methods.
1 parent a6293f9 commit cd0a4ce

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

lib/ism330dl/ism330dl/device.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def __init__(self, i2c, address=ISM330DL_I2C_DEFAULT_ADDR):
1616
self._accel_scale = ACCEL_FS_2G
1717
self._gyro_scale = GYRO_FS_250DPS
1818

19+
self._temp_gain = 1.0
20+
self._temp_offset = 0.0
21+
1922
self.check_device()
2023
self.reset()
2124
self.configure_accel(ACCEL_ODR_104HZ, ACCEL_FS_2G)
@@ -149,8 +152,28 @@ def gyroscope_rads(self):
149152
return tuple(v * pi / 180.0 for v in dps)
150153

151154
def temperature_c(self):
152-
raw = self.temperature_raw()
153-
return TEMP_OFFSET + raw / TEMP_SENSITIVITY
155+
factory = TEMP_OFFSET + self.temperature_raw() / TEMP_SENSITIVITY
156+
return self._temp_gain * factory + self._temp_offset
157+
158+
def set_temp_offset(self, offset_c):
159+
"""Set a temperature offset in °C (gain remains 1.0)."""
160+
self._temp_gain = 1.0
161+
self._temp_offset = float(offset_c)
162+
163+
def calibrate_temperature(self, ref_low, measured_low, ref_high, measured_high):
164+
"""Two-point calibration from reference measurements.
165+
166+
Args:
167+
ref_low: reference temperature at the low point (°C).
168+
measured_low: sensor reading at the low point (°C).
169+
ref_high: reference temperature at the high point (°C).
170+
measured_high: sensor reading at the high point (°C).
171+
"""
172+
delta = float(measured_high - measured_low)
173+
if delta == 0.0:
174+
raise ValueError("measured_low and measured_high must differ")
175+
self._temp_gain = float(ref_high - ref_low) / delta
176+
self._temp_offset = float(ref_low) - self._temp_gain * float(measured_low)
154177

155178
def orientation(self):
156179
ax, ay, az = self.acceleration_g()

tests/scenarios/ism330dl.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,33 @@ tests:
145145
expect_true: true
146146
mode: [mock]
147147

148+
- name: "Temperature offset shifts reading"
149+
action: script
150+
script: |
151+
dev.set_temp_offset(-2.5)
152+
result = abs(dev.temperature_c() - 22.5) < 0.1
153+
expect_true: true
154+
mode: [mock]
155+
156+
- name: "Temperature with two-point calibration"
157+
action: script
158+
script: |
159+
dev.calibrate_temperature(20.0, 25.0, 30.0, 35.0)
160+
result = dev.temperature_c()
161+
expect_range: [19.0, 21.0]
162+
mode: [mock]
163+
164+
- name: "Calibration rejects equal measurements"
165+
action: script
166+
script: |
167+
try:
168+
dev.calibrate_temperature(20.0, 25.0, 30.0, 25.0)
169+
result = False
170+
except ValueError:
171+
result = True
172+
expect_true: true
173+
mode: [mock]
174+
148175
- name: "Temperature in plausible range"
149176
action: call
150177
method: temperature_c

0 commit comments

Comments
 (0)