temp_model.h 3.9 KB

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