temperature.h 6.2 KB

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