temp_model.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // model-based temperature safety checker declarations
  2. #ifndef TEMP_MGR_INTV
  3. #error "this file is not a public interface, it should be used *only* within temperature.cpp!"
  4. #endif
  5. #include "planner.h"
  6. constexpr uint8_t TEMP_MODEL_CAL_S = 60; // Maximum recording length during calibration (s)
  7. constexpr uint8_t TEMP_MODEL_CAL_R_STEP = 4; // Fan interpolation steps during calibration
  8. constexpr float TEMP_MODEL_fS = 0.065; // simulation filter (1st-order IIR factor)
  9. constexpr float TEMP_MODEL_fE = 0.05; // error filter (1st-order IIR factor)
  10. // transport delay buffer size (samples)
  11. constexpr uint8_t TEMP_MODEL_LAG_SIZE = (TEMP_MODEL_LAG / TEMP_MGR_INTV + 0.5);
  12. // resistance values for all fan levels
  13. constexpr uint8_t TEMP_MODEL_R_SIZE = (1 << FAN_SOFT_PWM_BITS);
  14. static const float TEMP_MODEL_R_DEFAULT[TEMP_MODEL_R_SIZE] PROGMEM = TEMP_MODEL_Rv;
  15. namespace temp_model {
  16. struct model_data
  17. {
  18. // temporary buffers
  19. float dT_lag_buf[TEMP_MODEL_LAG_SIZE]; // transport delay buffer
  20. uint8_t dT_lag_idx = 0; // transport delay buffer index
  21. float dT_err_prev = 0; // previous temperature delta error
  22. float T_prev = 0; // last temperature extruder
  23. // configurable parameters
  24. float P; // heater power (W)
  25. float C; // heatblock capacitance (J/K)
  26. float R[TEMP_MODEL_R_SIZE]; // heatblock resistance for all fan levels (K/W)
  27. float Ta_corr; // ambient temperature correction (K)
  28. // thresholds
  29. float warn; // warning threshold (K/s)
  30. float err; // error threshold (K/s)
  31. // status flags
  32. union
  33. {
  34. bool flags;
  35. struct
  36. {
  37. bool uninitialized: 1; // model is not initialized
  38. bool error: 1; // error threshold set
  39. bool warning: 1; // warning threshold set
  40. } flag_bits;
  41. };
  42. // pre-computed values (initialized via reset)
  43. float C_i; // heatblock capacitance (precomputed dT/C)
  44. float warn_s; // warning threshold (per sample)
  45. float err_s; // error threshold (per sample)
  46. // simulation functions
  47. void reset(uint8_t heater_pwm, uint8_t fan_pwm, float heater_temp, float ambient_temp);
  48. void step(uint8_t heater_pwm, uint8_t fan_pwm, float heater_temp, float ambient_temp);
  49. };
  50. static bool enabled; // model check enabled
  51. static bool warn_beep = true; // beep on warning threshold
  52. static model_data data; // default heater data
  53. static bool calibrated(); // return calibration/model validity status
  54. static void check(); // check and trigger errors or warnings based on current state
  55. // warning state (updated from from isr context)
  56. volatile static struct
  57. {
  58. float dT_err; // temperature delta error (per sample)
  59. bool warning: 1; // warning condition
  60. bool assert: 1; // warning is still asserted
  61. } warning_state;
  62. static void handle_warning(); // handle warnings from user context
  63. #ifdef TEMP_MODEL_DEBUG
  64. static struct
  65. {
  66. volatile struct
  67. {
  68. uint32_t stamp;
  69. int8_t delta_ms;
  70. uint8_t counter;
  71. uint8_t cur_pwm;
  72. float cur_temp;
  73. float cur_amb;
  74. } entry;
  75. uint8_t serial;
  76. bool enabled;
  77. } log_buf;
  78. static void log_usr(); // user log handler
  79. static void log_isr(); // isr log handler
  80. #endif
  81. } // namespace temp_model
  82. namespace temp_model_cal {
  83. // recording scratch buffer
  84. struct rec_entry
  85. {
  86. float temp; // heater temperature
  87. uint8_t pwm; // heater PWM
  88. };
  89. constexpr uint16_t REC_BUFFER_SIZE = TEMP_MODEL_CAL_S / TEMP_MGR_INTV;
  90. static rec_entry* const rec_buffer = (rec_entry*)block_buffer; // oh-hey, free memory!
  91. static_assert(sizeof(rec_entry[REC_BUFFER_SIZE]) <= sizeof(block_buffer),
  92. "recording length too long to fit within available buffer");
  93. } // namespace temp_model_cal