Improved BME690 heater management flow

This commit is contained in:
grove5_admin committed 2026-05-01 15:59:35 -07:00
1 parent 006f3cb2d4
commit 6e7efe77b3
2 files changed
+76 -27

No files matched your search

+72 -23
View File
@@ -14,7 +14,7 @@ static const uint16_t SWEEP_TEMPS[CULTIVAR_SWEEP_COUNT] = {
};
static const uint16_t SWEEP_DURS[CULTIVAR_SWEEP_COUNT] = {
150, 150, 150, // Zone 1
200, 200, 200, // Zone 1 — 200ms ensures heater stabilises at 200-250°C
180, 180, 180, 200, // Zone 2
200, 200, 200 // Zone 3
};
@@ -86,9 +86,17 @@ bool CultivarEnv::_initBME690() {
CULTIVAR_SERIAL_PRINTF("[cultivar_env] Warning: variant_id=0x%02X (expected 0x02 for BME690)\n",
_dev.variant_id);
}
CULTIVAR_SERIAL_PRINTF("[cultivar_env] calib: par_t1=%u par_t2=%d par_t3=%d par_h1=%d par_h2=%d\n",
_dev.calib.par_t1, (int)_dev.calib.par_t2, (int)_dev.calib.par_t3,
(int)_dev.calib.par_h1, (int)_dev.calib.par_h2);
// Always log calibration coefficients at startup. par_h2=0 silently causes
// humidity=0% in the integer compensation path and is otherwise invisible.
{
char _cb[128];
snprintf(_cb, sizeof(_cb),
"[cultivar_env] calib t1=%u t2=%d t3=%d h1=%d h2=%d h3=%d h4=%d h5=%d\n",
(unsigned)_dev.calib.par_t1, (int)_dev.calib.par_t2, (int)_dev.calib.par_t3,
(int)_dev.calib.par_h1, (int)_dev.calib.par_h2, (int)_dev.calib.par_h3,
(int)_dev.calib.par_h4, (int)_dev.calib.par_h5);
Serial.print(_cb);
}
_conf.os_temp = BME69X_OS_2X;
_conf.os_pres = BME69X_OS_16X;
@@ -262,6 +270,14 @@ bool CultivarEnv::_readAmbient(CultivarAmbient &out) {
out.humidity = (float)data.humidity / 1000.0f; // uint32_t %RH×1000 → %RH
out.pressure = (float)data.pressure / 100.0f; // uint32_t Pa → hPa
// Humidity compensation returning exactly 0 means a calibration coefficient
// (likely par_h2) is zero or the integer math path misbehaved on this chip.
// Log it clearly — the par_h* values printed at init will show the cause.
if (data.humidity == 0) {
Serial.println("[cultivar_env] WARN: humidity compensation returned 0 — "
"check par_h1/par_h2 in calibration log above");
}
// Also capture raw ADCs for time-series / Linux-side reprocessing
uint32_t r_temp, r_pres;
uint16_t r_hum, r_gas;
@@ -362,7 +378,7 @@ bool CultivarEnv::_runSweep(CultivarGasPoint *out, uint8_t &count) {
out[i].heater_temp_c = SWEEP_TEMPS[i];
out[i].heater_dur_ms = SWEEP_DURS[i];
out[i].zone = SWEEP_ZONES[i];
out[i].resistance = 0.0f;
out[i].resistance_raw = 0;
out[i].adc_gas_res = 0;
out[i].gas_range = 0;
out[i].res_heat = 0;
@@ -378,16 +394,24 @@ bool CultivarEnv::_runSweep(CultivarGasPoint *out, uint8_t &count) {
if (r != BME69X_OK) continue;
struct bme69x_data data;
if (!_forcedRead(data)) continue;
if (!_forcedRead(data)) {
// Forced mode was already triggered before the timeout — the heater
// may have fired. Always cool before the next point.
delay(200);
continue;
}
bool heater_stable = (data.status & BME69X_HEAT_STAB_MSK) != 0;
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;
out[i].resistance_raw = (uint32_t)data.gas_resistance;
out[i].res_heat = data.res_heat;
out[i].idac = data.idac;
out[i].heater_stable = heater_stable;
// Require both GASM_VALID and HEAT_STAB. A measurement taken when the
// heater didn't reach target temperature is at an unknown temperature —
// it cannot be meaningfully placed on the resistance-vs-temperature curve.
out[i].valid = gas_valid && heater_stable;
// Capture raw gas ADC for Linux-side modelling
uint32_t r_temp, r_pres;
@@ -398,12 +422,23 @@ bool CultivarEnv::_runSweep(CultivarGasPoint *out, uint8_t &count) {
out[i].gas_range = r_range;
}
CULTIVAR_SERIAL_PRINTF("[cultivar_env] sweep[%d] res_heat=%u idac=%u gas_wait=%u gas_res=%lu status=0x%02X\n",
i, (unsigned)data.res_heat, (unsigned)data.idac, (unsigned)data.gas_wait,
(unsigned long)data.gas_resistance, (unsigned)data.status);
CULTIVAR_SERIAL_PRINTF(
"[cultivar_env] sweep[%d] %u°C res=%lu adc=%u range=%u stable=%d valid=%d status=0x%02X\n",
i, (unsigned)SWEEP_TEMPS[i],
(unsigned long)out[i].resistance_raw,
(unsigned)out[i].adc_gas_res, (unsigned)out[i].gas_range,
(int)heater_stable, (int)out[i].valid, (unsigned)data.status);
count++;
delay(50);
// Inter-point cooldown. Since the sweep runs low-to-high (200→400°C),
// the direction is benign for Zone 2/3 — a few degrees of carryover on
// a 280–400°C target is <2% error. Zone 1 (200–250°C) is the sensitive
// case: the heater Δ is only 175–225°C, so carryover matters more and
// calc_res_heat overshoots if the die hasn't cooled. 200ms keeps
// cumulative die heating manageable across the full sweep without
// meaningfully extending total collection time.
delay(200);
}
return count > 0;
@@ -431,6 +466,15 @@ bool CultivarEnv::collect(CultivarEnvPayload &payload) {
_warmed_up = true;
}
// _readAmbient fires a 200°C/200ms heater cycle to keep the Bosch state
// machine in a known state. The die is elevated after it. Without a pause
// here, Zone 1 point 0 (also 200°C) fires on a hot die — calc_res_heat
// computed the DAC current for a cold start at amb_temp, so it overshoots
// and the actual heater temperature lands well above 200°C. This is the
// primary reason Zone 1 ADC values saturate at 1023. 300ms is enough for
// the die to return to within a few degrees of true ambient.
delay(300);
_runSweep(payload.sweep, payload.sweep_count);
_baseline.samples_collected++;
@@ -518,7 +562,12 @@ void CultivarEnv::_warmup() {
// but leaving the chip in a known state is tidy.
_heatr = saved_heatr;
CULTIVAR_SERIAL_PRINTF("[cultivar_env] hot-plate conditioning complete\n");
CULTIVAR_SERIAL_PRINTF("[cultivar_env] hot-plate conditioning complete — cooling 500ms\n");
// Die is thermally saturated after CULTIVAR_WARMUP_CYCLES at 320°C.
// Wait before the first real sweep so calc_res_heat() uses a representative
// amb_temp and the first Zone 1 point isn't measured on a hot element.
delay(500);
}
// ── toJSON ────────────────────────────────────────────────────────────────
@@ -584,7 +633,7 @@ size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
"\"heater_temp_c\":%u,"
"\"heater_dur_ms\":%u,"
"\"zone\":%u,"
"\"resistance_raw\":%.0f,"
"\"resistance_raw\":%lu,"
"\"adc_gas_res\":%u,"
"\"gas_range\":%u,"
"\"res_heat\":%u,"
@@ -596,7 +645,7 @@ size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
p.sweep[i].heater_temp_c,
p.sweep[i].heater_dur_ms,
p.sweep[i].zone,
p.sweep[i].resistance,
(unsigned long)p.sweep[i].resistance_raw,
(unsigned)p.sweep[i].adc_gas_res,
(unsigned)p.sweep[i].gas_range,
(unsigned)p.sweep[i].res_heat,
@@ -652,16 +701,16 @@ void CultivarEnv::printPayload(const CultivarEnvPayload &p) {
p.baseline.established ? "yes" : "no");
if (CULTIVAR_ENV_DEBUG) Serial.println("── Gas Sweep (BME690) ────────────────");
CULTIVAR_SERIAL_PRINTF(" %-6s %-6s %-5s %-16s %-7s %s\n",
CULTIVAR_SERIAL_PRINTF(" %-6s %-6s %-5s %-14s %-7s %s\n",
"Temp°C", "Dur ms", "Zone", "Resistance Ω", "Stable", "Valid");
for (uint8_t i = 0; i < p.sweep_count; i++) {
CULTIVAR_SERIAL_PRINTF(" %-6u %-6u %-5u %-16.0f %-7s %s\n",
CULTIVAR_SERIAL_PRINTF(" %-6u %-6u %-5u %-14lu %-7s %s\n",
p.sweep[i].heater_temp_c,
p.sweep[i].heater_dur_ms,
p.sweep[i].zone,
p.sweep[i].resistance,
p.sweep[i].heater_stable ? "✓" : "✗",
p.sweep[i].valid ? "✓" : "✗");
(unsigned long)p.sweep[i].resistance_raw,
p.sweep[i].heater_stable ? "ok" : "UNSTABLE",
p.sweep[i].valid ? "ok" : "invalid");
}
if (CULTIVAR_ENV_DEBUG) Serial.println("─────────────────────────────────────");
}
+4 -4
View File
@@ -98,13 +98,13 @@ struct CultivarGasPoint {
uint16_t heater_temp_c; // Heater setpoint °C
uint16_t heater_dur_ms; // Heater duration ms
uint8_t zone; // CULTIVAR_ZONE_*
float resistance; // Compensated resistance Ω (from Bosch)
uint16_t adc_gas_res; // Raw 10-bit gas ADC value
uint32_t resistance_raw; // Bosch-compensated resistance Ω (uint32, no float precision loss)
uint16_t adc_gas_res; // Raw 10-bit gas ADC value (0/1023 = railed)
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
bool heater_stable; // HEAT_STAB_MSK was set (heater reached target temp)
bool valid; // GASM_VALID_MSK && HEAT_STAB_MSK — both required
};
/**