My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. /*
  2. temperature.cpp - temperature control
  3. Part of Marlin
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program 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. This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Marlin.h"
  17. #include "ultralcd.h"
  18. #include "temperature.h"
  19. #include "watchdog.h"
  20. #include "language.h"
  21. #include "Sd2PinMap.h"
  22. //===========================================================================
  23. //================================== macros =================================
  24. //===========================================================================
  25. #ifdef K1 // Defined in Configuration.h in the PID settings
  26. #define K2 (1.0-K1)
  27. #endif
  28. #if defined(PIDTEMPBED) || defined(PIDTEMP)
  29. #define PID_dT ((OVERSAMPLENR * 12.0)/(F_CPU / 64.0 / 256.0))
  30. #endif
  31. //===========================================================================
  32. //============================= public variables ============================
  33. //===========================================================================
  34. int target_temperature[4] = { 0 };
  35. int target_temperature_bed = 0;
  36. int current_temperature_raw[4] = { 0 };
  37. float current_temperature[4] = { 0.0 };
  38. int current_temperature_bed_raw = 0;
  39. float current_temperature_bed = 0.0;
  40. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  41. int redundant_temperature_raw = 0;
  42. float redundant_temperature = 0.0;
  43. #endif
  44. #ifdef PIDTEMPBED
  45. float bedKp=DEFAULT_bedKp;
  46. float bedKi=(DEFAULT_bedKi*PID_dT);
  47. float bedKd=(DEFAULT_bedKd/PID_dT);
  48. #endif //PIDTEMPBED
  49. #ifdef FAN_SOFT_PWM
  50. unsigned char fanSpeedSoftPwm;
  51. #endif
  52. unsigned char soft_pwm_bed;
  53. #ifdef BABYSTEPPING
  54. volatile int babystepsTodo[3] = { 0 };
  55. #endif
  56. #ifdef FILAMENT_SENSOR
  57. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  58. #endif
  59. #define HAS_HEATER_THERMAL_PROTECTION (defined(THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0)
  60. #define HAS_BED_THERMAL_PROTECTION (defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0 && TEMP_SENSOR_BED != 0)
  61. #if HAS_HEATER_THERMAL_PROTECTION || HAS_BED_THERMAL_PROTECTION
  62. enum TRState { TRReset, TRInactive, TRFirstHeating, TRStable, TRRunaway };
  63. void thermal_runaway_protection(TRState *state, millis_t *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
  64. #if HAS_HEATER_THERMAL_PROTECTION
  65. static TRState thermal_runaway_state_machine[4] = { TRReset, TRReset, TRReset, TRReset };
  66. static millis_t thermal_runaway_timer[4]; // = {0,0,0,0};
  67. #endif
  68. #if HAS_BED_THERMAL_PROTECTION
  69. static TRState thermal_runaway_bed_state_machine = TRReset;
  70. static millis_t thermal_runaway_bed_timer;
  71. #endif
  72. #endif
  73. //===========================================================================
  74. //============================ private variables ============================
  75. //===========================================================================
  76. static volatile bool temp_meas_ready = false;
  77. #ifdef PIDTEMP
  78. //static cannot be external:
  79. static float temp_iState[EXTRUDERS] = { 0 };
  80. static float temp_dState[EXTRUDERS] = { 0 };
  81. static float pTerm[EXTRUDERS];
  82. static float iTerm[EXTRUDERS];
  83. static float dTerm[EXTRUDERS];
  84. //int output;
  85. static float pid_error[EXTRUDERS];
  86. static float temp_iState_min[EXTRUDERS];
  87. static float temp_iState_max[EXTRUDERS];
  88. static bool pid_reset[EXTRUDERS];
  89. #endif //PIDTEMP
  90. #ifdef PIDTEMPBED
  91. //static cannot be external:
  92. static float temp_iState_bed = { 0 };
  93. static float temp_dState_bed = { 0 };
  94. static float pTerm_bed;
  95. static float iTerm_bed;
  96. static float dTerm_bed;
  97. //int output;
  98. static float pid_error_bed;
  99. static float temp_iState_min_bed;
  100. static float temp_iState_max_bed;
  101. #else //PIDTEMPBED
  102. static millis_t next_bed_check_ms;
  103. #endif //PIDTEMPBED
  104. static unsigned char soft_pwm[EXTRUDERS];
  105. #ifdef FAN_SOFT_PWM
  106. static unsigned char soft_pwm_fan;
  107. #endif
  108. #if HAS_AUTO_FAN
  109. static millis_t next_auto_fan_check_ms;
  110. #endif
  111. #ifdef PIDTEMP
  112. #ifdef PID_PARAMS_PER_EXTRUDER
  113. float Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kp, DEFAULT_Kp, DEFAULT_Kp, DEFAULT_Kp);
  114. float Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT);
  115. float Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT);
  116. #ifdef PID_ADD_EXTRUSION_RATE
  117. float Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kc, DEFAULT_Kc, DEFAULT_Kc, DEFAULT_Kc);
  118. #endif // PID_ADD_EXTRUSION_RATE
  119. #else //PID_PARAMS_PER_EXTRUDER
  120. float Kp = DEFAULT_Kp;
  121. float Ki = DEFAULT_Ki * PID_dT;
  122. float Kd = DEFAULT_Kd / PID_dT;
  123. #ifdef PID_ADD_EXTRUSION_RATE
  124. float Kc = DEFAULT_Kc;
  125. #endif // PID_ADD_EXTRUSION_RATE
  126. #endif // PID_PARAMS_PER_EXTRUDER
  127. #endif //PIDTEMP
  128. // Init min and max temp with extreme values to prevent false errors during startup
  129. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  130. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  131. static int minttemp[EXTRUDERS] = { 0 };
  132. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383, 16383 );
  133. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  134. #ifdef BED_MAXTEMP
  135. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  136. #endif
  137. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  138. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  139. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  140. #else
  141. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE, (void *)HEATER_3_TEMPTABLE );
  142. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN );
  143. #endif
  144. static float analog2temp(int raw, uint8_t e);
  145. static float analog2tempBed(int raw);
  146. static void updateTemperaturesFromRawValues();
  147. #ifdef WATCH_TEMP_PERIOD
  148. int watch_start_temp[EXTRUDERS] = { 0 };
  149. millis_t watchmillis[EXTRUDERS] = { 0 };
  150. #endif //WATCH_TEMP_PERIOD
  151. #ifndef SOFT_PWM_SCALE
  152. #define SOFT_PWM_SCALE 0
  153. #endif
  154. #ifdef FILAMENT_SENSOR
  155. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  156. #endif
  157. #ifdef HEATER_0_USES_MAX6675
  158. static int read_max6675();
  159. #endif
  160. //===========================================================================
  161. //================================ functions ================================
  162. //===========================================================================
  163. void PID_autotune(float temp, int extruder, int ncycles)
  164. {
  165. float input = 0.0;
  166. int cycles = 0;
  167. bool heating = true;
  168. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  169. long t_high = 0, t_low = 0;
  170. long bias, d;
  171. float Ku, Tu;
  172. float Kp, Ki, Kd;
  173. float max = 0, min = 10000;
  174. #if HAS_AUTO_FAN
  175. millis_t next_auto_fan_check_ms = temp_ms + 2500;
  176. #endif
  177. if (extruder >= EXTRUDERS
  178. #if !HAS_TEMP_BED
  179. || extruder < 0
  180. #endif
  181. ) {
  182. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  183. return;
  184. }
  185. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  186. disable_all_heaters(); // switch off all heaters.
  187. if (extruder < 0)
  188. soft_pwm_bed = bias = d = MAX_BED_POWER / 2;
  189. else
  190. soft_pwm[extruder] = bias = d = PID_MAX / 2;
  191. // PID Tuning loop
  192. for (;;) {
  193. millis_t ms = millis();
  194. if (temp_meas_ready) { // temp sample ready
  195. updateTemperaturesFromRawValues();
  196. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  197. max = max(max, input);
  198. min = min(min, input);
  199. #if HAS_AUTO_FAN
  200. if (ms > next_auto_fan_check_ms) {
  201. checkExtruderAutoFans();
  202. next_auto_fan_check_ms = ms + 2500;
  203. }
  204. #endif
  205. if (heating == true && input > temp) {
  206. if (ms - t2 > 5000) {
  207. heating = false;
  208. if (extruder < 0)
  209. soft_pwm_bed = (bias - d) >> 1;
  210. else
  211. soft_pwm[extruder] = (bias - d) >> 1;
  212. t1 = ms;
  213. t_high = t1 - t2;
  214. max = temp;
  215. }
  216. }
  217. if (heating == false && input < temp) {
  218. if (ms - t1 > 5000) {
  219. heating = true;
  220. t2 = ms;
  221. t_low = t2 - t1;
  222. if (cycles > 0) {
  223. long max_pow = extruder < 0 ? MAX_BED_POWER : PID_MAX;
  224. bias += (d*(t_high - t_low))/(t_low + t_high);
  225. bias = constrain(bias, 20, max_pow - 20);
  226. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  227. SERIAL_PROTOCOLPGM(MSG_BIAS); SERIAL_PROTOCOL(bias);
  228. SERIAL_PROTOCOLPGM(MSG_D); SERIAL_PROTOCOL(d);
  229. SERIAL_PROTOCOLPGM(MSG_T_MIN); SERIAL_PROTOCOL(min);
  230. SERIAL_PROTOCOLPGM(MSG_T_MAX); SERIAL_PROTOCOLLN(max);
  231. if (cycles > 2) {
  232. Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
  233. Tu = ((float)(t_low + t_high) / 1000.0);
  234. SERIAL_PROTOCOLPGM(MSG_KU); SERIAL_PROTOCOL(Ku);
  235. SERIAL_PROTOCOLPGM(MSG_TU); SERIAL_PROTOCOLLN(Tu);
  236. Kp = 0.6 * Ku;
  237. Ki = 2 * Kp / Tu;
  238. Kd = Kp * Tu / 8;
  239. SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
  240. SERIAL_PROTOCOLPGM(MSG_KP); SERIAL_PROTOCOLLN(Kp);
  241. SERIAL_PROTOCOLPGM(MSG_KI); SERIAL_PROTOCOLLN(Ki);
  242. SERIAL_PROTOCOLPGM(MSG_KD); SERIAL_PROTOCOLLN(Kd);
  243. /*
  244. Kp = 0.33*Ku;
  245. Ki = Kp/Tu;
  246. Kd = Kp*Tu/3;
  247. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  248. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  249. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  250. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  251. Kp = 0.2*Ku;
  252. Ki = 2*Kp/Tu;
  253. Kd = Kp*Tu/3;
  254. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  255. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  256. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  257. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  258. */
  259. }
  260. }
  261. if (extruder < 0)
  262. soft_pwm_bed = (bias + d) >> 1;
  263. else
  264. soft_pwm[extruder] = (bias + d) >> 1;
  265. cycles++;
  266. min = temp;
  267. }
  268. }
  269. }
  270. if (input > temp + 20) {
  271. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  272. return;
  273. }
  274. // Every 2 seconds...
  275. if (ms > temp_ms + 2000) {
  276. int p;
  277. if (extruder < 0) {
  278. p = soft_pwm_bed;
  279. SERIAL_PROTOCOLPGM(MSG_OK_B);
  280. }
  281. else {
  282. p = soft_pwm[extruder];
  283. SERIAL_PROTOCOLPGM(MSG_OK_T);
  284. }
  285. SERIAL_PROTOCOL(input);
  286. SERIAL_PROTOCOLPGM(MSG_AT);
  287. SERIAL_PROTOCOLLN(p);
  288. temp_ms = ms;
  289. } // every 2 seconds
  290. // Over 2 minutes?
  291. if (((ms - t1) + (ms - t2)) > (10L*60L*1000L*2L)) {
  292. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  293. return;
  294. }
  295. if (cycles > ncycles) {
  296. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  297. SERIAL_PROTOCOLPGM("#define DEFAULT_Kp "); SERIAL_PROTOCOLLN(Kp);
  298. SERIAL_PROTOCOLPGM("#define DEFAULT_Ki "); SERIAL_PROTOCOLLN(Ki);
  299. SERIAL_PROTOCOLPGM("#define DEFAULT_Kd "); SERIAL_PROTOCOLLN(Kd);
  300. return;
  301. }
  302. lcd_update();
  303. }
  304. }
  305. void updatePID() {
  306. #ifdef PIDTEMP
  307. for (int e = 0; e < EXTRUDERS; e++) {
  308. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  309. }
  310. #endif
  311. #ifdef PIDTEMPBED
  312. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  313. #endif
  314. }
  315. int getHeaterPower(int heater) {
  316. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  317. }
  318. #if HAS_AUTO_FAN
  319. void setExtruderAutoFanState(int pin, bool state)
  320. {
  321. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  322. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  323. pinMode(pin, OUTPUT);
  324. digitalWrite(pin, newFanSpeed);
  325. analogWrite(pin, newFanSpeed);
  326. }
  327. void checkExtruderAutoFans()
  328. {
  329. uint8_t fanState = 0;
  330. // which fan pins need to be turned on?
  331. #if HAS_AUTO_FAN_0
  332. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  333. fanState |= 1;
  334. #endif
  335. #if HAS_AUTO_FAN_1
  336. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  337. {
  338. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  339. fanState |= 1;
  340. else
  341. fanState |= 2;
  342. }
  343. #endif
  344. #if HAS_AUTO_FAN_2
  345. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  346. {
  347. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  348. fanState |= 1;
  349. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  350. fanState |= 2;
  351. else
  352. fanState |= 4;
  353. }
  354. #endif
  355. #if HAS_AUTO_FAN_3
  356. if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  357. {
  358. if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  359. fanState |= 1;
  360. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  361. fanState |= 2;
  362. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN)
  363. fanState |= 4;
  364. else
  365. fanState |= 8;
  366. }
  367. #endif
  368. // update extruder auto fan states
  369. #if HAS_AUTO_FAN_0
  370. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  371. #endif
  372. #if HAS_AUTO_FAN_1
  373. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  374. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  375. #endif
  376. #if HAS_AUTO_FAN_2
  377. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  378. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  379. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  380. #endif
  381. #if HAS_AUTO_FAN_3
  382. if (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  383. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN
  384. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  385. setExtruderAutoFanState(EXTRUDER_3_AUTO_FAN_PIN, (fanState & 8) != 0);
  386. #endif
  387. }
  388. #endif // any extruder auto fan pins set
  389. //
  390. // Temperature Error Handlers
  391. //
  392. inline void _temp_error(int e, const char *msg1, const char *msg2) {
  393. if (IsRunning()) {
  394. SERIAL_ERROR_START;
  395. if (e >= 0) SERIAL_ERRORLN((int)e);
  396. serialprintPGM(msg1);
  397. MYSERIAL.write('\n');
  398. #ifdef ULTRA_LCD
  399. lcd_setalertstatuspgm(msg2);
  400. #endif
  401. }
  402. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  403. Stop();
  404. #endif
  405. }
  406. void max_temp_error(uint8_t e) {
  407. disable_all_heaters();
  408. _temp_error(e, PSTR(MSG_MAXTEMP_EXTRUDER_OFF), PSTR(MSG_ERR_MAXTEMP));
  409. }
  410. void min_temp_error(uint8_t e) {
  411. disable_all_heaters();
  412. _temp_error(e, PSTR(MSG_MINTEMP_EXTRUDER_OFF), PSTR(MSG_ERR_MINTEMP));
  413. }
  414. void bed_max_temp_error(void) {
  415. #if HAS_HEATER_BED
  416. WRITE_HEATER_BED(0);
  417. #endif
  418. _temp_error(-1, PSTR(MSG_MAXTEMP_BED_OFF), PSTR(MSG_ERR_MAXTEMP_BED));
  419. }
  420. float get_pid_output(int e) {
  421. float pid_output;
  422. #ifdef PIDTEMP
  423. #ifndef PID_OPENLOOP
  424. pid_error[e] = target_temperature[e] - current_temperature[e];
  425. if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
  426. pid_output = BANG_MAX;
  427. pid_reset[e] = true;
  428. }
  429. else if (pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  430. pid_output = 0;
  431. pid_reset[e] = true;
  432. }
  433. else {
  434. if (pid_reset[e]) {
  435. temp_iState[e] = 0.0;
  436. pid_reset[e] = false;
  437. }
  438. pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
  439. temp_iState[e] += pid_error[e];
  440. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  441. iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
  442. dTerm[e] = K2 * PID_PARAM(Kd,e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
  443. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  444. if (pid_output > PID_MAX) {
  445. if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  446. pid_output = PID_MAX;
  447. }
  448. else if (pid_output < 0) {
  449. if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  450. pid_output = 0;
  451. }
  452. }
  453. temp_dState[e] = current_temperature[e];
  454. #else
  455. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  456. #endif //PID_OPENLOOP
  457. #ifdef PID_DEBUG
  458. SERIAL_ECHO_START;
  459. SERIAL_ECHO(MSG_PID_DEBUG);
  460. SERIAL_ECHO(e);
  461. SERIAL_ECHO(MSG_PID_DEBUG_INPUT);
  462. SERIAL_ECHO(current_temperature[e]);
  463. SERIAL_ECHO(MSG_PID_DEBUG_OUTPUT);
  464. SERIAL_ECHO(pid_output);
  465. SERIAL_ECHO(MSG_PID_DEBUG_PTERM);
  466. SERIAL_ECHO(pTerm[e]);
  467. SERIAL_ECHO(MSG_PID_DEBUG_ITERM);
  468. SERIAL_ECHO(iTerm[e]);
  469. SERIAL_ECHO(MSG_PID_DEBUG_DTERM);
  470. SERIAL_ECHOLN(dTerm[e]);
  471. #endif //PID_DEBUG
  472. #else /* PID off */
  473. pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
  474. #endif
  475. return pid_output;
  476. }
  477. #ifdef PIDTEMPBED
  478. float get_pid_output_bed() {
  479. float pid_output;
  480. #ifndef PID_OPENLOOP
  481. pid_error_bed = target_temperature_bed - current_temperature_bed;
  482. pTerm_bed = bedKp * pid_error_bed;
  483. temp_iState_bed += pid_error_bed;
  484. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  485. iTerm_bed = bedKi * temp_iState_bed;
  486. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  487. temp_dState_bed = current_temperature_bed;
  488. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  489. if (pid_output > MAX_BED_POWER) {
  490. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  491. pid_output = MAX_BED_POWER;
  492. }
  493. else if (pid_output < 0) {
  494. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  495. pid_output = 0;
  496. }
  497. #else
  498. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  499. #endif // PID_OPENLOOP
  500. #ifdef PID_BED_DEBUG
  501. SERIAL_ECHO_START;
  502. SERIAL_ECHO(" PID_BED_DEBUG ");
  503. SERIAL_ECHO(": Input ");
  504. SERIAL_ECHO(current_temperature_bed);
  505. SERIAL_ECHO(" Output ");
  506. SERIAL_ECHO(pid_output);
  507. SERIAL_ECHO(" pTerm ");
  508. SERIAL_ECHO(pTerm_bed);
  509. SERIAL_ECHO(" iTerm ");
  510. SERIAL_ECHO(iTerm_bed);
  511. SERIAL_ECHO(" dTerm ");
  512. SERIAL_ECHOLN(dTerm_bed);
  513. #endif //PID_BED_DEBUG
  514. return pid_output;
  515. }
  516. #endif
  517. /**
  518. * Manage heating activities for extruder hot-ends and a heated bed
  519. * - Acquire updated temperature readings
  520. * - Invoke thermal runaway protection
  521. * - Manage extruder auto-fan
  522. * - Apply filament width to the extrusion rate (may move)
  523. * - Update the heated bed PID output value
  524. */
  525. void manage_heater() {
  526. if (!temp_meas_ready) return;
  527. updateTemperaturesFromRawValues();
  528. #ifdef HEATER_0_USES_MAX6675
  529. float ct = current_temperature[0];
  530. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  531. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  532. #endif //HEATER_0_USES_MAX6675
  533. #if defined(WATCH_TEMP_PERIOD) || !defined(PIDTEMPBED) || HAS_AUTO_FAN
  534. millis_t ms = millis();
  535. #endif
  536. // Loop through all extruders
  537. for (int e = 0; e < EXTRUDERS; e++) {
  538. #if HAS_HEATER_THERMAL_PROTECTION
  539. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_RUNAWAY_PROTECTION_PERIOD, THERMAL_RUNAWAY_PROTECTION_HYSTERESIS);
  540. #endif
  541. float pid_output = get_pid_output(e);
  542. // Check if temperature is within the correct range
  543. soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  544. #ifdef WATCH_TEMP_PERIOD
  545. if (watchmillis[e] && ms > watchmillis[e] + WATCH_TEMP_PERIOD) {
  546. if (degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE) {
  547. setTargetHotend(0, e);
  548. LCD_MESSAGEPGM(MSG_HEATING_FAILED_LCD); // translatable
  549. SERIAL_ECHO_START;
  550. SERIAL_ECHOLNPGM(MSG_HEATING_FAILED);
  551. }
  552. else {
  553. watchmillis[e] = 0;
  554. }
  555. }
  556. #endif //WATCH_TEMP_PERIOD
  557. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  558. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  559. disable_all_heaters();
  560. _temp_error(0, PSTR(MSG_EXTRUDER_SWITCHED_OFF), PSTR(MSG_ERR_REDUNDANT_TEMP));
  561. }
  562. #endif // TEMP_SENSOR_1_AS_REDUNDANT
  563. } // Extruders Loop
  564. #if HAS_AUTO_FAN
  565. if (ms > next_auto_fan_check_ms) { // only need to check fan state very infrequently
  566. checkExtruderAutoFans();
  567. next_auto_fan_check_ms = ms + 2500;
  568. }
  569. #endif
  570. // Control the extruder rate based on the width sensor
  571. #ifdef FILAMENT_SENSOR
  572. if (filament_sensor) {
  573. meas_shift_index = delay_index1 - meas_delay_cm;
  574. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  575. // Get the delayed info and add 100 to reconstitute to a percent of
  576. // the nominal filament diameter then square it to get an area
  577. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  578. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  579. if (vm < 0.01) vm = 0.01;
  580. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  581. }
  582. #endif //FILAMENT_SENSOR
  583. #ifndef PIDTEMPBED
  584. if (ms < next_bed_check_ms) return;
  585. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  586. #endif
  587. #if TEMP_SENSOR_BED != 0
  588. #if HAS_BED_THERMAL_PROTECTION
  589. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, -1, THERMAL_RUNAWAY_PROTECTION_BED_PERIOD, THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS);
  590. #endif
  591. #ifdef PIDTEMPBED
  592. float pid_output = get_pid_output_bed();
  593. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  594. #elif defined(BED_LIMIT_SWITCHING)
  595. // Check if temperature is within the correct band
  596. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  597. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  598. soft_pwm_bed = 0;
  599. else if (current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  600. soft_pwm_bed = MAX_BED_POWER >> 1;
  601. }
  602. else {
  603. soft_pwm_bed = 0;
  604. WRITE_HEATER_BED(LOW);
  605. }
  606. #else // BED_LIMIT_SWITCHING
  607. // Check if temperature is within the correct range
  608. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  609. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  610. }
  611. else {
  612. soft_pwm_bed = 0;
  613. WRITE_HEATER_BED(LOW);
  614. }
  615. #endif
  616. #endif //TEMP_SENSOR_BED != 0
  617. }
  618. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  619. // Derived from RepRap FiveD extruder::getTemperature()
  620. // For hot end temperature measurement.
  621. static float analog2temp(int raw, uint8_t e) {
  622. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  623. if (e > EXTRUDERS)
  624. #else
  625. if (e >= EXTRUDERS)
  626. #endif
  627. {
  628. SERIAL_ERROR_START;
  629. SERIAL_ERROR((int)e);
  630. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  631. kill();
  632. return 0.0;
  633. }
  634. #ifdef HEATER_0_USES_MAX6675
  635. if (e == 0) return 0.25 * raw;
  636. #endif
  637. if (heater_ttbl_map[e] != NULL) {
  638. float celsius = 0;
  639. uint8_t i;
  640. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  641. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  642. if (PGM_RD_W((*tt)[i][0]) > raw) {
  643. celsius = PGM_RD_W((*tt)[i-1][1]) +
  644. (raw - PGM_RD_W((*tt)[i-1][0])) *
  645. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  646. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  647. break;
  648. }
  649. }
  650. // Overflow: Set to last value in the table
  651. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  652. return celsius;
  653. }
  654. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  655. }
  656. // Derived from RepRap FiveD extruder::getTemperature()
  657. // For bed temperature measurement.
  658. static float analog2tempBed(int raw) {
  659. #ifdef BED_USES_THERMISTOR
  660. float celsius = 0;
  661. byte i;
  662. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  663. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  664. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  665. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  666. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  667. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  668. break;
  669. }
  670. }
  671. // Overflow: Set to last value in the table
  672. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  673. return celsius;
  674. #elif defined BED_USES_AD595
  675. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  676. #else
  677. return 0;
  678. #endif
  679. }
  680. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  681. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  682. static void updateTemperaturesFromRawValues() {
  683. #ifdef HEATER_0_USES_MAX6675
  684. current_temperature_raw[0] = read_max6675();
  685. #endif
  686. for(uint8_t e = 0; e < EXTRUDERS; e++) {
  687. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  688. }
  689. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  690. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  691. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  692. #endif
  693. #if HAS_FILAMENT_SENSOR
  694. filament_width_meas = analog2widthFil();
  695. #endif
  696. //Reset the watchdog after we know we have a temperature measurement.
  697. watchdog_reset();
  698. CRITICAL_SECTION_START;
  699. temp_meas_ready = false;
  700. CRITICAL_SECTION_END;
  701. }
  702. #ifdef FILAMENT_SENSOR
  703. // Convert raw Filament Width to millimeters
  704. float analog2widthFil() {
  705. return current_raw_filwidth / 16383.0 * 5.0;
  706. //return current_raw_filwidth;
  707. }
  708. // Convert raw Filament Width to a ratio
  709. int widthFil_to_size_ratio() {
  710. float temp = filament_width_meas;
  711. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  712. else if (temp > MEASURED_UPPER_LIMIT) temp = MEASURED_UPPER_LIMIT;
  713. return filament_width_nominal / temp * 100;
  714. }
  715. #endif
  716. /**
  717. * Initialize the temperature manager
  718. * The manager is implemented by periodic calls to manage_heater()
  719. */
  720. void tp_init() {
  721. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  722. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  723. MCUCR=BIT(JTD);
  724. MCUCR=BIT(JTD);
  725. #endif
  726. // Finish init of mult extruder arrays
  727. for (int e = 0; e < EXTRUDERS; e++) {
  728. // populate with the first value
  729. maxttemp[e] = maxttemp[0];
  730. #ifdef PIDTEMP
  731. temp_iState_min[e] = 0.0;
  732. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  733. #endif //PIDTEMP
  734. #ifdef PIDTEMPBED
  735. temp_iState_min_bed = 0.0;
  736. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  737. #endif //PIDTEMPBED
  738. }
  739. #if HAS_HEATER_0
  740. SET_OUTPUT(HEATER_0_PIN);
  741. #endif
  742. #if HAS_HEATER_1
  743. SET_OUTPUT(HEATER_1_PIN);
  744. #endif
  745. #if HAS_HEATER_2
  746. SET_OUTPUT(HEATER_2_PIN);
  747. #endif
  748. #if HAS_HEATER_3
  749. SET_OUTPUT(HEATER_3_PIN);
  750. #endif
  751. #if HAS_HEATER_BED
  752. SET_OUTPUT(HEATER_BED_PIN);
  753. #endif
  754. #if HAS_FAN
  755. SET_OUTPUT(FAN_PIN);
  756. #ifdef FAST_PWM_FAN
  757. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  758. #endif
  759. #ifdef FAN_SOFT_PWM
  760. soft_pwm_fan = fanSpeedSoftPwm / 2;
  761. #endif
  762. #endif
  763. #ifdef HEATER_0_USES_MAX6675
  764. #ifndef SDSUPPORT
  765. OUT_WRITE(SCK_PIN, LOW);
  766. OUT_WRITE(MOSI_PIN, HIGH);
  767. OUT_WRITE(MISO_PIN, HIGH);
  768. #else
  769. pinMode(SS_PIN, OUTPUT);
  770. digitalWrite(SS_PIN, HIGH);
  771. #endif
  772. OUT_WRITE(MAX6675_SS,HIGH);
  773. #endif //HEATER_0_USES_MAX6675
  774. #ifdef DIDR2
  775. #define ANALOG_SELECT(pin) do{ if (pin < 8) DIDR0 |= BIT(pin); else DIDR2 |= BIT(pin - 8); }while(0)
  776. #else
  777. #define ANALOG_SELECT(pin) do{ DIDR0 |= BIT(pin); }while(0)
  778. #endif
  779. // Set analog inputs
  780. ADCSRA = BIT(ADEN) | BIT(ADSC) | BIT(ADIF) | 0x07;
  781. DIDR0 = 0;
  782. #ifdef DIDR2
  783. DIDR2 = 0;
  784. #endif
  785. #if HAS_TEMP_0
  786. ANALOG_SELECT(TEMP_0_PIN);
  787. #endif
  788. #if HAS_TEMP_1
  789. ANALOG_SELECT(TEMP_1_PIN);
  790. #endif
  791. #if HAS_TEMP_2
  792. ANALOG_SELECT(TEMP_2_PIN);
  793. #endif
  794. #if HAS_TEMP_3
  795. ANALOG_SELECT(TEMP_3_PIN);
  796. #endif
  797. #if HAS_TEMP_BED
  798. ANALOG_SELECT(TEMP_BED_PIN);
  799. #endif
  800. #if HAS_FILAMENT_SENSOR
  801. ANALOG_SELECT(FILWIDTH_PIN);
  802. #endif
  803. // Use timer0 for temperature measurement
  804. // Interleave temperature interrupt with millies interrupt
  805. OCR0B = 128;
  806. TIMSK0 |= BIT(OCIE0B);
  807. // Wait for temperature measurement to settle
  808. delay(250);
  809. #define TEMP_MIN_ROUTINE(NR) \
  810. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  811. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  812. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  813. minttemp_raw[NR] += OVERSAMPLENR; \
  814. else \
  815. minttemp_raw[NR] -= OVERSAMPLENR; \
  816. }
  817. #define TEMP_MAX_ROUTINE(NR) \
  818. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  819. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  820. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  821. maxttemp_raw[NR] -= OVERSAMPLENR; \
  822. else \
  823. maxttemp_raw[NR] += OVERSAMPLENR; \
  824. }
  825. #ifdef HEATER_0_MINTEMP
  826. TEMP_MIN_ROUTINE(0);
  827. #endif
  828. #ifdef HEATER_0_MAXTEMP
  829. TEMP_MAX_ROUTINE(0);
  830. #endif
  831. #if EXTRUDERS > 1
  832. #ifdef HEATER_1_MINTEMP
  833. TEMP_MIN_ROUTINE(1);
  834. #endif
  835. #ifdef HEATER_1_MAXTEMP
  836. TEMP_MAX_ROUTINE(1);
  837. #endif
  838. #if EXTRUDERS > 2
  839. #ifdef HEATER_2_MINTEMP
  840. TEMP_MIN_ROUTINE(2);
  841. #endif
  842. #ifdef HEATER_2_MAXTEMP
  843. TEMP_MAX_ROUTINE(2);
  844. #endif
  845. #if EXTRUDERS > 3
  846. #ifdef HEATER_3_MINTEMP
  847. TEMP_MIN_ROUTINE(3);
  848. #endif
  849. #ifdef HEATER_3_MAXTEMP
  850. TEMP_MAX_ROUTINE(3);
  851. #endif
  852. #endif // EXTRUDERS > 3
  853. #endif // EXTRUDERS > 2
  854. #endif // EXTRUDERS > 1
  855. #ifdef BED_MINTEMP
  856. /* No bed MINTEMP error implemented?!? */ /*
  857. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  858. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  859. bed_minttemp_raw += OVERSAMPLENR;
  860. #else
  861. bed_minttemp_raw -= OVERSAMPLENR;
  862. #endif
  863. }
  864. */
  865. #endif //BED_MINTEMP
  866. #ifdef BED_MAXTEMP
  867. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  868. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  869. bed_maxttemp_raw -= OVERSAMPLENR;
  870. #else
  871. bed_maxttemp_raw += OVERSAMPLENR;
  872. #endif
  873. }
  874. #endif //BED_MAXTEMP
  875. }
  876. void setWatch() {
  877. #ifdef WATCH_TEMP_PERIOD
  878. millis_t ms = millis();
  879. for (int e = 0; e < EXTRUDERS; e++) {
  880. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2)) {
  881. watch_start_temp[e] = degHotend(e);
  882. watchmillis[e] = ms;
  883. }
  884. }
  885. #endif
  886. }
  887. #if HAS_HEATER_THERMAL_PROTECTION || HAS_BED_THERMAL_PROTECTION
  888. void thermal_runaway_protection(TRState *state, millis_t *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  889. static float tr_target_temperature[EXTRUDERS+1] = { 0.0 };
  890. /*
  891. SERIAL_ECHO_START;
  892. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  893. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHOPGM(heater_id);
  894. SERIAL_ECHOPGM(" ; State:");
  895. SERIAL_ECHOPGM(*state);
  896. SERIAL_ECHOPGM(" ; Timer:");
  897. SERIAL_ECHOPGM(*timer);
  898. SERIAL_ECHOPGM(" ; Temperature:");
  899. SERIAL_ECHOPGM(temperature);
  900. SERIAL_ECHOPGM(" ; Target Temp:");
  901. SERIAL_ECHOPGM(target_temperature);
  902. SERIAL_EOL;
  903. */
  904. int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
  905. // If the target temperature changes, restart
  906. if (tr_target_temperature[heater_index] != target_temperature)
  907. *state = TRReset;
  908. switch (*state) {
  909. case TRReset:
  910. *timer = 0;
  911. *state = TRInactive;
  912. break;
  913. // Inactive state waits for a target temperature to be set
  914. case TRInactive:
  915. if (target_temperature > 0) {
  916. tr_target_temperature[heater_index] = target_temperature;
  917. *state = TRFirstHeating;
  918. }
  919. break;
  920. // When first heating, wait for the temperature to be reached then go to Stable state
  921. case TRFirstHeating:
  922. if (temperature >= tr_target_temperature[heater_index]) *state = TRStable;
  923. break;
  924. // While the temperature is stable watch for a bad temperature
  925. case TRStable:
  926. // If the temperature is over the target (-hysteresis) restart the timer
  927. if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc)
  928. *timer = millis();
  929. // If the timer goes too long without a reset, trigger shutdown
  930. else if (millis() > *timer + period_seconds * 1000UL)
  931. *state = TRRunaway;
  932. break;
  933. case TRRunaway:
  934. SERIAL_ERROR_START;
  935. SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
  936. if (heater_id < 0) SERIAL_ERRORLNPGM("bed"); else SERIAL_ERRORLN(heater_id);
  937. LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY);
  938. disable_all_heaters();
  939. disable_all_steppers();
  940. for (;;) {
  941. manage_heater();
  942. lcd_update();
  943. }
  944. }
  945. }
  946. #endif // HAS_HEATER_THERMAL_PROTECTION || HAS_BED_THERMAL_PROTECTION
  947. void disable_all_heaters() {
  948. for (int i=0; i<EXTRUDERS; i++) setTargetHotend(0, i);
  949. setTargetBed(0);
  950. #define DISABLE_HEATER(NR) { \
  951. target_temperature[NR] = 0; \
  952. soft_pwm[NR] = 0; \
  953. WRITE_HEATER_ ## NR (LOW); \
  954. }
  955. #if HAS_TEMP_0
  956. target_temperature[0] = 0;
  957. soft_pwm[0] = 0;
  958. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  959. #endif
  960. #if EXTRUDERS > 1 && HAS_TEMP_1
  961. DISABLE_HEATER(1);
  962. #endif
  963. #if EXTRUDERS > 2 && HAS_TEMP_2
  964. DISABLE_HEATER(2);
  965. #endif
  966. #if EXTRUDERS > 3 && HAS_TEMP_3
  967. DISABLE_HEATER(3);
  968. #endif
  969. #if HAS_TEMP_BED
  970. target_temperature_bed = 0;
  971. soft_pwm_bed = 0;
  972. #if HAS_HEATER_BED
  973. WRITE_HEATER_BED(LOW);
  974. #endif
  975. #endif
  976. }
  977. #ifdef HEATER_0_USES_MAX6675
  978. #define MAX6675_HEAT_INTERVAL 250u
  979. static millis_t next_max6675_ms = 0;
  980. int max6675_temp = 2000;
  981. static int read_max6675() {
  982. millis_t ms = millis();
  983. if (ms < next_max6675_ms)
  984. return max6675_temp;
  985. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  986. max6675_temp = 0;
  987. #ifdef PRR
  988. PRR &= ~BIT(PRSPI);
  989. #elif defined(PRR0)
  990. PRR0 &= ~BIT(PRSPI);
  991. #endif
  992. SPCR = BIT(MSTR) | BIT(SPE) | BIT(SPR0);
  993. // enable TT_MAX6675
  994. WRITE(MAX6675_SS, 0);
  995. // ensure 100ns delay - a bit extra is fine
  996. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  997. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  998. // read MSB
  999. SPDR = 0;
  1000. for (;(SPSR & BIT(SPIF)) == 0;);
  1001. max6675_temp = SPDR;
  1002. max6675_temp <<= 8;
  1003. // read LSB
  1004. SPDR = 0;
  1005. for (;(SPSR & BIT(SPIF)) == 0;);
  1006. max6675_temp |= SPDR;
  1007. // disable TT_MAX6675
  1008. WRITE(MAX6675_SS, 1);
  1009. if (max6675_temp & 4) {
  1010. // thermocouple open
  1011. max6675_temp = 4000;
  1012. }
  1013. else {
  1014. max6675_temp = max6675_temp >> 3;
  1015. }
  1016. return max6675_temp;
  1017. }
  1018. #endif //HEATER_0_USES_MAX6675
  1019. /**
  1020. * Stages in the ISR loop
  1021. */
  1022. enum TempState {
  1023. PrepareTemp_0,
  1024. MeasureTemp_0,
  1025. PrepareTemp_BED,
  1026. MeasureTemp_BED,
  1027. PrepareTemp_1,
  1028. MeasureTemp_1,
  1029. PrepareTemp_2,
  1030. MeasureTemp_2,
  1031. PrepareTemp_3,
  1032. MeasureTemp_3,
  1033. Prepare_FILWIDTH,
  1034. Measure_FILWIDTH,
  1035. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1036. };
  1037. static unsigned long raw_temp_value[4] = { 0 };
  1038. static unsigned long raw_temp_bed_value = 0;
  1039. static void set_current_temp_raw() {
  1040. #if HAS_TEMP_0 && !defined(HEATER_0_USES_MAX6675)
  1041. current_temperature_raw[0] = raw_temp_value[0];
  1042. #endif
  1043. #if HAS_TEMP_1
  1044. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1045. redundant_temperature_raw = raw_temp_value[1];
  1046. #else
  1047. current_temperature_raw[1] = raw_temp_value[1];
  1048. #endif
  1049. #if HAS_TEMP_2
  1050. current_temperature_raw[2] = raw_temp_value[2];
  1051. #if HAS_TEMP_3
  1052. current_temperature_raw[3] = raw_temp_value[3];
  1053. #endif
  1054. #endif
  1055. #endif
  1056. current_temperature_bed_raw = raw_temp_bed_value;
  1057. temp_meas_ready = true;
  1058. }
  1059. /**
  1060. * Timer 0 is shared with millies
  1061. * - Manage PWM to all the heaters and fan
  1062. * - Update the raw temperature values
  1063. * - Check new temperature values for MIN/MAX errors
  1064. * - Step the babysteps value for each axis towards 0
  1065. */
  1066. ISR(TIMER0_COMPB_vect) {
  1067. static unsigned char temp_count = 0;
  1068. static TempState temp_state = StartupDelay;
  1069. static unsigned char pwm_count = BIT(SOFT_PWM_SCALE);
  1070. // Static members for each heater
  1071. #ifdef SLOW_PWM_HEATERS
  1072. static unsigned char slow_pwm_count = 0;
  1073. #define ISR_STATICS(n) \
  1074. static unsigned char soft_pwm_ ## n; \
  1075. static unsigned char state_heater_ ## n = 0; \
  1076. static unsigned char state_timer_heater_ ## n = 0
  1077. #else
  1078. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1079. #endif
  1080. // Statics per heater
  1081. ISR_STATICS(0);
  1082. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1083. ISR_STATICS(1);
  1084. #if EXTRUDERS > 2
  1085. ISR_STATICS(2);
  1086. #if EXTRUDERS > 3
  1087. ISR_STATICS(3);
  1088. #endif
  1089. #endif
  1090. #endif
  1091. #if HAS_HEATER_BED
  1092. ISR_STATICS(BED);
  1093. #endif
  1094. #if HAS_FILAMENT_SENSOR
  1095. static unsigned long raw_filwidth_value = 0;
  1096. #endif
  1097. #ifndef SLOW_PWM_HEATERS
  1098. /**
  1099. * standard PWM modulation
  1100. */
  1101. if (pwm_count == 0) {
  1102. soft_pwm_0 = soft_pwm[0];
  1103. if (soft_pwm_0 > 0) {
  1104. WRITE_HEATER_0(1);
  1105. }
  1106. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1107. #if EXTRUDERS > 1
  1108. soft_pwm_1 = soft_pwm[1];
  1109. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1110. #if EXTRUDERS > 2
  1111. soft_pwm_2 = soft_pwm[2];
  1112. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1113. #if EXTRUDERS > 3
  1114. soft_pwm_3 = soft_pwm[3];
  1115. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1116. #endif
  1117. #endif
  1118. #endif
  1119. #if HAS_HEATER_BED
  1120. soft_pwm_BED = soft_pwm_bed;
  1121. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1122. #endif
  1123. #ifdef FAN_SOFT_PWM
  1124. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1125. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1126. #endif
  1127. }
  1128. if (soft_pwm_0 < pwm_count) { WRITE_HEATER_0(0); }
  1129. #if EXTRUDERS > 1
  1130. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1131. #if EXTRUDERS > 2
  1132. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1133. #if EXTRUDERS > 3
  1134. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1135. #endif
  1136. #endif
  1137. #endif
  1138. #if HAS_HEATER_BED
  1139. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1140. #endif
  1141. #ifdef FAN_SOFT_PWM
  1142. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1143. #endif
  1144. pwm_count += BIT(SOFT_PWM_SCALE);
  1145. pwm_count &= 0x7f;
  1146. #else // SLOW_PWM_HEATERS
  1147. /*
  1148. * SLOW PWM HEATERS
  1149. *
  1150. * for heaters drived by relay
  1151. */
  1152. #ifndef MIN_STATE_TIME
  1153. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1154. #endif
  1155. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1156. #define _SLOW_PWM_ROUTINE(NR, src) \
  1157. soft_pwm_ ## NR = src; \
  1158. if (soft_pwm_ ## NR > 0) { \
  1159. if (state_timer_heater_ ## NR == 0) { \
  1160. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1161. state_heater_ ## NR = 1; \
  1162. WRITE_HEATER_ ## NR(1); \
  1163. } \
  1164. } \
  1165. else { \
  1166. if (state_timer_heater_ ## NR == 0) { \
  1167. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1168. state_heater_ ## NR = 0; \
  1169. WRITE_HEATER_ ## NR(0); \
  1170. } \
  1171. }
  1172. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1173. #define PWM_OFF_ROUTINE(NR) \
  1174. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1175. if (state_timer_heater_ ## NR == 0) { \
  1176. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1177. state_heater_ ## NR = 0; \
  1178. WRITE_HEATER_ ## NR (0); \
  1179. } \
  1180. }
  1181. if (slow_pwm_count == 0) {
  1182. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1183. #if EXTRUDERS > 1
  1184. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1185. #if EXTRUDERS > 2
  1186. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1187. #if EXTRUDERS > 3
  1188. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1189. #endif
  1190. #endif
  1191. #endif
  1192. #if HAS_HEATER_BED
  1193. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1194. #endif
  1195. } // slow_pwm_count == 0
  1196. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1197. #if EXTRUDERS > 1
  1198. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1199. #if EXTRUDERS > 2
  1200. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1201. #if EXTRUDERS > 3
  1202. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1203. #endif
  1204. #endif
  1205. #endif
  1206. #if HAS_HEATER_BED
  1207. PWM_OFF_ROUTINE(BED); // BED
  1208. #endif
  1209. #ifdef FAN_SOFT_PWM
  1210. if (pwm_count == 0) {
  1211. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1212. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1213. }
  1214. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1215. #endif //FAN_SOFT_PWM
  1216. pwm_count += BIT(SOFT_PWM_SCALE);
  1217. pwm_count &= 0x7f;
  1218. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1219. if ((pwm_count % 64) == 0) {
  1220. slow_pwm_count++;
  1221. slow_pwm_count &= 0x7f;
  1222. // EXTRUDER 0
  1223. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1224. #if EXTRUDERS > 1 // EXTRUDER 1
  1225. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1226. #if EXTRUDERS > 2 // EXTRUDER 2
  1227. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1228. #if EXTRUDERS > 3 // EXTRUDER 3
  1229. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1230. #endif
  1231. #endif
  1232. #endif
  1233. #if HAS_HEATER_BED
  1234. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1235. #endif
  1236. } // (pwm_count % 64) == 0
  1237. #endif // SLOW_PWM_HEATERS
  1238. #define SET_ADMUX_ADCSRA(pin) ADMUX = BIT(REFS0) | (pin & 0x07); ADCSRA |= BIT(ADSC)
  1239. #ifdef MUX5
  1240. #define START_ADC(pin) if (pin > 7) ADCSRB = BIT(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1241. #else
  1242. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1243. #endif
  1244. // Prepare or measure a sensor, each one every 12th frame
  1245. switch(temp_state) {
  1246. case PrepareTemp_0:
  1247. #if HAS_TEMP_0
  1248. START_ADC(TEMP_0_PIN);
  1249. #endif
  1250. lcd_buttons_update();
  1251. temp_state = MeasureTemp_0;
  1252. break;
  1253. case MeasureTemp_0:
  1254. #if HAS_TEMP_0
  1255. raw_temp_value[0] += ADC;
  1256. #endif
  1257. temp_state = PrepareTemp_BED;
  1258. break;
  1259. case PrepareTemp_BED:
  1260. #if HAS_TEMP_BED
  1261. START_ADC(TEMP_BED_PIN);
  1262. #endif
  1263. lcd_buttons_update();
  1264. temp_state = MeasureTemp_BED;
  1265. break;
  1266. case MeasureTemp_BED:
  1267. #if HAS_TEMP_BED
  1268. raw_temp_bed_value += ADC;
  1269. #endif
  1270. temp_state = PrepareTemp_1;
  1271. break;
  1272. case PrepareTemp_1:
  1273. #if HAS_TEMP_1
  1274. START_ADC(TEMP_1_PIN);
  1275. #endif
  1276. lcd_buttons_update();
  1277. temp_state = MeasureTemp_1;
  1278. break;
  1279. case MeasureTemp_1:
  1280. #if HAS_TEMP_1
  1281. raw_temp_value[1] += ADC;
  1282. #endif
  1283. temp_state = PrepareTemp_2;
  1284. break;
  1285. case PrepareTemp_2:
  1286. #if HAS_TEMP_2
  1287. START_ADC(TEMP_2_PIN);
  1288. #endif
  1289. lcd_buttons_update();
  1290. temp_state = MeasureTemp_2;
  1291. break;
  1292. case MeasureTemp_2:
  1293. #if HAS_TEMP_2
  1294. raw_temp_value[2] += ADC;
  1295. #endif
  1296. temp_state = PrepareTemp_3;
  1297. break;
  1298. case PrepareTemp_3:
  1299. #if HAS_TEMP_3
  1300. START_ADC(TEMP_3_PIN);
  1301. #endif
  1302. lcd_buttons_update();
  1303. temp_state = MeasureTemp_3;
  1304. break;
  1305. case MeasureTemp_3:
  1306. #if HAS_TEMP_3
  1307. raw_temp_value[3] += ADC;
  1308. #endif
  1309. temp_state = Prepare_FILWIDTH;
  1310. break;
  1311. case Prepare_FILWIDTH:
  1312. #if HAS_FILAMENT_SENSOR
  1313. START_ADC(FILWIDTH_PIN);
  1314. #endif
  1315. lcd_buttons_update();
  1316. temp_state = Measure_FILWIDTH;
  1317. break;
  1318. case Measure_FILWIDTH:
  1319. #if HAS_FILAMENT_SENSOR
  1320. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1321. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1322. raw_filwidth_value -= (raw_filwidth_value>>7); //multiply raw_filwidth_value by 127/128
  1323. raw_filwidth_value += ((unsigned long)ADC<<7); //add new ADC reading
  1324. }
  1325. #endif
  1326. temp_state = PrepareTemp_0;
  1327. temp_count++;
  1328. break;
  1329. case StartupDelay:
  1330. temp_state = PrepareTemp_0;
  1331. break;
  1332. // default:
  1333. // SERIAL_ERROR_START;
  1334. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1335. // break;
  1336. } // switch(temp_state)
  1337. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1338. // Update the raw values if they've been read. Else we could be updating them during reading.
  1339. if (!temp_meas_ready) set_current_temp_raw();
  1340. // Filament Sensor - can be read any time since IIR filtering is used
  1341. #if HAS_FILAMENT_SENSOR
  1342. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1343. #endif
  1344. temp_count = 0;
  1345. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1346. raw_temp_bed_value = 0;
  1347. #if HAS_TEMP_0 && !defined(HEATER_0_USES_MAX6675)
  1348. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1349. #define GE0 <=
  1350. #else
  1351. #define GE0 >=
  1352. #endif
  1353. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1354. if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
  1355. #endif
  1356. #if HAS_TEMP_1
  1357. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1358. #define GE1 <=
  1359. #else
  1360. #define GE1 >=
  1361. #endif
  1362. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1363. if (minttemp_raw[1] GE1 current_temperature_raw[1]) min_temp_error(1);
  1364. #endif // TEMP_SENSOR_1
  1365. #if HAS_TEMP_2
  1366. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1367. #define GE2 <=
  1368. #else
  1369. #define GE2 >=
  1370. #endif
  1371. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1372. if (minttemp_raw[2] GE2 current_temperature_raw[2]) min_temp_error(2);
  1373. #endif // TEMP_SENSOR_2
  1374. #if HAS_TEMP_3
  1375. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1376. #define GE3 <=
  1377. #else
  1378. #define GE3 >=
  1379. #endif
  1380. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1381. if (minttemp_raw[3] GE3 current_temperature_raw[3]) min_temp_error(3);
  1382. #endif // TEMP_SENSOR_3
  1383. #if HAS_TEMP_BED
  1384. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1385. #define GEBED <=
  1386. #else
  1387. #define GEBED >=
  1388. #endif
  1389. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) {
  1390. target_temperature_bed = 0;
  1391. bed_max_temp_error();
  1392. }
  1393. #endif
  1394. } // temp_count >= OVERSAMPLENR
  1395. #ifdef BABYSTEPPING
  1396. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1397. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1398. if (curTodo > 0) {
  1399. babystep(axis,/*fwd*/true);
  1400. babystepsTodo[axis]--; //fewer to do next time
  1401. }
  1402. else if (curTodo < 0) {
  1403. babystep(axis,/*fwd*/false);
  1404. babystepsTodo[axis]++; //fewer to do next time
  1405. }
  1406. }
  1407. #endif //BABYSTEPPING
  1408. }
  1409. #ifdef PIDTEMP
  1410. // Apply the scale factors to the PID values
  1411. float scalePID_i(float i) { return i * PID_dT; }
  1412. float unscalePID_i(float i) { return i / PID_dT; }
  1413. float scalePID_d(float d) { return d / PID_dT; }
  1414. float unscalePID_d(float d) { return d * PID_dT; }
  1415. #endif //PIDTEMP