temperature.h 5.9 KB

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