temp_model.h 3.7 KB

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