temperature.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #ifdef SYSTEM_TIMER_2
  24. #define ENABLE_TEMPERATURE_INTERRUPT() TIMSK2 |= (1<<OCIE2B)
  25. #define DISABLE_TEMPERATURE_INTERRUPT() TIMSK2 &= ~(1<<OCIE2B)
  26. #else //SYSTEM_TIMER_2
  27. #define ENABLE_TEMPERATURE_INTERRUPT() TIMSK0 |= (1<<OCIE0B)
  28. #define DISABLE_TEMPERATURE_INTERRUPT() TIMSK0 &= ~(1<<OCIE0B)
  29. #endif //SYSTEM_TIMER_2
  30. // public functions
  31. void tp_init(); //initialize the heating
  32. void manage_heater(); //it is critical that this is called periodically.
  33. // low level conversion routines
  34. // do not use these routines and variables outside of temperature.cpp
  35. extern int target_temperature[EXTRUDERS];
  36. extern float current_temperature[EXTRUDERS];
  37. #ifdef SHOW_TEMP_ADC_VALUES
  38. extern int current_temperature_raw[EXTRUDERS];
  39. extern int current_temperature_bed_raw;
  40. #endif
  41. extern int target_temperature_bed;
  42. extern float current_temperature_bed;
  43. #ifdef PINDA_THERMISTOR
  44. extern uint16_t current_temperature_raw_pinda;
  45. extern float current_temperature_pinda;
  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 TEMP_SENSOR_1_AS_REDUNDANT
  58. extern float redundant_temperature;
  59. #endif
  60. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  61. extern unsigned char soft_pwm_bed;
  62. #endif
  63. #ifdef PIDTEMP
  64. extern int pid_cycle, pid_number_of_cycles;
  65. extern float Kc,_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. #endif
  75. void resetPID(uint8_t extruder);
  76. inline void babystepsTodoZadd(int n)
  77. {
  78. if (n != 0) {
  79. CRITICAL_SECTION_START
  80. babystepsTodo[Z_AXIS] += n;
  81. CRITICAL_SECTION_END
  82. }
  83. }
  84. inline void babystepsTodoZsubtract(int n)
  85. {
  86. if (n != 0) {
  87. CRITICAL_SECTION_START
  88. babystepsTodo[Z_AXIS] -= n;
  89. CRITICAL_SECTION_END
  90. }
  91. }
  92. //high level conversion routines, for use outside of temperature.cpp
  93. //inline so that there is no performance decrease.
  94. //deg=degreeCelsius
  95. FORCE_INLINE float degHotend(uint8_t extruder) {
  96. return current_temperature[extruder];
  97. };
  98. #ifdef SHOW_TEMP_ADC_VALUES
  99. FORCE_INLINE float rawHotendTemp(uint8_t extruder) {
  100. return current_temperature_raw[extruder];
  101. };
  102. FORCE_INLINE float rawBedTemp() {
  103. return current_temperature_bed_raw;
  104. };
  105. #endif
  106. FORCE_INLINE float degBed() {
  107. return current_temperature_bed;
  108. };
  109. FORCE_INLINE float degTargetHotend(uint8_t extruder) {
  110. return target_temperature[extruder];
  111. };
  112. FORCE_INLINE float degTargetBed() {
  113. return target_temperature_bed;
  114. };
  115. FORCE_INLINE void setTargetHotend(const float &celsius, uint8_t extruder) {
  116. target_temperature[extruder] = celsius;
  117. resetPID(extruder);
  118. };
  119. static inline void setTargetHotendSafe(const float &celsius, uint8_t extruder)
  120. {
  121. if (extruder<EXTRUDERS) {
  122. target_temperature[extruder] = celsius;
  123. resetPID(extruder);
  124. }
  125. }
  126. static inline void setAllTargetHotends(const float &celsius)
  127. {
  128. for(int i=0;i<EXTRUDERS;i++) setTargetHotend(celsius,i);
  129. }
  130. FORCE_INLINE void setTargetBed(const float &celsius) {
  131. target_temperature_bed = celsius;
  132. };
  133. FORCE_INLINE bool isHeatingHotend(uint8_t extruder){
  134. return target_temperature[extruder] > current_temperature[extruder];
  135. };
  136. FORCE_INLINE bool isHeatingBed() {
  137. return target_temperature_bed > current_temperature_bed;
  138. };
  139. FORCE_INLINE bool isCoolingHotend(uint8_t extruder) {
  140. return target_temperature[extruder] < current_temperature[extruder];
  141. };
  142. FORCE_INLINE bool isCoolingBed() {
  143. return target_temperature_bed < current_temperature_bed;
  144. };
  145. #define degHotend0() degHotend(0)
  146. #define degTargetHotend0() degTargetHotend(0)
  147. #define setTargetHotend0(_celsius) setTargetHotend((_celsius), 0)
  148. #define isHeatingHotend0() isHeatingHotend(0)
  149. #define isCoolingHotend0() isCoolingHotend(0)
  150. #if EXTRUDERS > 1
  151. #define degHotend1() degHotend(1)
  152. #define degTargetHotend1() degTargetHotend(1)
  153. #define setTargetHotend1(_celsius) setTargetHotend((_celsius), 1)
  154. #define isHeatingHotend1() isHeatingHotend(1)
  155. #define isCoolingHotend1() isCoolingHotend(1)
  156. #else
  157. #define setTargetHotend1(_celsius) do{}while(0)
  158. #endif
  159. #if EXTRUDERS > 2
  160. #define degHotend2() degHotend(2)
  161. #define degTargetHotend2() degTargetHotend(2)
  162. #define setTargetHotend2(_celsius) setTargetHotend((_celsius), 2)
  163. #define isHeatingHotend2() isHeatingHotend(2)
  164. #define isCoolingHotend2() isCoolingHotend(2)
  165. #else
  166. #define setTargetHotend2(_celsius) do{}while(0)
  167. #endif
  168. #if EXTRUDERS > 3
  169. #error Invalid number of extruders
  170. #endif
  171. int getHeaterPower(int heater);
  172. void disable_heater();
  173. void setWatch();
  174. void updatePID();
  175. FORCE_INLINE void autotempShutdown(){
  176. #ifdef AUTOTEMP
  177. if(autotemp_enabled)
  178. {
  179. autotemp_enabled=false;
  180. if(degTargetHotend(active_extruder)>autotemp_min)
  181. setTargetHotend(0,active_extruder);
  182. }
  183. #endif
  184. }
  185. void PID_autotune(float temp, int extruder, int ncycles);
  186. void setExtruderAutoFanState(int pin, bool state);
  187. void checkExtruderAutoFans();
  188. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  189. enum {
  190. EFCE_OK = 0, //!< normal operation, both fans are ok
  191. EFCE_DETECTED, //!< fan error detected, but not reported yet
  192. EFCE_REPORTED //!< fan error detected and reported to LCD and serial
  193. };
  194. extern volatile uint8_t fan_check_error;
  195. void countFanSpeed();
  196. void checkFanSpeed();
  197. void fanSpeedError(unsigned char _fan);
  198. void check_fans();
  199. #endif //(defined(TACH_0))
  200. void check_min_temp();
  201. void check_max_temp();
  202. #endif
  203. extern unsigned long extruder_autofan_last_check;
  204. extern uint8_t fanSpeedBckp;
  205. extern bool fan_measuring;