Skip to content

Commit 2e2c2b1

Browse files
committed
wsen-pads: Add two-point temperature calibration methods.
1 parent ca5e4a5 commit 2e2c2b1

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

lib/wsen-pads/wsen_pads/device.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def __init__(self, i2c, address=WSEN_PADS_I2C_DEFAULT_ADDR):
2828
"""
2929
self.i2c = i2c
3030
self.address = address
31+
self._temp_gain = 1.0
32+
self._temp_offset = 0.0
3133

3234
# Wait for the sensor boot sequence after power-up.
3335
sleep_ms(BOOT_DELAY_MS)
@@ -269,7 +271,8 @@ def temperature(self):
269271
"""
270272
Read and return temperature in degrees Celsius.
271273
"""
272-
return self.temperature_raw() * TEMPERATURE_C_PER_DIGIT
274+
factory = self.temperature_raw() * TEMPERATURE_C_PER_DIGIT
275+
return self._temp_gain * factory + self._temp_offset
273276

274277
def read(self):
275278
"""
@@ -285,7 +288,8 @@ def read(self):
285288
p_raw = self._to_signed24((p_data[2] << 16) | (p_data[1] << 8) | p_data[0])
286289
t_data = self._read_block(REG_DATA_T_L, 2)
287290
t_raw = self._to_signed16((t_data[1] << 8) | t_data[0])
288-
return p_raw * PRESSURE_HPA_PER_DIGIT, t_raw * TEMPERATURE_C_PER_DIGIT
291+
t_c = self._temp_gain * (t_raw * TEMPERATURE_C_PER_DIGIT) + self._temp_offset
292+
return p_raw * PRESSURE_HPA_PER_DIGIT, t_c
289293

290294
# ---------------------------------------------------------------------
291295
# One-shot mode
@@ -410,3 +414,34 @@ def disable_low_pass(self):
410414
current = self._read_reg(REG_CTRL_1)
411415
current &= ~(CTRL1_EN_LPFP | CTRL1_LPFP_CFG)
412416
self._write_reg(REG_CTRL_1, current)
417+
418+
# ---------------------------------------------------------------------
419+
# Temperature calibration
420+
# ---------------------------------------------------------------------
421+
422+
def set_temp_offset(self, offset_c):
423+
"""Set a temperature offset in °C (gain remains 1.0).
424+
425+
Args:
426+
offset_c: offset value in degrees Celsius.
427+
"""
428+
self._temp_gain = 1.0
429+
self._temp_offset = float(offset_c)
430+
431+
def calibrate_temperature(self, ref_low, measured_low, ref_high, measured_high):
432+
"""Two-point calibration from reference measurements.
433+
434+
Computes gain and offset so that the sensor reading is adjusted
435+
to match reference values at two temperature points.
436+
437+
Args:
438+
ref_low: reference temperature at the low point (°C).
439+
measured_low: sensor reading at the low point (°C).
440+
ref_high: reference temperature at the high point (°C).
441+
measured_high: sensor reading at the high point (°C).
442+
"""
443+
delta = float(measured_high - measured_low)
444+
if delta == 0.0:
445+
raise ValueError("measured_low and measured_high must differ")
446+
self._temp_gain = float(ref_high - ref_low) / delta
447+
self._temp_offset = float(ref_low) - self._temp_gain * float(measured_low)

tests/scenarios/wsen_pads.yaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,29 @@ tests:
4949
- name: "Read pressure returns float"
5050
action: call
5151
method: pressure
52-
expect_not_none: true
52+
expect_range: [1013.0, 1014.0]
5353
mode: [mock]
5454

5555
- name: "Read temperature returns float"
5656
action: call
5757
method: temperature
58-
expect_not_none: true
58+
expect: 25.0
59+
mode: [mock]
60+
61+
- name: "Temperature with offset"
62+
action: script
63+
script: |
64+
dev.set_temp_offset(-2.0)
65+
result = dev.temperature()
66+
expect_range: [22.0, 24.0]
67+
mode: [mock]
68+
69+
- name: "Temperature with two-point calibration"
70+
action: script
71+
script: |
72+
dev.calibrate_temperature(20.0, 25.0, 30.0, 35.0)
73+
result = dev.temperature()
74+
expect_range: [19.0, 21.0]
5975
mode: [mock]
6076

6177
- name: "Pressure in plausible range"

0 commit comments

Comments
 (0)