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.

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