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

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