temperature.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. temperature.h - temperature controller
  3. Part of Marlin
  4. Copyright (c) 2011 Erik van der Zalm
  5. Grbl is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Grbl is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef temperature_h
  17. #define temperature_h
  18. #include "Marlin.h"
  19. #include "config.h"
  20. // public functions
  21. void soft_pwm_init(); //initialize the soft pwm isr
  22. void temp_mgr_init(); //initialize the temperature handler
  23. void manage_heater(); //it is critical that this is called periodically.
  24. bool get_temp_error(); //return true if any thermal error is set
  25. extern bool checkAllHotends(void);
  26. // low level conversion routines
  27. // do not use these routines and variables outside of temperature.cpp
  28. extern int target_temperature[EXTRUDERS];
  29. extern float current_temperature[EXTRUDERS];
  30. #ifdef SHOW_TEMP_ADC_VALUES
  31. extern int current_temperature_raw[EXTRUDERS];
  32. extern int current_temperature_bed_raw;
  33. #endif
  34. extern int target_temperature_bed;
  35. extern float current_temperature_bed;
  36. #ifdef PINDA_THERMISTOR
  37. extern uint16_t current_temperature_raw_pinda;
  38. extern float current_temperature_pinda;
  39. bool has_temperature_compensation();
  40. #endif
  41. #ifdef AMBIENT_THERMISTOR
  42. extern int current_temperature_raw_ambient;
  43. extern float current_temperature_ambient;
  44. #endif
  45. #ifdef VOLT_PWR_PIN
  46. extern int current_voltage_raw_pwr;
  47. #endif
  48. #ifdef VOLT_BED_PIN
  49. extern int current_voltage_raw_bed;
  50. #endif
  51. #ifdef IR_SENSOR_ANALOG
  52. extern uint16_t current_voltage_raw_IR;
  53. #endif //IR_SENSOR_ANALOG
  54. extern bool bedPWMDisabled;
  55. #ifdef PIDTEMP
  56. extern int pid_cycle, pid_number_of_cycles;
  57. extern float _Kp,_Ki,_Kd;
  58. float scalePID_i(float i);
  59. float scalePID_d(float d);
  60. float unscalePID_i(float i);
  61. float unscalePID_d(float d);
  62. bool pidTuningRunning(); // returns true if PID tuning is still running
  63. void preparePidTuning(); // non-blocking call to set "pidTuningRunning" to true immediately
  64. #endif
  65. #ifdef BABYSTEPPING
  66. extern volatile int babystepsTodo[3];
  67. inline void babystepsTodoZadd(int n)
  68. {
  69. if (n != 0) {
  70. CRITICAL_SECTION_START
  71. babystepsTodo[Z_AXIS] += n;
  72. CRITICAL_SECTION_END
  73. }
  74. }
  75. #endif
  76. void resetPID(uint8_t extruder);
  77. //high level conversion routines, for use outside of temperature.cpp
  78. //inline so that there is no performance decrease.
  79. //deg=degreeCelsius
  80. // Doesn't save FLASH when FORCE_INLINE removed.
  81. FORCE_INLINE float degHotend(uint8_t extruder) {
  82. return current_temperature[extruder];
  83. };
  84. #ifdef SHOW_TEMP_ADC_VALUES
  85. FORCE_INLINE float rawHotendTemp(uint8_t extruder) {
  86. return current_temperature_raw[extruder];
  87. };
  88. FORCE_INLINE float rawBedTemp() {
  89. return current_temperature_bed_raw;
  90. };
  91. #endif
  92. FORCE_INLINE float degBed() {
  93. return current_temperature_bed;
  94. };
  95. // Doesn't save FLASH when FORCE_INLINE removed.
  96. FORCE_INLINE float degTargetHotend(uint8_t extruder) {
  97. return target_temperature[extruder];
  98. };
  99. FORCE_INLINE float degTargetBed() {
  100. return target_temperature_bed;
  101. };
  102. // Doesn't save FLASH when FORCE_INLINE removed.
  103. FORCE_INLINE void setTargetHotend(const float &celsius, uint8_t extruder) {
  104. target_temperature[extruder] = celsius;
  105. resetPID(extruder);
  106. };
  107. // Doesn't save FLASH when not inlined.
  108. static inline void setTargetHotendSafe(const float &celsius, uint8_t extruder)
  109. {
  110. if (extruder<EXTRUDERS) {
  111. setTargetHotend(celsius, extruder);
  112. }
  113. }
  114. // Doesn't save FLASH when not inlined.
  115. static inline void setAllTargetHotends(const float &celsius)
  116. {
  117. for(uint8_t i = 0; i < EXTRUDERS; i++) setTargetHotend(celsius, i);
  118. }
  119. FORCE_INLINE void setTargetBed(const float &celsius) {
  120. target_temperature_bed = celsius;
  121. };
  122. FORCE_INLINE bool isHeatingHotend(uint8_t extruder){
  123. return target_temperature[extruder] > current_temperature[extruder];
  124. };
  125. FORCE_INLINE bool isHeatingBed() {
  126. return target_temperature_bed > current_temperature_bed;
  127. };
  128. FORCE_INLINE bool isCoolingHotend(uint8_t extruder) {
  129. return target_temperature[extruder] < current_temperature[extruder];
  130. };
  131. FORCE_INLINE bool isCoolingBed() {
  132. return target_temperature_bed < current_temperature_bed;
  133. };
  134. #define degHotend0() degHotend(0)
  135. #define degTargetHotend0() degTargetHotend(0)
  136. #define setTargetHotend0(_celsius) setTargetHotend((_celsius), 0)
  137. #define isHeatingHotend0() isHeatingHotend(0)
  138. #define isCoolingHotend0() isCoolingHotend(0)
  139. #if EXTRUDERS > 1
  140. #define degHotend1() degHotend(1)
  141. #define degTargetHotend1() degTargetHotend(1)
  142. #define setTargetHotend1(_celsius) setTargetHotend((_celsius), 1)
  143. #define isHeatingHotend1() isHeatingHotend(1)
  144. #define isCoolingHotend1() isCoolingHotend(1)
  145. #else
  146. #define setTargetHotend1(_celsius) do{}while(0)
  147. #endif
  148. #if EXTRUDERS > 2
  149. #define degHotend2() degHotend(2)
  150. #define degTargetHotend2() degTargetHotend(2)
  151. #define setTargetHotend2(_celsius) setTargetHotend((_celsius), 2)
  152. #define isHeatingHotend2() isHeatingHotend(2)
  153. #define isCoolingHotend2() isCoolingHotend(2)
  154. #else
  155. #define setTargetHotend2(_celsius) do{}while(0)
  156. #endif
  157. #if EXTRUDERS > 3
  158. #error Invalid number of extruders
  159. #endif
  160. // return "false", if all heaters are 'off' (ie. "true", if any heater is 'on')
  161. #define CHECK_ALL_HEATERS (checkAllHotends()||(target_temperature_bed!=0))
  162. int getHeaterPower(int heater);
  163. void disable_heater(); // Disable all heaters *instantaneously*
  164. void updatePID();
  165. FORCE_INLINE void autotempShutdown(){
  166. #ifdef AUTOTEMP
  167. if(autotemp_enabled)
  168. {
  169. autotemp_enabled=false;
  170. if(degTargetHotend(active_extruder)>autotemp_min)
  171. setTargetHotend(0,active_extruder);
  172. }
  173. #endif
  174. }
  175. void PID_autotune(float temp, int extruder, int ncycles);
  176. #ifdef TEMP_MODEL
  177. void temp_model_set_enabled(bool enabled);
  178. void temp_model_set_warn_beep(bool enabled);
  179. void temp_model_set_params(float C = NAN, float P = NAN, float Ta_corr = NAN, float warn = NAN, float err = NAN);
  180. void temp_model_set_resistance(uint8_t index, float R);
  181. void temp_model_report_settings();
  182. void temp_model_reset_settings();
  183. void temp_model_load_settings();
  184. void temp_model_save_settings();
  185. void temp_model_autotune(int16_t temp = 0, bool selftest = false);
  186. bool temp_model_autotune_result(); // return true if the last autotune was complete and successful
  187. #ifdef TEMP_MODEL_DEBUG
  188. void temp_model_log_enable(bool enable);
  189. #endif
  190. #endif
  191. #ifdef FAN_SOFT_PWM
  192. extern unsigned char fanSpeedSoftPwm;
  193. #endif
  194. extern uint8_t fanSpeedBckp;
  195. #endif