123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #ifndef TEMP_MGR_INTV
- #error "this file is not a public interface, it should be used *only* within temperature.cpp!"
- #endif
- #include "planner.h"
- constexpr uint8_t TEMP_MODEL_CAL_S = 60;
- constexpr uint8_t TEMP_MODEL_CAL_R_STEP = 4;
- constexpr float TEMP_MODEL_fS = 0.065;
- constexpr float TEMP_MODEL_fE = 0.05;
- constexpr uint8_t TEMP_MODEL_LAG_SIZE = (TEMP_MODEL_LAG / TEMP_MGR_INTV + 0.5);
- constexpr uint8_t TEMP_MODEL_R_SIZE = (1 << FAN_SOFT_PWM_BITS);
- static const float TEMP_MODEL_R_DEFAULT[TEMP_MODEL_R_SIZE] PROGMEM = TEMP_MODEL_Rv;
- namespace temp_model {
- struct model_data
- {
-
- float dT_lag_buf[TEMP_MODEL_LAG_SIZE];
- uint8_t dT_lag_idx = 0;
- float dT_err_prev = 0;
- float T_prev = 0;
-
- float P;
- float C;
- float R[TEMP_MODEL_R_SIZE];
- float Ta_corr;
-
- float warn;
- float err;
-
- union
- {
- bool flags;
- struct
- {
- bool uninitialized: 1;
- bool error: 1;
- bool warning: 1;
- } flag_bits;
- };
-
- float C_i;
- float warn_s;
- float err_s;
-
- void reset(uint8_t heater_pwm, uint8_t fan_pwm, float heater_temp, float ambient_temp);
- void step(uint8_t heater_pwm, uint8_t fan_pwm, float heater_temp, float ambient_temp);
- };
- static bool enabled;
- static bool warn_beep = true;
- static model_data data;
- static bool calibrated();
- static void check();
- volatile static struct
- {
- float dT_err;
- bool warning: 1;
- bool assert: 1;
- } warning_state;
- static void handle_warning();
- #ifdef TEMP_MODEL_DEBUG
- static struct
- {
- volatile struct
- {
- uint32_t stamp;
- int8_t delta_ms;
- uint8_t counter;
- uint8_t cur_pwm;
- float cur_temp;
- float cur_amb;
- } entry;
- uint8_t serial;
- bool enabled;
- } log_buf;
- static void log_usr();
- static void log_isr();
- #endif
- }
- namespace temp_model_cal {
- struct rec_entry
- {
- float temp;
- uint8_t pwm;
- };
- constexpr uint16_t REC_BUFFER_SIZE = TEMP_MODEL_CAL_S / TEMP_MGR_INTV;
- static rec_entry* const rec_buffer = (rec_entry*)block_buffer;
- static_assert(sizeof(rec_entry[REC_BUFFER_SIZE]) <= sizeof(block_buffer),
- "recording length too long to fit within available buffer");
- }
|