From 4c893fb64ea79fc999ddf91fe19c64c4f0fe8ce2 Mon Sep 17 00:00:00 2001 From: jess Date: Thu, 9 Apr 2026 19:03:22 -0700 Subject: [PATCH] pH cal: correct borate to 9.18, add NIST buffer temp correction --- main/eis.c | 40 ++++++++++++++++++++++++++++++++++++++-- main/eis.h | 1 + main/eis4.c | 10 ++++++---- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/main/eis.c b/main/eis.c index 0b3aeea..be02577 100644 --- a/main/eis.c +++ b/main/eis.c @@ -656,7 +656,43 @@ float eis_get_ph_offset(void) { return ph_offset_cached; } /* ---- 3-buffer × 3-temperature pH calibration ---- */ -static const float PH_BUFFERS[PH_CAL_BUFFERS] = {4.0f, 6.86f, 9.0f}; +/* nominal 25 C pH of the three NIST primary buffers (phthalate, phosphate, borate) */ +static const float PH_BUFFERS[PH_CAL_BUFFERS] = {4.01f, 6.86f, 9.18f}; + +/* NIST SRM buffer pH vs temperature, 0-50 C in 5 C steps */ +#define PH_NIST_T_MIN 0 +#define PH_NIST_T_STEP 5 +#define PH_NIST_N 11 + +static const float PH_NIST_PHTHALATE[PH_NIST_N] = { + 4.003f, 3.999f, 3.998f, 3.999f, 4.002f, 4.008f, + 4.015f, 4.024f, 4.035f, 4.047f, 4.060f +}; +static const float PH_NIST_PHOSPHATE[PH_NIST_N] = { + 6.984f, 6.951f, 6.923f, 6.900f, 6.881f, 6.865f, + 6.853f, 6.844f, 6.838f, 6.834f, 6.833f +}; +static const float PH_NIST_BORATE[PH_NIST_N] = { + 9.464f, 9.395f, 9.332f, 9.276f, 9.225f, 9.180f, + 9.139f, 9.102f, 9.068f, 9.038f, 9.011f +}; + +static const float * const PH_NIST_TABLES[PH_CAL_BUFFERS] = { + PH_NIST_PHTHALATE, PH_NIST_PHOSPHATE, PH_NIST_BORATE +}; + +float eis_ph_buffer_at_temp(uint8_t buf, float temp_c) +{ + if (buf >= PH_CAL_BUFFERS) return 0.0f; + const float *tbl = PH_NIST_TABLES[buf]; + if (temp_c <= (float)PH_NIST_T_MIN) return tbl[0]; + float t_max = (float)(PH_NIST_T_MIN + PH_NIST_T_STEP * (PH_NIST_N - 1)); + if (temp_c >= t_max) return tbl[PH_NIST_N - 1]; + float f = (temp_c - (float)PH_NIST_T_MIN) / (float)PH_NIST_T_STEP; + int i = (int)f; + float frac = f - (float)i; + return tbl[i] + frac * (tbl[i + 1] - tbl[i]); +} #define NVS_PH_CAL_PTS_KEY "ph_cal9" @@ -682,7 +718,7 @@ static void ph_cal_recalculate(void) int bit = i * PH_CAL_TEMPS + PH_TEMP_BASE; if (!(ph_cal.valid & (1 << bit))) continue; float x = ph_cal.s[i][PH_TEMP_BASE].ocp_mv; - float y = PH_BUFFERS[i]; + float y = eis_ph_buffer_at_temp(i, ph_cal.s[i][PH_TEMP_BASE].temp_c); sx += x; sy += y; sxx += x * x; sxy += x * y; n++; } diff --git a/main/eis.h b/main/eis.h index 34a50e9..bf8908f 100644 --- a/main/eis.h +++ b/main/eis.h @@ -90,5 +90,6 @@ void eis_ph_cal_clear_all(void); bool eis_ph_cal_get_point(uint8_t buf, uint8_t tslot, float *ocp_mv, float *temp_c); int eis_ph_cal_count(void); float eis_ph_cal_buffer_ph(uint8_t buf); +float eis_ph_buffer_at_temp(uint8_t buf, float temp_c); #endif diff --git a/main/eis4.c b/main/eis4.c index 334423e..b79f3aa 100644 --- a/main/eis4.c +++ b/main/eis4.c @@ -254,8 +254,8 @@ void app_main(void) uint8_t bid = cmd.ph_cal_point.buffer_id; uint8_t tsl = cmd.ph_cal_point.temp_slot; if (bid >= PH_CAL_BUFFERS || tsl >= PH_CAL_TEMPS) break; - float buf_ph = eis_ph_cal_buffer_ph(bid); - printf("pH cal: buffer %u slot %u (pH %.2f)\n", bid, tsl, buf_ph); + printf("pH cal: buffer %u slot %u (nominal pH %.2f)\n", + bid, tsl, eis_ph_cal_buffer_ph(bid)); PhConfig ph_cfg; ph_cfg.stabilize_s = cmd.ph_cal_point.stabilize_s; @@ -266,12 +266,14 @@ void app_main(void) eis_ph_cal_set_point(bid, tsl, ph_result.v_ocp_mv, ph_result.temp_c); + float buf_ph = eis_ph_buffer_at_temp(bid, ph_result.temp_c); + int baseline_n = 0; for (int i = 0; i < PH_CAL_BUFFERS; i++) if (eis_ph_cal_get_point(i, PH_TEMP_BASE, NULL, NULL)) baseline_n++; - printf("pH cal: [%u][%u] OCP=%.1f mV T=%.1f C (%d/%d)\n", - bid, tsl, ph_result.v_ocp_mv, ph_result.temp_c, + printf("pH cal: [%u][%u] OCP=%.1f mV T=%.1f C pH=%.3f (%d/%d)\n", + bid, tsl, ph_result.v_ocp_mv, ph_result.temp_c, buf_ph, baseline_n, eis_ph_cal_count()); send_ph_cal_point(bid, tsl, ph_result.v_ocp_mv, ph_result.temp_c, buf_ph, (uint8_t)baseline_n);