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

temperature.cpp 54KB

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