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 48KB

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