temperature.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #ifdef SYSTEM_TIMER_2
  21. #define ENABLE_TEMPERATURE_INTERRUPT() TIMSK2 |= (1<<OCIE2B)
  22. #define DISABLE_TEMPERATURE_INTERRUPT() TIMSK2 &= ~(1<<OCIE2B)
  23. #else //SYSTEM_TIMER_2
  24. #define ENABLE_TEMPERATURE_INTERRUPT() TIMSK0 |= (1<<OCIE0B)
  25. #define DISABLE_TEMPERATURE_INTERRUPT() TIMSK0 &= ~(1<<OCIE0B)
  26. #endif //SYSTEM_TIMER_2
  27. // public functions
  28. void tp_init(); //initialize the heating
  29. void manage_heater(); //it is critical that this is called periodically.
  30. extern bool checkAllHotends(void);
  31. // low level conversion routines
  32. // do not use these routines and variables outside of temperature.cpp
  33. extern int target_temperature[EXTRUDERS];
  34. extern float current_temperature[EXTRUDERS];
  35. #ifdef SHOW_TEMP_ADC_VALUES
  36. extern int current_temperature_raw[EXTRUDERS];
  37. extern int current_temperature_bed_raw;
  38. #endif
  39. extern int target_temperature_bed;
  40. extern float current_temperature_bed;
  41. #ifdef PINDA_THERMISTOR
  42. extern uint16_t current_temperature_raw_pinda;
  43. extern float current_temperature_pinda;
  44. bool has_temperature_compensation();
  45. #endif
  46. #ifdef AMBIENT_THERMISTOR
  47. extern int current_temperature_raw_ambient;
  48. extern float current_temperature_ambient;
  49. #endif
  50. #ifdef VOLT_PWR_PIN
  51. extern int current_voltage_raw_pwr;
  52. #endif
  53. #ifdef VOLT_BED_PIN
  54. extern int current_voltage_raw_bed;
  55. #endif
  56. #ifdef IR_SENSOR_ANALOG
  57. extern uint16_t current_voltage_raw_IR;
  58. #endif //IR_SENSOR_ANALOG
  59. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  60. extern unsigned char soft_pwm_bed;
  61. #endif
  62. extern bool bedPWMDisabled;
  63. #ifdef PIDTEMP
  64. extern int pid_cycle, pid_number_of_cycles;
  65. extern float _Kp,_Ki,_Kd;
  66. extern bool pid_tuning_finished;
  67. float scalePID_i(float i);
  68. float scalePID_d(float d);
  69. float unscalePID_i(float i);
  70. float unscalePID_d(float d);
  71. #endif
  72. #ifdef BABYSTEPPING
  73. extern volatile int babystepsTodo[3];
  74. inline void babystepsTodoZadd(int n)
  75. {
  76. if (n != 0) {
  77. CRITICAL_SECTION_START
  78. babystepsTodo[Z_AXIS] += n;
  79. CRITICAL_SECTION_END
  80. }
  81. }
  82. #endif
  83. void resetPID(uint8_t extruder);
  84. //high level conversion routines, for use outside of temperature.cpp
  85. //inline so that there is no performance decrease.
  86. //deg=degreeCelsius
  87. // Doesn't save FLASH when FORCE_INLINE removed.
  88. FORCE_INLINE float degHotend(uint8_t extruder) {
  89. return current_temperature[extruder];
  90. };
  91. #ifdef SHOW_TEMP_ADC_VALUES
  92. FORCE_INLINE float rawHotendTemp(uint8_t extruder) {
  93. return current_temperature_raw[extruder];
  94. };
  95. FORCE_INLINE float rawBedTemp() {
  96. return current_temperature_bed_raw;
  97. };
  98. #endif
  99. FORCE_INLINE float degBed() {
  100. return current_temperature_bed;
  101. };
  102. // Doesn't save FLASH when FORCE_INLINE removed.
  103. FORCE_INLINE float degTargetHotend(uint8_t extruder) {
  104. return target_temperature[extruder];
  105. };
  106. FORCE_INLINE float degTargetBed() {
  107. return target_temperature_bed;
  108. };
  109. // Doesn't save FLASH when FORCE_INLINE removed.
  110. FORCE_INLINE void setTargetHotend(const float &celsius, uint8_t extruder) {
  111. target_temperature[extruder] = celsius;
  112. resetPID(extruder);
  113. };
  114. // Doesn't save FLASH when not inlined.
  115. static inline void setTargetHotendSafe(const float &celsius, uint8_t extruder)
  116. {
  117. if (extruder<EXTRUDERS) {
  118. target_temperature[extruder] = celsius;
  119. resetPID(extruder);
  120. }
  121. }
  122. // Doesn't save FLASH when not inlined.
  123. static inline void setAllTargetHotends(const float &celsius)
  124. {
  125. for(uint8_t i = 0; i < EXTRUDERS; i++) setTargetHotend(celsius, i);
  126. }
  127. FORCE_INLINE void setTargetBed(const float &celsius) {
  128. target_temperature_bed = celsius;
  129. };
  130. FORCE_INLINE bool isHeatingHotend(uint8_t extruder){
  131. return target_temperature[extruder] > current_temperature[extruder];
  132. };
  133. FORCE_INLINE bool isHeatingBed() {
  134. return target_temperature_bed > current_temperature_bed;
  135. };
  136. FORCE_INLINE bool isCoolingHotend(uint8_t extruder) {
  137. return target_temperature[extruder] < current_temperature[extruder];
  138. };
  139. FORCE_INLINE bool isCoolingBed() {
  140. return target_temperature_bed < current_temperature_bed;
  141. };
  142. #define degHotend0() degHotend(0)
  143. #define degTargetHotend0() degTargetHotend(0)
  144. #define setTargetHotend0(_celsius) setTargetHotend((_celsius), 0)
  145. #define isHeatingHotend0() isHeatingHotend(0)
  146. #define isCoolingHotend0() isCoolingHotend(0)
  147. #if EXTRUDERS > 1
  148. #define degHotend1() degHotend(1)
  149. #define degTargetHotend1() degTargetHotend(1)
  150. #define setTargetHotend1(_celsius) setTargetHotend((_celsius), 1)
  151. #define isHeatingHotend1() isHeatingHotend(1)
  152. #define isCoolingHotend1() isCoolingHotend(1)
  153. #else
  154. #define setTargetHotend1(_celsius) do{}while(0)
  155. #endif
  156. #if EXTRUDERS > 2
  157. #define degHotend2() degHotend(2)
  158. #define degTargetHotend2() degTargetHotend(2)
  159. #define setTargetHotend2(_celsius) setTargetHotend((_celsius), 2)
  160. #define isHeatingHotend2() isHeatingHotend(2)
  161. #define isCoolingHotend2() isCoolingHotend(2)
  162. #else
  163. #define setTargetHotend2(_celsius) do{}while(0)
  164. #endif
  165. #if EXTRUDERS > 3
  166. #error Invalid number of extruders
  167. #endif
  168. // return "false", if all heaters are 'off' (ie. "true", if any heater is 'on')
  169. #define CHECK_ALL_HEATERS (checkAllHotends()||(target_temperature_bed!=0))
  170. int getHeaterPower(int heater);
  171. void disable_heater(); // Disable all heaters
  172. void updatePID();
  173. FORCE_INLINE void autotempShutdown(){
  174. #ifdef AUTOTEMP
  175. if(autotemp_enabled)
  176. {
  177. autotemp_enabled=false;
  178. if(degTargetHotend(active_extruder)>autotemp_min)
  179. setTargetHotend(0,active_extruder);
  180. }
  181. #endif
  182. }
  183. void PID_autotune(float temp, int extruder, int ncycles);
  184. void check_min_temp();
  185. void check_max_temp();
  186. #ifdef FAN_SOFT_PWM
  187. extern unsigned char fanSpeedSoftPwm;
  188. #endif
  189. extern uint8_t fanSpeedBckp;
  190. #endif