temperature.h 7.2 KB

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