My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 47KB

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