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.

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