Add burn-in calibration & transfer more raw gas values

This commit is contained in:
grove5_admin committed 2026-05-01 09:04:25 -07:00
1 parent d97b4a545a
commit 8fade93df3
2 files changed
+125 -3

No files matched your search

+108 -3
View File
@@ -363,6 +363,10 @@ bool CultivarEnv::_runSweep(CultivarGasPoint *out, uint8_t &count) {
out[i].heater_dur_ms = SWEEP_DURS[i];
out[i].zone = SWEEP_ZONES[i];
out[i].resistance = 0.0f;
out[i].adc_gas_res = 0;
out[i].gas_range = 0;
out[i].res_heat = 0;
out[i].idac = 0;
out[i].heater_stable = false;
out[i].valid = false;
@@ -380,6 +384,8 @@ bool CultivarEnv::_runSweep(CultivarGasPoint *out, uint8_t &count) {
bool gas_valid = (data.status & BME69X_GASM_VALID_MSK) != 0;
out[i].resistance = data.gas_resistance;
out[i].res_heat = data.res_heat;
out[i].idac = data.idac;
out[i].heater_stable = heater_stable;
out[i].valid = gas_valid;
@@ -411,6 +417,20 @@ bool CultivarEnv::collect(CultivarEnvPayload &payload) {
bool ok = _readAmbient(payload.ambient);
_readCO2(payload.co2);
_computeHealth(payload.health, payload.ambient, payload.co2);
// Feed best-known ambient into the Bosch heater-target calculation.
// calc_res_heat() reads dev->amb_temp on every bme69x_set_heatr_conf()
// call — without this, amb_temp stays pinned at the init-time default
// (25 °C), biasing the DAC target at upper zones.
_updateAmbTemp(payload.ambient, payload.co2);
// One-shot hot-plate conditioning on the first real cycle. Results are
// discarded; payloads are not emitted during warm-up.
if (!_warmed_up) {
_warmup();
_warmed_up = true;
}
_runSweep(payload.sweep, payload.sweep_count);
_baseline.samples_collected++;
@@ -424,6 +444,83 @@ bool CultivarEnv::pollCO2(CultivarCO2 &out) {
return _readCO2(out);
}
// ── _updateAmbTemp ────────────────────────────────────────────────────────
// Push the best-known chamber ambient into the Bosch dev struct so that
// calc_res_heat() produces an accurate heater DAC target. Prefer the SCD43
// temperature when available — the BME690's own ambient reading tends to
// run several °C high due to self-heating from back-to-back heater pulses,
// which would feed a biased value back into its own heater calculation.
void CultivarEnv::_updateAmbTemp(const CultivarAmbient &ambient,
const CultivarCO2 &co2) {
float t = NAN;
if (co2.valid) {
t = co2.temperature;
} else if (ambient.valid) {
t = ambient.temperature;
} else {
return; // no usable source; leave amb_temp as-is
}
// int8_t clamp per Bosch driver contract (-40 .. 85 °C is the usable
// range for this calibration).
if (t < -40.0f) t = -40.0f;
if (t > 85.0f) t = 85.0f;
int8_t new_amb = (int8_t)lroundf(t);
if (new_amb != _dev.amb_temp) {
CULTIVAR_SERIAL_PRINTF("[cultivar_env] amb_temp %d -> %d °C (src=%s)\n",
(int)_dev.amb_temp, (int)new_amb,
co2.valid ? "scd43" : "bme690");
_dev.amb_temp = new_amb;
}
}
// ── _warmup ───────────────────────────────────────────────────────────────
// Run CULTIVAR_WARMUP_CYCLES forced-mode cycles at a single high heater
// setpoint to condition the MOX hot-plate before the first real sweep.
// Discards all data. Only called once per power-on (guarded by _warmed_up).
void CultivarEnv::_warmup() {
if (!bme_ok) return;
CULTIVAR_SERIAL_PRINTF("[cultivar_env] hot-plate conditioning: %u cycles @ %u°C/%ums\n",
(unsigned)CULTIVAR_WARMUP_CYCLES,
(unsigned)CULTIVAR_WARMUP_TEMP_C,
(unsigned)CULTIVAR_WARMUP_DUR_MS);
struct bme69x_heatr_conf saved_heatr = _heatr;
_heatr.enable = BME69X_ENABLE;
_heatr.heatr_temp = CULTIVAR_WARMUP_TEMP_C;
_heatr.heatr_dur = CULTIVAR_WARMUP_DUR_MS;
int8_t r = bme69x_set_heatr_conf(BME69X_FORCED_MODE, &_heatr, &_dev);
if (r != BME69X_OK) {
CULTIVAR_SERIAL_PRINTF("[cultivar_env] warmup: set_heatr_conf failed r=%d\n", (int)r);
_heatr = saved_heatr;
return;
}
for (uint8_t i = 0; i < CULTIVAR_WARMUP_CYCLES; i++) {
struct bme69x_data data;
bool ok = _forcedRead(data);
if (ok && (i % 5 == 0 || i == CULTIVAR_WARMUP_CYCLES - 1)) {
CULTIVAR_SERIAL_PRINTF("[cultivar_env] warmup[%u/%u] res_heat=%u idac=%u gas_res=%lu status=0x%02X\n",
(unsigned)(i + 1),
(unsigned)CULTIVAR_WARMUP_CYCLES,
(unsigned)data.res_heat, (unsigned)data.idac,
(unsigned long)data.gas_resistance,
(unsigned)data.status);
}
delay(20);
}
// Restore caller's heater config; _runSweep overwrites per-point anyway
// but leaving the chip in a known state is tidy.
_heatr = saved_heatr;
CULTIVAR_SERIAL_PRINTF("[cultivar_env] hot-plate conditioning complete\n");
}
// ── toJSON ────────────────────────────────────────────────────────────────
// Full field names match cultivar_types.py STM32Payload model exactly.
// No translation layer needed on the daemon side.
@@ -469,11 +566,15 @@ size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
p.health.temp_ok ? "true" : "false",
p.health.humidity_ok ? "true" : "false");
// Baseline — full field names
// Baseline — full field names. burn_in mirrors !established for
// downstream convenience (a payload collected while baseline is not yet
// established should not be used for fingerprinting).
n += snprintf(buf + n, len - n,
"\"baseline\":{\"established\":%s,\"n_samples\":%u},",
"\"baseline\":{\"established\":%s,\"n_samples\":%u},"
"\"burn_in\":%s,",
p.baseline.established ? "true" : "false",
p.baseline.samples_collected);
p.baseline.samples_collected,
p.baseline.established ? "false" : "true");
// Gas sweep — full field names
n += snprintf(buf + n, len - n, "\"sweep\":[");
@@ -486,6 +587,8 @@ size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
"\"resistance_raw\":%.0f,"
"\"adc_gas_res\":%u,"
"\"gas_range\":%u,"
"\"res_heat\":%u,"
"\"idac\":%u,"
"\"heater_stable\":%s,"
"\"valid\":%s"
"}",
@@ -496,6 +599,8 @@ size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
p.sweep[i].resistance,
(unsigned)p.sweep[i].adc_gas_res,
(unsigned)p.sweep[i].gas_range,
(unsigned)p.sweep[i].res_heat,
(unsigned)p.sweep[i].idac,
p.sweep[i].heater_stable ? "true" : "false",
p.sweep[i].valid ? "true" : "false");
}
+17
View File
@@ -77,6 +77,16 @@
#define CULTIVAR_ZONE_BIOLOGICAL 2 // 280-340°C — mycelium/contamination
#define CULTIVAR_ZONE_HEAVY 3 // 360-400°C — ammonia/sulfur/heavy VOC
// ── Warm-up / conditioning ────────────────────────────────────────────────
// MOX hotplate conditioning: runs once on the first collect() to burn off
// adsorbed species from shipping/storage and drive the hot-plate film to a
// stable operating point. Results are discarded. Payloads collected before
// Linux flips baseline.established=true are still marked burn_in=true so
// downstream doesn't fingerprint against them.
#define CULTIVAR_WARMUP_CYCLES 30
#define CULTIVAR_WARMUP_TEMP_C 320
#define CULTIVAR_WARMUP_DUR_MS 300
// ── Structs ───────────────────────────────────────────────────────────────
/**
@@ -91,6 +101,8 @@ struct CultivarGasPoint {
float resistance; // Compensated resistance Ω (from Bosch)
uint16_t adc_gas_res; // Raw 10-bit gas ADC value
uint8_t gas_range; // Gas range (0-15, affects resistance calc)
uint8_t res_heat; // Heater resistance register value (DAC target)
uint8_t idac; // Initial heater DAC drive (current setting)
bool heater_stable; // HEAT_STAB_MSK was set
bool valid; // Gas measurement valid
};
@@ -263,6 +275,9 @@ private:
// Baseline state
CultivarBaselineStatus _baseline;
// One-shot hot-plate conditioning — see CULTIVAR_WARMUP_* in header top.
bool _warmed_up = false;
// Buffered CO2 — updated asynchronously by pollCO2(), read by collect()
CultivarCO2 _co2_buffer = {0};
uint32_t _co2_buffer_time_ms = 0;
@@ -274,6 +289,8 @@ private:
bool _readCO2(CultivarCO2 &out);
bool _runSweep(CultivarGasPoint *out, uint8_t &count);
bool _forcedRead(struct bme69x_data &data);
void _warmup();
void _updateAmbTemp(const CultivarAmbient &ambient, const CultivarCO2 &co2);
void _computeHealth(CultivarSensorHealth &health,
const CultivarAmbient &ambient,
const CultivarCO2 &co2);