temperature.h 5.9 KB

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