safety commit

This commit is contained in:
2026-07-06 20:16:51 +02:00
parent 02e51717e8
commit 28ae97fa45
8 changed files with 806 additions and 34 deletions

View File

@@ -25,7 +25,10 @@ static const float TARGET_TEMP_C = 0.0f; // power-on default: idle (heater off)
static const float AUTOTUNE_DEFAULT_TEMP_C = 40.0f; // autotune when no temp given and idle
static const float TARGET_MIN_C = 0.0f; // 0 = idle (heater off, fan at idle speed)
static const float TARGET_MAX_C = 80.0f;
static const float OVERTEMP_FRACTION = 0.05f; // hard cutoff at target * 1.05
// Absolute max corner temp — heater off + full fan. Decoupled from PID target so you can
// run target 5055 while tuning with headroom for hot corners (ABS in chamber).
static const float EMERGENCY_MAX_TEMP_C = 70.0f;
static const float CORNER_STOP_MARGIN_C = 3.0f; // taper heater when max within this of emergency
// PID on chamber average
static const float PID_KP = 4.0f;
@@ -67,6 +70,9 @@ static const float FAN_OFF_BELOW_TARGET_C = 8.0f; // no heat-up fan when avg thi
static const float FAN_RAMP_BELOW_TARGET_C = 15.0f; // fan ramps in between this and FAN_OFF_BELOW
static const uint8_t FAN_MIX_MAX_PWM = 200; // ~78 % — cap for spread-driven mixing
static const uint8_t FAN_MAX_PWM = 255; // failsafe / over-temp only
// Most 24 V MOSFET modules are active-low (pin LOW = fan on). If off/speed seem wrong,
// try flipping this and reflash. Test: `fan test 0` (off) vs `fan test 200` vs `fan test 255`.
static const bool FAN_PWM_INVERT = true;
// Corner mixing — moderate airflow; full speed reserved for safety
static const float SPREAD_DEADBAND_C = 0.5f;
@@ -84,6 +90,20 @@ static const uint32_t AUTOTUNE_RELAY_STALL_MS = 1500000UL; // 25 min in rel
static const uint32_t AUTOTUNE_SESSION_TIMEOUT_MS = 3600000UL; // 60 min total
static const uint32_t AUTOTUNE_RELAY_PERIOD_MAX_MS = 2400000UL;
// Fan step-response — open-loop heater, fan PWM steps (command: stepresp)
static const float STEPRESP_DEFAULT_TEMP_C = 45.0f;
static const float STEPRESP_DEFAULT_HEATER_PCT = 35.0f;
static const float STEPRESP_MIN_HEATER_PCT = 10.0f;
static const float STEPRESP_MAX_HEATER_PCT = 70.0f;
static const float STEPRESP_PREHEAT_BAND_C = 2.0f;
static const uint32_t STEPRESP_PREHEAT_TIMEOUT_MS = 1200000UL; // 20 min
static const uint32_t STEPRESP_BASELINE_MS = 120000UL; // 2 min fan-off baseline
static const uint32_t STEPRESP_STEP_HOLD_MS = 300000UL; // 5 min per fan level
static const uint32_t STEPRESP_LOG_INTERVAL_MS = 1000UL;
static const uint8_t STEPRESP_FAN_STEPS[] = {0, 77, 140, 200, 255};
static const uint8_t STEPRESP_FAN_STEP_COUNT =
sizeof(STEPRESP_FAN_STEPS) / sizeof(STEPRESP_FAN_STEPS[0]);
// ---------------------------------------------------------------------------
// Timing
// ---------------------------------------------------------------------------

268
include/fan_step_response.h Normal file
View File

@@ -0,0 +1,268 @@
#pragma once
#include <Arduino.h>
#include "config.h"
class FanStepResponse {
public:
enum class Phase : uint8_t { Idle, Preheat, Baseline, StepHold, Done, Failed };
FanStepResponse()
: phase_(Phase::Idle),
targetC_(STEPRESP_DEFAULT_TEMP_C),
heaterPct_(STEPRESP_DEFAULT_HEATER_PCT),
stepIndex_(0),
sessionStartMs_(0),
phaseStartMs_(0),
lastLogMs_(0),
lastAvgC_(0.0f) {}
Phase phase() const { return phase_; }
bool isActive() const {
return phase_ == Phase::Preheat || phase_ == Phase::Baseline || phase_ == Phase::StepHold;
}
uint32_t elapsedMs(uint32_t nowMs) const {
if (sessionStartMs_ == 0) {
return 0;
}
return nowMs - sessionStartMs_;
}
uint8_t stepIndex() const { return stepIndex_; }
uint8_t stepCount() const { return STEPRESP_FAN_STEP_COUNT; }
float targetC() const { return targetC_; }
float heaterPct() const { return heaterPct_; }
uint8_t currentFanPwm() const {
if (stepIndex_ >= STEPRESP_FAN_STEP_COUNT) {
return 0;
}
return STEPRESP_FAN_STEPS[stepIndex_];
}
const char *phaseName() const {
switch (phase_) {
case Phase::Preheat:
return "preheat";
case Phase::Baseline:
return "baseline";
case Phase::StepHold:
return "step";
default:
return "";
}
}
bool start(float targetC, float heaterPct) {
if (targetC < 25.0f || targetC > TARGET_MAX_C) {
return false;
}
if (heaterPct < STEPRESP_MIN_HEATER_PCT || heaterPct > STEPRESP_MAX_HEATER_PCT) {
return false;
}
targetC_ = targetC;
heaterPct_ = heaterPct;
stepIndex_ = 0;
sessionStartMs_ = millis();
phaseStartMs_ = sessionStartMs_;
lastLogMs_ = 0;
lastAvgC_ = 0.0f;
phase_ = Phase::Preheat;
Serial.print(F("stepresp: preheat to "));
Serial.print(targetC_ - STEPRESP_PREHEAT_BAND_C, 1);
Serial.print(F("-"));
Serial.print(targetC_, 1);
Serial.print(F("C avg, heater="));
Serial.print(heaterPct_, 0);
Serial.println(F("% fan=0"));
return true;
}
void abort() {
if (isActive()) {
Serial.println(F("stepresp: cancelled"));
}
phase_ = Phase::Idle;
sessionStartMs_ = 0;
}
void reset() {
phase_ = Phase::Idle;
sessionStartMs_ = 0;
}
bool update(float avgTempC, float maxTempC, float spreadC, uint32_t nowMs, float &heaterDutyOut,
uint8_t &fanPwmOut) {
heaterDutyOut = 0.0f;
fanPwmOut = 0;
if (phase_ == Phase::Idle || phase_ == Phase::Done || phase_ == Phase::Failed) {
return false;
}
if (maxTempC >= EMERGENCY_MAX_TEMP_C) {
fail(F("stepresp: abort — max sensor at safety limit"));
return false;
}
if (phase_ == Phase::Preheat) {
fanPwmOut = 0;
if (nowMs - phaseStartMs_ > STEPRESP_PREHEAT_TIMEOUT_MS) {
fail(F("stepresp: abort — preheat timeout"));
return false;
}
if (avgTempC >= targetC_ - STEPRESP_PREHEAT_BAND_C) {
enterBaseline(nowMs);
} else {
heaterDutyOut = heaterPct_;
}
return true;
}
heaterDutyOut = heaterPct_;
fanPwmOut = currentFanPwm();
if (phase_ == Phase::Baseline) {
if (nowMs - phaseStartMs_ >= STEPRESP_BASELINE_MS) {
advanceStep(nowMs);
}
return true;
}
if (phase_ == Phase::StepHold) {
if (nowMs - phaseStartMs_ >= STEPRESP_STEP_HOLD_MS) {
if (stepIndex_ + 1 >= STEPRESP_FAN_STEP_COUNT) {
finish(nowMs, avgTempC, spreadC);
} else {
++stepIndex_;
enterStepHold(nowMs, true);
}
}
return true;
}
return false;
}
void logIfDue(const float *sensorTemps, const bool *sensorValid, uint8_t sensorCount,
float avgTempC, float minTempC, float maxTempC, float spreadC, uint32_t nowMs) {
if (!isActive()) {
return;
}
if (lastLogMs_ != 0 && nowMs - lastLogMs_ < STEPRESP_LOG_INTERVAL_MS) {
return;
}
lastLogMs_ = nowMs;
lastAvgC_ = avgTempC;
Serial.print(F("sr,"));
Serial.print(nowMs);
Serial.print(',');
Serial.print(phaseName());
Serial.print(',');
Serial.print(stepIndex_);
Serial.print('/');
Serial.print(STEPRESP_FAN_STEP_COUNT);
Serial.print(',');
Serial.print(heaterPct_, 0);
Serial.print(',');
Serial.print(currentFanPwm());
Serial.print(',');
Serial.print(avgTempC, 2);
Serial.print(',');
Serial.print(minTempC, 2);
Serial.print(',');
Serial.print(maxTempC, 2);
Serial.print(',');
Serial.print(spreadC, 2);
for (uint8_t i = 0; i < sensorCount; ++i) {
Serial.print(',');
if (sensorValid[i]) {
Serial.print(sensorTemps[i], 2);
}
}
Serial.println();
}
private:
void enterBaseline(uint32_t nowMs) {
phase_ = Phase::Baseline;
phaseStartMs_ = nowMs;
stepIndex_ = 0;
Serial.print(F("stepresp: baseline fan="));
Serial.print(currentFanPwm());
Serial.print(F(" for "));
Serial.print(STEPRESP_BASELINE_MS / 1000UL);
Serial.println(F("s"));
}
void enterStepHold(uint32_t nowMs, bool isStep) {
phase_ = Phase::StepHold;
phaseStartMs_ = nowMs;
Serial.print(F("stepresp: "));
if (isStep) {
Serial.print(F("step "));
}
Serial.print(stepIndex_ + 1);
Serial.print(F("/"));
Serial.print(STEPRESP_FAN_STEP_COUNT);
Serial.print(F(" fan="));
Serial.print(currentFanPwm());
Serial.print(F(" ("));
Serial.print((currentFanPwm() * 100) / 255);
Serial.print(F("%) hold "));
Serial.print(STEPRESP_STEP_HOLD_MS / 1000UL);
Serial.println(F("s"));
}
void advanceStep(uint32_t nowMs) {
if (STEPRESP_FAN_STEP_COUNT <= 1) {
finish(nowMs, lastAvgC_, 0.0f);
return;
}
stepIndex_ = 1;
enterStepHold(nowMs, true);
}
void finish(uint32_t nowMs, float avgTempC, float spreadC) {
phase_ = Phase::Done;
Serial.print(F("stepresp: done in "));
Serial.print((nowMs - sessionStartMs_) / 1000UL);
Serial.println(F("s"));
Serial.print(F(" target="));
Serial.print(targetC_, 1);
Serial.print(F("C heater="));
Serial.print(heaterPct_, 0);
Serial.print(F("% final avg="));
Serial.print(avgTempC, 1);
Serial.print(F("C spread="));
Serial.print(spreadC, 1);
Serial.println(F("C"));
Serial.println(F(" parse sr,... lines for step response (fan PWM vs temp)"));
}
void fail(const __FlashStringHelper *reason) {
Serial.println(reason);
phase_ = Phase::Failed;
sessionStartMs_ = 0;
}
Phase phase_;
float targetC_;
float heaterPct_;
uint8_t stepIndex_;
uint32_t sessionStartMs_;
uint32_t phaseStartMs_;
uint32_t lastLogMs_;
float lastAvgC_;
};

View File

@@ -3,6 +3,7 @@
#include <Arduino.h>
#include "config.h"
#include "fan_step_response.h"
#include "pid_autotuner.h"
#include "pid_controller.h"
#include "settings_store.h"
@@ -10,11 +11,12 @@
class ThermalController {
public:
enum class HeaterBlock : uint8_t { None, Cutoff, Corner, Allow, Autotune };
enum class HeaterBlock : uint8_t { None, Cutoff, Corner, Allow, Autotune, StepResp };
ThermalController()
: pid_(PID_KP, PID_KI, PID_KD, 0.0f, 100.0f),
autotuner_(),
stepresp_(),
targetTempC_(TARGET_TEMP_C),
heaterDutyPercent_(0.0f),
heaterAllowancePercent_(100.0f),
@@ -40,6 +42,7 @@ public:
pinMode(FAN_PIN, OUTPUT);
pinMode(HEATER_PIN, OUTPUT);
digitalWrite(HEATER_PIN, LOW);
writeFan(0);
pid_.setSetpoint(targetTempC_);
pid_.reset();
@@ -105,7 +108,7 @@ public:
bool isAdaptive() const { return adaptiveEnabled_; }
bool startAutotune(float setpointC) {
if (autotuner_.isActive()) {
if (autotuner_.isActive() || stepresp_.isActive()) {
return false;
}
adaptiveEnabled_ = false;
@@ -128,6 +131,46 @@ public:
float autotunePreheatTargetC() const { return autotuner_.preheatTargetC(); }
bool startStepResponse(float targetC, float heaterPct) {
if (autotuner_.isActive() || stepresp_.isActive()) {
return false;
}
stopFanTest();
adaptiveEnabled_ = false;
cutoffActive_ = false;
pid_.reset();
setTarget(targetC, false);
if (!stepresp_.start(targetC, heaterPct)) {
return false;
}
writeFan(0);
return true;
}
void stopStepResponse() {
stepresp_.abort();
writeFan(0);
}
bool isStepResponseActive() const { return stepresp_.isActive(); }
uint32_t stepResponseElapsedMs(uint32_t nowMs) const { return stepresp_.elapsedMs(nowMs); }
const char *stepResponsePhaseName() const { return stepresp_.phaseName(); }
uint8_t stepResponseStepIndex() const { return stepresp_.stepIndex(); }
uint8_t stepResponseStepCount() const { return stepresp_.stepCount(); }
float stepResponseHeaterPct() const { return stepresp_.heaterPct(); }
void logStepResponseIfDue(const float *sensorTemps, const bool *sensorValid, uint8_t sensorCount,
float avgTempC, float minTempC, float maxTempC, float spreadC,
uint32_t nowMs) {
stepresp_.logIfDue(sensorTemps, sensorValid, sensorCount, avgTempC, minTempC, maxTempC,
spreadC, nowMs);
}
bool commitAutotuneIfDone() {
if (autotuner_.phase() != PidAutotuner::Phase::Done) {
return false;
@@ -206,7 +249,7 @@ public:
if (isIdle()) {
return INFINITY;
}
return targetTempC_ * (1.0f + OVERTEMP_FRACTION);
return EMERGENCY_MAX_TEMP_C;
}
bool isCutoffActive() const { return cutoffActive_; }
@@ -249,6 +292,8 @@ public:
return "allow";
case HeaterBlock::Autotune:
return "autotune";
case HeaterBlock::StepResp:
return "stepresp";
default:
return "none";
}
@@ -264,6 +309,11 @@ public:
SPREAD_EMA_ALPHA * cornerSpreadC +
(1.0f - SPREAD_EMA_ALPHA) * cornerSpreadC_;
if (stepresp_.isActive()) {
updateStepResponse(avgTempC, maxTempC, nowMs);
return;
}
if (autotuner_.isActive()) {
updateAutotune(avgTempC, maxTempC, nowMs);
return;
@@ -296,6 +346,7 @@ public:
applyFan(millis());
pid_.reset();
autotuner_.abort();
stepresp_.abort();
}
void forceHeaterOff() {
@@ -307,16 +358,51 @@ public:
void writeFan(uint8_t pwm) {
fanPwm_ = pwm;
if (pwm == 0) {
// Re-assert output and stop Timer0 PWM on D5 — analogWrite(0) can leave the pin driving
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
} else {
analogWrite(FAN_PIN, pwm);
pinMode(FAN_PIN, OUTPUT);
if (FAN_PWM_INVERT) {
if (pwm == 0) {
digitalWrite(FAN_PIN, HIGH);
return;
}
if (pwm >= 254) {
digitalWrite(FAN_PIN, LOW);
return;
}
analogWrite(FAN_PIN, static_cast<uint8_t>(255 - pwm));
return;
}
if (pwm == 0) {
digitalWrite(FAN_PIN, LOW);
return;
}
if (pwm >= 254) {
digitalWrite(FAN_PIN, HIGH);
return;
}
analogWrite(FAN_PIN, pwm);
}
private:
void updateStepResponse(float avgTempC, float maxTempC, uint32_t nowMs) {
float duty = 0.0f;
uint8_t fan = 0;
stepresp_.update(avgTempC, maxTempC, cornerSpreadC_, nowMs, duty, fan);
heaterDutyPercent_ = duty;
heaterAllowancePercent_ = duty;
heaterBlock_ = duty > 0.0f ? HeaterBlock::StepResp : HeaterBlock::None;
applyHeaterBurst(nowMs);
writeFan(fan);
lastHeaterUpdateMs_ = nowMs;
if (stepresp_.phase() == FanStepResponse::Phase::Done ||
stepresp_.phase() == FanStepResponse::Phase::Failed) {
stepresp_.reset();
}
}
void updateAutotune(float avgTempC, float maxTempC, uint32_t nowMs) {
float duty = 0.0f;
uint8_t fan = FAN_HEAT_MIN_PWM;
@@ -439,22 +525,10 @@ private:
}
float maxHeatStopTemp(float avgTempC) const {
if (avgTempC >= targetTempC_) {
return targetTempC_;
if (!shouldLimitMaxCorner(avgTempC)) {
return EMERGENCY_MAX_TEMP_C;
}
float stopAt = targetTempC_;
if (isBalancedChamber()) {
stopAt = targetTempC_ + BALANCED_MAX_ABOVE_TARGET_C;
} else {
stopAt = targetTempC_ + cornerSpreadC_ * SPREAD_HEADROOM_FACTOR + 1.0f;
}
const float cutoff = cutoffThreshold();
if (stopAt > cutoff) {
stopAt = cutoff;
}
return stopAt;
return EMERGENCY_MAX_TEMP_C - CORNER_STOP_MARGIN_C;
}
float allowanceFromMaxCorner(float maxTempC, float avgTempC) const {
@@ -614,7 +688,7 @@ private:
}
if (isIdle()) {
if (!sensorWarmValid_ || lastMaxTempC_ >= IDLE_AUTO_FAN_OFF_TEMP_C) {
if (sensorWarmValid_ && lastMaxTempC_ >= IDLE_AUTO_FAN_OFF_TEMP_C) {
writeFan(FAN_MAX_PWM);
} else if (fanIdleOverride_) {
writeFan(FAN_IDLE_PWM);
@@ -635,6 +709,7 @@ private:
PidController pid_;
PidAutotuner autotuner_;
FanStepResponse stepresp_;
float targetTempC_;
float heaterDutyPercent_;
float heaterAllowancePercent_;

View File

@@ -5,15 +5,15 @@
#include "config.h"
static const uint16_t TUNING_MAGIC = 0xDA7A;
static const uint16_t TUNING_MAGIC = 0xDA7B;
static const int TUNING_EEPROM_ADDR = 0;
struct TuningData {
uint16_t magic = 0;
float kp = PID_KP;
float ki = PID_KI;
float kd = PID_KD;
uint8_t fanMixMax = FAN_MIX_MAX_PWM;
float heatKp = HEAT_PI_KP;
float heatKi = HEAT_PI_KI;
float mixKp = MIX_PI_KP;
float mixKi = MIX_PI_KI;
};
inline uint8_t tuningChecksum(const TuningData &data) {