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

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