My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

temperature.cpp 69KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  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 "temperature.h"
  27. #include "thermistortables.h"
  28. #include "ultralcd.h"
  29. #include "planner.h"
  30. #include "language.h"
  31. #if ENABLED(HEATER_0_USES_MAX6675)
  32. #include "spi.h"
  33. #endif
  34. #if ENABLED(BABYSTEPPING)
  35. #include "stepper.h"
  36. #endif
  37. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  38. #include "endstops.h"
  39. #endif
  40. #if ENABLED(USE_WATCHDOG)
  41. #include "watchdog.h"
  42. #endif
  43. #ifdef K1 // Defined in Configuration.h in the PID settings
  44. #define K2 (1.0-K1)
  45. #endif
  46. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  47. static void* heater_ttbl_map[2] = { (void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  48. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  49. #else
  50. 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, (void*)HEATER_4_TEMPTABLE);
  51. 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, HEATER_4_TEMPTABLE_LEN);
  52. #endif
  53. Temperature thermalManager;
  54. // public:
  55. float Temperature::current_temperature[HOTENDS] = { 0.0 },
  56. Temperature::current_temperature_bed = 0.0;
  57. int16_t Temperature::current_temperature_raw[HOTENDS] = { 0 },
  58. Temperature::target_temperature[HOTENDS] = { 0 },
  59. Temperature::current_temperature_bed_raw = 0;
  60. #if HAS_HEATER_BED
  61. int16_t Temperature::target_temperature_bed = 0;
  62. #endif
  63. // Initialized by settings.load()
  64. #if ENABLED(PIDTEMP)
  65. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  66. float Temperature::Kp[HOTENDS], Temperature::Ki[HOTENDS], Temperature::Kd[HOTENDS];
  67. #if ENABLED(PID_EXTRUSION_SCALING)
  68. float Temperature::Kc[HOTENDS];
  69. #endif
  70. #else
  71. float Temperature::Kp, Temperature::Ki, Temperature::Kd;
  72. #if ENABLED(PID_EXTRUSION_SCALING)
  73. float Temperature::Kc;
  74. #endif
  75. #endif
  76. #endif
  77. // Initialized by settings.load()
  78. #if ENABLED(PIDTEMPBED)
  79. float Temperature::bedKp, Temperature::bedKi, Temperature::bedKd;
  80. #endif
  81. #if ENABLED(BABYSTEPPING)
  82. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  83. #endif
  84. #if WATCH_HOTENDS
  85. uint16_t Temperature::watch_target_temp[HOTENDS] = { 0 };
  86. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  87. #endif
  88. #if WATCH_THE_BED
  89. uint16_t Temperature::watch_target_bed_temp = 0;
  90. millis_t Temperature::watch_bed_next_ms = 0;
  91. #endif
  92. #if ENABLED(PREVENT_COLD_EXTRUSION)
  93. bool Temperature::allow_cold_extrude = false;
  94. int16_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  95. #endif
  96. // private:
  97. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  98. uint16_t Temperature::redundant_temperature_raw = 0;
  99. float Temperature::redundant_temperature = 0.0;
  100. #endif
  101. volatile bool Temperature::temp_meas_ready = false;
  102. #if ENABLED(PIDTEMP)
  103. float Temperature::temp_iState[HOTENDS] = { 0 },
  104. Temperature::temp_dState[HOTENDS] = { 0 },
  105. Temperature::pTerm[HOTENDS],
  106. Temperature::iTerm[HOTENDS],
  107. Temperature::dTerm[HOTENDS];
  108. #if ENABLED(PID_EXTRUSION_SCALING)
  109. float Temperature::cTerm[HOTENDS];
  110. long Temperature::last_e_position;
  111. long Temperature::lpq[LPQ_MAX_LEN];
  112. int Temperature::lpq_ptr = 0;
  113. #endif
  114. float Temperature::pid_error[HOTENDS];
  115. bool Temperature::pid_reset[HOTENDS];
  116. #endif
  117. #if ENABLED(PIDTEMPBED)
  118. float Temperature::temp_iState_bed = { 0 },
  119. Temperature::temp_dState_bed = { 0 },
  120. Temperature::pTerm_bed,
  121. Temperature::iTerm_bed,
  122. Temperature::dTerm_bed,
  123. Temperature::pid_error_bed;
  124. #else
  125. millis_t Temperature::next_bed_check_ms;
  126. #endif
  127. uint16_t Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 },
  128. Temperature::raw_temp_bed_value = 0;
  129. // Init min and max temp with extreme values to prevent false errors during startup
  130. int16_t 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, HEATER_4_RAW_LO_TEMP),
  131. 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, HEATER_4_RAW_HI_TEMP),
  132. Temperature::minttemp[HOTENDS] = { 0 },
  133. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  134. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  135. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  136. #endif
  137. #ifdef MILLISECONDS_PREHEAT_TIME
  138. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  139. #endif
  140. #ifdef BED_MINTEMP
  141. int16_t Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  142. #endif
  143. #ifdef BED_MAXTEMP
  144. int16_t Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  145. #endif
  146. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  147. int8_t Temperature::meas_shift_index; // Index of a delayed sample in buffer
  148. #endif
  149. #if HAS_AUTO_FAN
  150. millis_t Temperature::next_auto_fan_check_ms = 0;
  151. #endif
  152. uint8_t Temperature::soft_pwm_amount[HOTENDS],
  153. Temperature::soft_pwm_amount_bed;
  154. #if ENABLED(FAN_SOFT_PWM)
  155. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  156. Temperature::soft_pwm_count_fan[FAN_COUNT];
  157. #endif
  158. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  159. uint16_t Temperature::current_raw_filwidth = 0; // Measured filament diameter - one extruder only
  160. #endif
  161. #if ENABLED(PROBING_HEATERS_OFF)
  162. bool Temperature::paused;
  163. #endif
  164. #if HEATER_IDLE_HANDLER
  165. millis_t Temperature::heater_idle_timeout_ms[HOTENDS] = { 0 };
  166. bool Temperature::heater_idle_timeout_exceeded[HOTENDS] = { false };
  167. #if HAS_TEMP_BED
  168. millis_t Temperature::bed_idle_timeout_ms = 0;
  169. bool Temperature::bed_idle_timeout_exceeded = false;
  170. #endif
  171. #endif
  172. #if ENABLED(ADC_KEYPAD)
  173. uint32_t Temperature::current_ADCKey_raw = 0;
  174. uint8_t Temperature::ADCKey_count = 0;
  175. #endif
  176. #if HAS_PID_HEATING
  177. /**
  178. * PID Autotuning (M303)
  179. *
  180. * Alternately heat and cool the nozzle, observing its behavior to
  181. * determine the best PID values to achieve a stable temperature.
  182. */
  183. void Temperature::PID_autotune(const float temp, const int8_t hotend, const int8_t ncycles, const bool set_result/*=false*/) {
  184. float input = 0.0;
  185. int cycles = 0;
  186. bool heating = true;
  187. millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
  188. long t_high = 0, t_low = 0;
  189. long bias, d;
  190. float Ku, Tu,
  191. workKp = 0, workKi = 0, workKd = 0,
  192. max = 0, min = 10000;
  193. #if WATCH_THE_BED || WATCH_HOTENDS
  194. const float watch_temp_target = temp -
  195. #if ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED) && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  196. (hotend < 0 ? (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1) : (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1))
  197. #elif ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED)
  198. (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)
  199. #else
  200. (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)
  201. #endif
  202. ;
  203. const int8_t watch_temp_period =
  204. #if ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED) && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  205. hotend < 0 ? WATCH_BED_TEMP_PERIOD : WATCH_TEMP_PERIOD
  206. #elif ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED)
  207. WATCH_BED_TEMP_PERIOD
  208. #else
  209. WATCH_TEMP_PERIOD
  210. #endif
  211. ;
  212. const int8_t watch_temp_increase =
  213. #if ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED) && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  214. hotend < 0 ? WATCH_BED_TEMP_INCREASE : WATCH_TEMP_INCREASE
  215. #elif ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED)
  216. WATCH_BED_TEMP_INCREASE
  217. #else
  218. WATCH_TEMP_INCREASE
  219. #endif
  220. ;
  221. millis_t temp_change_ms = next_temp_ms + watch_temp_period * 1000UL;
  222. float next_watch_temp = 0.0;
  223. bool heated = false;
  224. #endif
  225. #if HAS_AUTO_FAN
  226. next_auto_fan_check_ms = next_temp_ms + 2500UL;
  227. #endif
  228. #if ENABLED(PIDTEMP)
  229. #define _TOP_HOTEND HOTENDS - 1
  230. #else
  231. #define _TOP_HOTEND -1
  232. #endif
  233. #if ENABLED(PIDTEMPBED)
  234. #define _BOT_HOTEND -1
  235. #else
  236. #define _BOT_HOTEND 0
  237. #endif
  238. if (!WITHIN(hotend, _BOT_HOTEND, _TOP_HOTEND)) {
  239. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  240. return;
  241. }
  242. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  243. disable_all_heaters(); // switch off all heaters.
  244. #if HAS_PID_FOR_BOTH
  245. if (hotend < 0)
  246. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  247. else
  248. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  249. #elif ENABLED(PIDTEMP)
  250. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  251. #else
  252. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  253. #endif
  254. wait_for_heatup = true;
  255. // PID Tuning loop
  256. while (wait_for_heatup) {
  257. const millis_t ms = millis();
  258. if (temp_meas_ready) { // temp sample ready
  259. updateTemperaturesFromRawValues();
  260. input =
  261. #if HAS_PID_FOR_BOTH
  262. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  263. #elif ENABLED(PIDTEMP)
  264. current_temperature[hotend]
  265. #else
  266. current_temperature_bed
  267. #endif
  268. ;
  269. NOLESS(max, input);
  270. NOMORE(min, input);
  271. #if HAS_AUTO_FAN
  272. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  273. checkExtruderAutoFans();
  274. next_auto_fan_check_ms = ms + 2500UL;
  275. }
  276. #endif
  277. if (heating && input > temp) {
  278. if (ELAPSED(ms, t2 + 5000UL)) {
  279. heating = false;
  280. #if HAS_PID_FOR_BOTH
  281. if (hotend < 0)
  282. soft_pwm_amount_bed = (bias - d) >> 1;
  283. else
  284. soft_pwm_amount[hotend] = (bias - d) >> 1;
  285. #elif ENABLED(PIDTEMP)
  286. soft_pwm_amount[hotend] = (bias - d) >> 1;
  287. #elif ENABLED(PIDTEMPBED)
  288. soft_pwm_amount_bed = (bias - d) >> 1;
  289. #endif
  290. t1 = ms;
  291. t_high = t1 - t2;
  292. max = temp;
  293. }
  294. }
  295. if (!heating && input < temp) {
  296. if (ELAPSED(ms, t1 + 5000UL)) {
  297. heating = true;
  298. t2 = ms;
  299. t_low = t2 - t1;
  300. if (cycles > 0) {
  301. long max_pow =
  302. #if HAS_PID_FOR_BOTH
  303. hotend < 0 ? MAX_BED_POWER : PID_MAX
  304. #elif ENABLED(PIDTEMP)
  305. PID_MAX
  306. #else
  307. MAX_BED_POWER
  308. #endif
  309. ;
  310. bias += (d * (t_high - t_low)) / (t_low + t_high);
  311. bias = constrain(bias, 20, max_pow - 20);
  312. d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias;
  313. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  314. SERIAL_PROTOCOLPAIR(MSG_D, d);
  315. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  316. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  317. if (cycles > 2) {
  318. Ku = (4.0 * d) / (M_PI * (max - min) * 0.5);
  319. Tu = ((float)(t_low + t_high) * 0.001);
  320. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  321. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  322. workKp = 0.6 * Ku;
  323. workKi = 2 * workKp / Tu;
  324. workKd = workKp * Tu * 0.125;
  325. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  326. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  327. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  328. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  329. /**
  330. workKp = 0.33*Ku;
  331. workKi = workKp/Tu;
  332. workKd = workKp*Tu/3;
  333. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  334. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  335. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  336. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  337. workKp = 0.2*Ku;
  338. workKi = 2*workKp/Tu;
  339. workKd = workKp*Tu/3;
  340. SERIAL_PROTOCOLLNPGM(" No overshoot");
  341. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  342. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  343. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  344. */
  345. }
  346. }
  347. #if HAS_PID_FOR_BOTH
  348. if (hotend < 0)
  349. soft_pwm_amount_bed = (bias + d) >> 1;
  350. else
  351. soft_pwm_amount[hotend] = (bias + d) >> 1;
  352. #elif ENABLED(PIDTEMP)
  353. soft_pwm_amount[hotend] = (bias + d) >> 1;
  354. #else
  355. soft_pwm_amount_bed = (bias + d) >> 1;
  356. #endif
  357. cycles++;
  358. min = temp;
  359. }
  360. }
  361. }
  362. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  363. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  364. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  365. break;
  366. }
  367. // Every 2 seconds...
  368. if (ELAPSED(ms, next_temp_ms)) {
  369. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  370. print_heaterstates();
  371. SERIAL_EOL();
  372. #endif
  373. next_temp_ms = ms + 2000UL;
  374. #if WATCH_THE_BED || WATCH_HOTENDS
  375. if (!heated && input > next_watch_temp) {
  376. if (input > watch_temp_target) heated = true;
  377. next_watch_temp = input + watch_temp_increase;
  378. temp_change_ms = ms + watch_temp_period * 1000UL;
  379. }
  380. else if (!heated && ELAPSED(ms, temp_change_ms))
  381. _temp_error(hotend, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  382. else if (heated && input < temp - MAX_OVERSHOOT_PID_AUTOTUNE)
  383. _temp_error(hotend, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  384. #endif
  385. } // every 2 seconds
  386. // Timeout after 20 minutes since the last undershoot/overshoot cycle
  387. if (((ms - t1) + (ms - t2)) > (20L * 60L * 1000L)) {
  388. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  389. break;
  390. }
  391. if (cycles > ncycles) {
  392. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  393. #if HAS_PID_FOR_BOTH
  394. const char* estring = hotend < 0 ? "bed" : "";
  395. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL();
  396. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL();
  397. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL();
  398. #elif ENABLED(PIDTEMP)
  399. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL();
  400. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL();
  401. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL();
  402. #else
  403. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL();
  404. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL();
  405. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL();
  406. #endif
  407. #define _SET_BED_PID() do { \
  408. bedKp = workKp; \
  409. bedKi = scalePID_i(workKi); \
  410. bedKd = scalePID_d(workKd); \
  411. }while(0)
  412. #define _SET_EXTRUDER_PID() do { \
  413. PID_PARAM(Kp, hotend) = workKp; \
  414. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  415. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  416. updatePID(); }while(0)
  417. // Use the result? (As with "M303 U1")
  418. if (set_result) {
  419. #if HAS_PID_FOR_BOTH
  420. if (hotend < 0)
  421. _SET_BED_PID();
  422. else
  423. _SET_EXTRUDER_PID();
  424. #elif ENABLED(PIDTEMP)
  425. _SET_EXTRUDER_PID();
  426. #else
  427. _SET_BED_PID();
  428. #endif
  429. }
  430. return;
  431. }
  432. lcd_update();
  433. }
  434. disable_all_heaters();
  435. }
  436. #endif // HAS_PID_HEATING
  437. /**
  438. * Class and Instance Methods
  439. */
  440. Temperature::Temperature() { }
  441. int Temperature::getHeaterPower(int heater) {
  442. return heater < 0 ? soft_pwm_amount_bed : soft_pwm_amount[heater];
  443. }
  444. #if HAS_AUTO_FAN
  445. void Temperature::checkExtruderAutoFans() {
  446. static const int8_t fanPin[] PROGMEM = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN };
  447. static const uint8_t fanBit[] PROGMEM = {
  448. 0,
  449. AUTO_1_IS_0 ? 0 : 1,
  450. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  451. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3,
  452. AUTO_4_IS_0 ? 0 : AUTO_4_IS_1 ? 1 : AUTO_4_IS_2 ? 2 : AUTO_4_IS_3 ? 3 : 4
  453. };
  454. uint8_t fanState = 0;
  455. HOTEND_LOOP()
  456. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  457. SBI(fanState, pgm_read_byte(&fanBit[e]));
  458. uint8_t fanDone = 0;
  459. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  460. int8_t pin = pgm_read_byte(&fanPin[f]);
  461. const uint8_t bit = pgm_read_byte(&fanBit[f]);
  462. if (pin >= 0 && !TEST(fanDone, bit)) {
  463. uint8_t newFanSpeed = TEST(fanState, bit) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  464. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  465. digitalWrite(pin, newFanSpeed);
  466. analogWrite(pin, newFanSpeed);
  467. SBI(fanDone, bit);
  468. }
  469. }
  470. }
  471. #endif // HAS_AUTO_FAN
  472. //
  473. // Temperature Error Handlers
  474. //
  475. void Temperature::_temp_error(const int8_t e, const char * const serial_msg, const char * const lcd_msg) {
  476. static bool killed = false;
  477. if (IsRunning()) {
  478. SERIAL_ERROR_START();
  479. serialprintPGM(serial_msg);
  480. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  481. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  482. }
  483. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  484. if (!killed) {
  485. Running = false;
  486. killed = true;
  487. kill(lcd_msg);
  488. }
  489. else
  490. disable_all_heaters(); // paranoia
  491. #endif
  492. }
  493. void Temperature::max_temp_error(const int8_t e) {
  494. #if HAS_TEMP_BED
  495. _temp_error(e, PSTR(MSG_T_MAXTEMP), e >= 0 ? PSTR(MSG_ERR_MAXTEMP) : PSTR(MSG_ERR_MAXTEMP_BED));
  496. #else
  497. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  498. #if HOTENDS == 1
  499. UNUSED(e);
  500. #endif
  501. #endif
  502. }
  503. void Temperature::min_temp_error(const int8_t e) {
  504. #if HAS_TEMP_BED
  505. _temp_error(e, PSTR(MSG_T_MINTEMP), e >= 0 ? PSTR(MSG_ERR_MINTEMP) : PSTR(MSG_ERR_MINTEMP_BED));
  506. #else
  507. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  508. #if HOTENDS == 1
  509. UNUSED(e);
  510. #endif
  511. #endif
  512. }
  513. float Temperature::get_pid_output(const int8_t e) {
  514. #if HOTENDS == 1
  515. UNUSED(e);
  516. #define _HOTEND_TEST true
  517. #else
  518. #define _HOTEND_TEST e == active_extruder
  519. #endif
  520. float pid_output;
  521. #if ENABLED(PIDTEMP)
  522. #if DISABLED(PID_OPENLOOP)
  523. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  524. dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
  525. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  526. #if HEATER_IDLE_HANDLER
  527. if (heater_idle_timeout_exceeded[HOTEND_INDEX]) {
  528. pid_output = 0;
  529. pid_reset[HOTEND_INDEX] = true;
  530. }
  531. else
  532. #endif
  533. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  534. pid_output = BANG_MAX;
  535. pid_reset[HOTEND_INDEX] = true;
  536. }
  537. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0
  538. #if HEATER_IDLE_HANDLER
  539. || heater_idle_timeout_exceeded[HOTEND_INDEX]
  540. #endif
  541. ) {
  542. pid_output = 0;
  543. pid_reset[HOTEND_INDEX] = true;
  544. }
  545. else {
  546. if (pid_reset[HOTEND_INDEX]) {
  547. temp_iState[HOTEND_INDEX] = 0.0;
  548. pid_reset[HOTEND_INDEX] = false;
  549. }
  550. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  551. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  552. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  553. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  554. #if ENABLED(PID_EXTRUSION_SCALING)
  555. cTerm[HOTEND_INDEX] = 0;
  556. if (_HOTEND_TEST) {
  557. long e_position = stepper.position(E_AXIS);
  558. if (e_position > last_e_position) {
  559. lpq[lpq_ptr] = e_position - last_e_position;
  560. last_e_position = e_position;
  561. }
  562. else {
  563. lpq[lpq_ptr] = 0;
  564. }
  565. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  566. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  567. pid_output += cTerm[HOTEND_INDEX];
  568. }
  569. #endif // PID_EXTRUSION_SCALING
  570. if (pid_output > PID_MAX) {
  571. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  572. pid_output = PID_MAX;
  573. }
  574. else if (pid_output < 0) {
  575. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  576. pid_output = 0;
  577. }
  578. }
  579. #else
  580. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  581. #endif // PID_OPENLOOP
  582. #if ENABLED(PID_DEBUG)
  583. SERIAL_ECHO_START();
  584. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  585. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  586. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  587. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  588. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  589. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  590. #if ENABLED(PID_EXTRUSION_SCALING)
  591. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  592. #endif
  593. SERIAL_EOL();
  594. #endif // PID_DEBUG
  595. #else /* PID off */
  596. #if HEATER_IDLE_HANDLER
  597. if (heater_idle_timeout_exceeded[HOTEND_INDEX])
  598. pid_output = 0;
  599. else
  600. #endif
  601. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  602. #endif
  603. return pid_output;
  604. }
  605. #if ENABLED(PIDTEMPBED)
  606. float Temperature::get_pid_output_bed() {
  607. float pid_output;
  608. #if DISABLED(PID_OPENLOOP)
  609. pid_error_bed = target_temperature_bed - current_temperature_bed;
  610. pTerm_bed = bedKp * pid_error_bed;
  611. temp_iState_bed += pid_error_bed;
  612. iTerm_bed = bedKi * temp_iState_bed;
  613. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  614. temp_dState_bed = current_temperature_bed;
  615. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  616. if (pid_output > MAX_BED_POWER) {
  617. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  618. pid_output = MAX_BED_POWER;
  619. }
  620. else if (pid_output < 0) {
  621. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  622. pid_output = 0;
  623. }
  624. #else
  625. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  626. #endif // PID_OPENLOOP
  627. #if ENABLED(PID_BED_DEBUG)
  628. SERIAL_ECHO_START();
  629. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  630. SERIAL_ECHOPGM(": Input ");
  631. SERIAL_ECHO(current_temperature_bed);
  632. SERIAL_ECHOPGM(" Output ");
  633. SERIAL_ECHO(pid_output);
  634. SERIAL_ECHOPGM(" pTerm ");
  635. SERIAL_ECHO(pTerm_bed);
  636. SERIAL_ECHOPGM(" iTerm ");
  637. SERIAL_ECHO(iTerm_bed);
  638. SERIAL_ECHOPGM(" dTerm ");
  639. SERIAL_ECHOLN(dTerm_bed);
  640. #endif // PID_BED_DEBUG
  641. return pid_output;
  642. }
  643. #endif // PIDTEMPBED
  644. /**
  645. * Manage heating activities for extruder hot-ends and a heated bed
  646. * - Acquire updated temperature readings
  647. * - Also resets the watchdog timer
  648. * - Invoke thermal runaway protection
  649. * - Manage extruder auto-fan
  650. * - Apply filament width to the extrusion rate (may move)
  651. * - Update the heated bed PID output value
  652. */
  653. /**
  654. * The following line SOMETIMES results in the dreaded "unable to find a register to spill in class 'POINTER_REGS'"
  655. * compile error.
  656. * 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);
  657. *
  658. * This is due to a bug in the C++ compiler used by the Arduino IDE from 1.6.10 to at least 1.8.1.
  659. *
  660. * The work around is to add the compiler flag "__attribute__((__optimize__("O2")))" to the declaration for manage_heater()
  661. */
  662. //void Temperature::manage_heater() __attribute__((__optimize__("O2")));
  663. void Temperature::manage_heater() {
  664. if (!temp_meas_ready) return;
  665. updateTemperaturesFromRawValues(); // also resets the watchdog
  666. #if ENABLED(HEATER_0_USES_MAX6675)
  667. if (current_temperature[0] > min(HEATER_0_MAXTEMP, MAX6675_TMAX - 1.0)) max_temp_error(0);
  668. if (current_temperature[0] < max(HEATER_0_MINTEMP, MAX6675_TMIN + .01)) min_temp_error(0);
  669. #endif
  670. #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN || HEATER_IDLE_HANDLER
  671. millis_t ms = millis();
  672. #endif
  673. HOTEND_LOOP() {
  674. #if HEATER_IDLE_HANDLER
  675. if (!heater_idle_timeout_exceeded[e] && heater_idle_timeout_ms[e] && ELAPSED(ms, heater_idle_timeout_ms[e]))
  676. heater_idle_timeout_exceeded[e] = true;
  677. #endif
  678. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  679. // Check for thermal runaway
  680. 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);
  681. #endif
  682. soft_pwm_amount[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)get_pid_output(e) >> 1 : 0;
  683. #if WATCH_HOTENDS
  684. // Make sure temperature is increasing
  685. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) { // Time to check this extruder?
  686. if (degHotend(e) < watch_target_temp[e]) // Failed to increase enough?
  687. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  688. else // Start again if the target is still far off
  689. start_watching_heater(e);
  690. }
  691. #endif
  692. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  693. // Make sure measured temperatures are close together
  694. if (FABS(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  695. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  696. #endif
  697. } // HOTEND_LOOP
  698. #if HAS_AUTO_FAN
  699. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  700. checkExtruderAutoFans();
  701. next_auto_fan_check_ms = ms + 2500UL;
  702. }
  703. #endif
  704. // Control the extruder rate based on the width sensor
  705. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  706. if (filament_sensor) {
  707. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  708. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  709. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  710. // Get the delayed info and add 100 to reconstitute to a percent of
  711. // the nominal filament diameter then square it to get an area
  712. const float vmroot = measurement_delay[meas_shift_index] * 0.01 + 1.0;
  713. planner.volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vmroot <= 0.1 ? 0.01 : sq(vmroot);
  714. planner.refresh_e_factor(FILAMENT_SENSOR_EXTRUDER_NUM);
  715. }
  716. #endif // FILAMENT_WIDTH_SENSOR
  717. #if WATCH_THE_BED
  718. // Make sure temperature is increasing
  719. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) { // Time to check the bed?
  720. if (degBed() < watch_target_bed_temp) // Failed to increase enough?
  721. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  722. else // Start again if the target is still far off
  723. start_watching_bed();
  724. }
  725. #endif // WATCH_THE_BED
  726. #if DISABLED(PIDTEMPBED)
  727. if (PENDING(ms, next_bed_check_ms)) return;
  728. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  729. #endif
  730. #if HAS_TEMP_BED
  731. #if HEATER_IDLE_HANDLER
  732. if (!bed_idle_timeout_exceeded && bed_idle_timeout_ms && ELAPSED(ms, bed_idle_timeout_ms))
  733. bed_idle_timeout_exceeded = true;
  734. #endif
  735. #if HAS_THERMALLY_PROTECTED_BED
  736. 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);
  737. #endif
  738. #if HEATER_IDLE_HANDLER
  739. if (bed_idle_timeout_exceeded)
  740. {
  741. soft_pwm_amount_bed = 0;
  742. #if DISABLED(PIDTEMPBED)
  743. WRITE_HEATER_BED(LOW);
  744. #endif
  745. }
  746. else
  747. #endif
  748. {
  749. #if ENABLED(PIDTEMPBED)
  750. soft_pwm_amount_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  751. #elif ENABLED(BED_LIMIT_SWITCHING)
  752. // Check if temperature is within the correct band
  753. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  754. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  755. soft_pwm_amount_bed = 0;
  756. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  757. soft_pwm_amount_bed = MAX_BED_POWER >> 1;
  758. }
  759. else {
  760. soft_pwm_amount_bed = 0;
  761. WRITE_HEATER_BED(LOW);
  762. }
  763. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  764. // Check if temperature is within the correct range
  765. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  766. soft_pwm_amount_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  767. }
  768. else {
  769. soft_pwm_amount_bed = 0;
  770. WRITE_HEATER_BED(LOW);
  771. }
  772. #endif
  773. }
  774. #endif // HAS_TEMP_BED
  775. }
  776. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  777. // Derived from RepRap FiveD extruder::getTemperature()
  778. // For hot end temperature measurement.
  779. float Temperature::analog2temp(int raw, uint8_t e) {
  780. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  781. if (e > HOTENDS)
  782. #else
  783. if (e >= HOTENDS)
  784. #endif
  785. {
  786. SERIAL_ERROR_START();
  787. SERIAL_ERROR((int)e);
  788. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  789. kill(PSTR(MSG_KILLED));
  790. return 0.0;
  791. }
  792. #if ENABLED(HEATER_0_USES_MAX6675)
  793. if (e == 0) return 0.25 * raw;
  794. #endif
  795. if (heater_ttbl_map[e] != NULL) {
  796. float celsius = 0;
  797. uint8_t i;
  798. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  799. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  800. if (PGM_RD_W((*tt)[i][0]) > raw) {
  801. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  802. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  803. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  804. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  805. break;
  806. }
  807. }
  808. // Overflow: Set to last value in the table
  809. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  810. return celsius;
  811. }
  812. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  813. }
  814. // Derived from RepRap FiveD extruder::getTemperature()
  815. // For bed temperature measurement.
  816. float Temperature::analog2tempBed(const int raw) {
  817. #if ENABLED(BED_USES_THERMISTOR)
  818. float celsius = 0;
  819. byte i;
  820. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  821. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  822. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  823. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  824. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  825. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  826. break;
  827. }
  828. }
  829. // Overflow: Set to last value in the table
  830. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  831. return celsius;
  832. #elif defined(BED_USES_AD595)
  833. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  834. #else
  835. UNUSED(raw);
  836. return 0;
  837. #endif
  838. }
  839. /**
  840. * Get the raw values into the actual temperatures.
  841. * The raw values are created in interrupt context,
  842. * and this function is called from normal context
  843. * as it would block the stepper routine.
  844. */
  845. void Temperature::updateTemperaturesFromRawValues() {
  846. #if ENABLED(HEATER_0_USES_MAX6675)
  847. current_temperature_raw[0] = read_max6675();
  848. #endif
  849. HOTEND_LOOP()
  850. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  851. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  852. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  853. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  854. #endif
  855. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  856. filament_width_meas = analog2widthFil();
  857. #endif
  858. #if ENABLED(USE_WATCHDOG)
  859. // Reset the watchdog after we know we have a temperature measurement.
  860. watchdog_reset();
  861. #endif
  862. CRITICAL_SECTION_START;
  863. temp_meas_ready = false;
  864. CRITICAL_SECTION_END;
  865. }
  866. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  867. // Convert raw Filament Width to millimeters
  868. float Temperature::analog2widthFil() {
  869. return current_raw_filwidth * 5.0 * (1.0 / 16383.0);
  870. //return current_raw_filwidth;
  871. }
  872. // Convert raw Filament Width to a ratio
  873. int Temperature::widthFil_to_size_ratio() {
  874. float temp = filament_width_meas;
  875. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  876. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  877. return filament_width_nominal / temp * 100;
  878. }
  879. #endif
  880. #if ENABLED(HEATER_0_USES_MAX6675)
  881. #ifndef MAX6675_SCK_PIN
  882. #define MAX6675_SCK_PIN SCK_PIN
  883. #endif
  884. #ifndef MAX6675_DO_PIN
  885. #define MAX6675_DO_PIN MISO_PIN
  886. #endif
  887. SPI<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  888. #endif
  889. /**
  890. * Initialize the temperature manager
  891. * The manager is implemented by periodic calls to manage_heater()
  892. */
  893. void Temperature::init() {
  894. #if MB(RUMBA) && (TEMP_SENSOR_0 == -1 || TEMP_SENSOR_1 == -1 || TEMP_SENSOR_2 == -1 || TEMP_SENSOR_BED == -1)
  895. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  896. MCUCR = _BV(JTD);
  897. MCUCR = _BV(JTD);
  898. #endif
  899. // Finish init of mult hotend arrays
  900. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  901. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  902. last_e_position = 0;
  903. #endif
  904. #if HAS_HEATER_0
  905. SET_OUTPUT(HEATER_0_PIN);
  906. #endif
  907. #if HAS_HEATER_1
  908. SET_OUTPUT(HEATER_1_PIN);
  909. #endif
  910. #if HAS_HEATER_2
  911. SET_OUTPUT(HEATER_2_PIN);
  912. #endif
  913. #if HAS_HEATER_3
  914. SET_OUTPUT(HEATER_3_PIN);
  915. #endif
  916. #if HAS_HEATER_4
  917. SET_OUTPUT(HEATER_3_PIN);
  918. #endif
  919. #if HAS_HEATER_BED
  920. SET_OUTPUT(HEATER_BED_PIN);
  921. #endif
  922. #if HAS_FAN0
  923. SET_OUTPUT(FAN_PIN);
  924. #if ENABLED(FAST_PWM_FAN)
  925. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  926. #endif
  927. #endif
  928. #if HAS_FAN1
  929. SET_OUTPUT(FAN1_PIN);
  930. #if ENABLED(FAST_PWM_FAN)
  931. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  932. #endif
  933. #endif
  934. #if HAS_FAN2
  935. SET_OUTPUT(FAN2_PIN);
  936. #if ENABLED(FAST_PWM_FAN)
  937. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  938. #endif
  939. #endif
  940. #if ENABLED(HEATER_0_USES_MAX6675)
  941. OUT_WRITE(SCK_PIN, LOW);
  942. OUT_WRITE(MOSI_PIN, HIGH);
  943. SET_INPUT_PULLUP(MISO_PIN);
  944. max6675_spi.init();
  945. OUT_WRITE(SS_PIN, HIGH);
  946. OUT_WRITE(MAX6675_SS, HIGH);
  947. #endif // HEATER_0_USES_MAX6675
  948. #ifdef DIDR2
  949. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  950. #else
  951. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  952. #endif
  953. // Set analog inputs
  954. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  955. DIDR0 = 0;
  956. #ifdef DIDR2
  957. DIDR2 = 0;
  958. #endif
  959. #if HAS_TEMP_0
  960. ANALOG_SELECT(TEMP_0_PIN);
  961. #endif
  962. #if HAS_TEMP_1
  963. ANALOG_SELECT(TEMP_1_PIN);
  964. #endif
  965. #if HAS_TEMP_2
  966. ANALOG_SELECT(TEMP_2_PIN);
  967. #endif
  968. #if HAS_TEMP_3
  969. ANALOG_SELECT(TEMP_3_PIN);
  970. #endif
  971. #if HAS_TEMP_4
  972. ANALOG_SELECT(TEMP_4_PIN);
  973. #endif
  974. #if HAS_TEMP_BED
  975. ANALOG_SELECT(TEMP_BED_PIN);
  976. #endif
  977. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  978. ANALOG_SELECT(FILWIDTH_PIN);
  979. #endif
  980. #if HAS_AUTO_FAN_0
  981. #if E0_AUTO_FAN_PIN == FAN1_PIN
  982. SET_OUTPUT(E0_AUTO_FAN_PIN);
  983. #if ENABLED(FAST_PWM_FAN)
  984. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  985. #endif
  986. #else
  987. SET_OUTPUT(E0_AUTO_FAN_PIN);
  988. #endif
  989. #endif
  990. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  991. #if E1_AUTO_FAN_PIN == FAN1_PIN
  992. SET_OUTPUT(E1_AUTO_FAN_PIN);
  993. #if ENABLED(FAST_PWM_FAN)
  994. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  995. #endif
  996. #else
  997. SET_OUTPUT(E1_AUTO_FAN_PIN);
  998. #endif
  999. #endif
  1000. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  1001. #if E2_AUTO_FAN_PIN == FAN1_PIN
  1002. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1003. #if ENABLED(FAST_PWM_FAN)
  1004. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1005. #endif
  1006. #else
  1007. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1008. #endif
  1009. #endif
  1010. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  1011. #if E3_AUTO_FAN_PIN == FAN1_PIN
  1012. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1013. #if ENABLED(FAST_PWM_FAN)
  1014. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1015. #endif
  1016. #else
  1017. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1018. #endif
  1019. #endif
  1020. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  1021. #if E4_AUTO_FAN_PIN == FAN1_PIN
  1022. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1023. #if ENABLED(FAST_PWM_FAN)
  1024. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1025. #endif
  1026. #else
  1027. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1028. #endif
  1029. #endif
  1030. // Use timer0 for temperature measurement
  1031. // Interleave temperature interrupt with millies interrupt
  1032. OCR0B = 128;
  1033. SBI(TIMSK0, OCIE0B);
  1034. // Wait for temperature measurement to settle
  1035. delay(250);
  1036. #define TEMP_MIN_ROUTINE(NR) \
  1037. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  1038. while (analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  1039. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1040. minttemp_raw[NR] += OVERSAMPLENR; \
  1041. else \
  1042. minttemp_raw[NR] -= OVERSAMPLENR; \
  1043. }
  1044. #define TEMP_MAX_ROUTINE(NR) \
  1045. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  1046. while (analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  1047. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1048. maxttemp_raw[NR] -= OVERSAMPLENR; \
  1049. else \
  1050. maxttemp_raw[NR] += OVERSAMPLENR; \
  1051. }
  1052. #ifdef HEATER_0_MINTEMP
  1053. TEMP_MIN_ROUTINE(0);
  1054. #endif
  1055. #ifdef HEATER_0_MAXTEMP
  1056. TEMP_MAX_ROUTINE(0);
  1057. #endif
  1058. #if HOTENDS > 1
  1059. #ifdef HEATER_1_MINTEMP
  1060. TEMP_MIN_ROUTINE(1);
  1061. #endif
  1062. #ifdef HEATER_1_MAXTEMP
  1063. TEMP_MAX_ROUTINE(1);
  1064. #endif
  1065. #if HOTENDS > 2
  1066. #ifdef HEATER_2_MINTEMP
  1067. TEMP_MIN_ROUTINE(2);
  1068. #endif
  1069. #ifdef HEATER_2_MAXTEMP
  1070. TEMP_MAX_ROUTINE(2);
  1071. #endif
  1072. #if HOTENDS > 3
  1073. #ifdef HEATER_3_MINTEMP
  1074. TEMP_MIN_ROUTINE(3);
  1075. #endif
  1076. #ifdef HEATER_3_MAXTEMP
  1077. TEMP_MAX_ROUTINE(3);
  1078. #endif
  1079. #if HOTENDS > 4
  1080. #ifdef HEATER_4_MINTEMP
  1081. TEMP_MIN_ROUTINE(4);
  1082. #endif
  1083. #ifdef HEATER_4_MAXTEMP
  1084. TEMP_MAX_ROUTINE(4);
  1085. #endif
  1086. #endif // HOTENDS > 4
  1087. #endif // HOTENDS > 3
  1088. #endif // HOTENDS > 2
  1089. #endif // HOTENDS > 1
  1090. #ifdef BED_MINTEMP
  1091. while (analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1092. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1093. bed_minttemp_raw += OVERSAMPLENR;
  1094. #else
  1095. bed_minttemp_raw -= OVERSAMPLENR;
  1096. #endif
  1097. }
  1098. #endif // BED_MINTEMP
  1099. #ifdef BED_MAXTEMP
  1100. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1101. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1102. bed_maxttemp_raw -= OVERSAMPLENR;
  1103. #else
  1104. bed_maxttemp_raw += OVERSAMPLENR;
  1105. #endif
  1106. }
  1107. #endif // BED_MAXTEMP
  1108. #if ENABLED(PROBING_HEATERS_OFF)
  1109. paused = false;
  1110. #endif
  1111. }
  1112. #if WATCH_HOTENDS
  1113. /**
  1114. * Start Heating Sanity Check for hotends that are below
  1115. * their target temperature by a configurable margin.
  1116. * This is called when the temperature is set. (M104, M109)
  1117. */
  1118. void Temperature::start_watching_heater(uint8_t e) {
  1119. #if HOTENDS == 1
  1120. UNUSED(e);
  1121. #endif
  1122. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1123. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1124. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1125. }
  1126. else
  1127. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1128. }
  1129. #endif
  1130. #if WATCH_THE_BED
  1131. /**
  1132. * Start Heating Sanity Check for hotends that are below
  1133. * their target temperature by a configurable margin.
  1134. * This is called when the temperature is set. (M140, M190)
  1135. */
  1136. void Temperature::start_watching_bed() {
  1137. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1138. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1139. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1140. }
  1141. else
  1142. watch_bed_next_ms = 0;
  1143. }
  1144. #endif
  1145. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1146. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1147. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1148. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1149. #endif
  1150. #if HAS_THERMALLY_PROTECTED_BED
  1151. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1152. millis_t Temperature::thermal_runaway_bed_timer;
  1153. #endif
  1154. void Temperature::thermal_runaway_protection(Temperature::TRState* state, millis_t* timer, float current, float target, int heater_id, int period_seconds, int hysteresis_degc) {
  1155. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1156. /**
  1157. SERIAL_ECHO_START();
  1158. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1159. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1160. SERIAL_ECHOPAIR(" ; State:", *state);
  1161. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1162. SERIAL_ECHOPAIR(" ; Temperature:", current);
  1163. SERIAL_ECHOPAIR(" ; Target Temp:", target);
  1164. if (heater_id >= 0)
  1165. SERIAL_ECHOPAIR(" ; Idle Timeout:", heater_idle_timeout_exceeded[heater_id]);
  1166. else
  1167. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle_timeout_exceeded);
  1168. SERIAL_EOL();
  1169. */
  1170. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1171. #if HEATER_IDLE_HANDLER
  1172. // If the heater idle timeout expires, restart
  1173. if (heater_id >= 0 && heater_idle_timeout_exceeded[heater_id]) {
  1174. *state = TRInactive;
  1175. tr_target_temperature[heater_index] = 0;
  1176. }
  1177. #if HAS_TEMP_BED
  1178. else if (heater_id < 0 && bed_idle_timeout_exceeded) {
  1179. *state = TRInactive;
  1180. tr_target_temperature[heater_index] = 0;
  1181. }
  1182. #endif
  1183. else
  1184. #endif
  1185. // If the target temperature changes, restart
  1186. if (tr_target_temperature[heater_index] != target) {
  1187. tr_target_temperature[heater_index] = target;
  1188. *state = target > 0 ? TRFirstHeating : TRInactive;
  1189. }
  1190. switch (*state) {
  1191. // Inactive state waits for a target temperature to be set
  1192. case TRInactive: break;
  1193. // When first heating, wait for the temperature to be reached then go to Stable state
  1194. case TRFirstHeating:
  1195. if (current < tr_target_temperature[heater_index]) break;
  1196. *state = TRStable;
  1197. // While the temperature is stable watch for a bad temperature
  1198. case TRStable:
  1199. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1200. *timer = millis() + period_seconds * 1000UL;
  1201. break;
  1202. }
  1203. else if (PENDING(millis(), *timer)) break;
  1204. *state = TRRunaway;
  1205. case TRRunaway:
  1206. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1207. }
  1208. }
  1209. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1210. void Temperature::disable_all_heaters() {
  1211. #if ENABLED(AUTOTEMP)
  1212. planner.autotemp_enabled = false;
  1213. #endif
  1214. HOTEND_LOOP() setTargetHotend(0, e);
  1215. setTargetBed(0);
  1216. // Unpause and reset everything
  1217. #if ENABLED(PROBING_HEATERS_OFF)
  1218. pause(false);
  1219. #endif
  1220. // If all heaters go down then for sure our print job has stopped
  1221. print_job_timer.stop();
  1222. #define DISABLE_HEATER(NR) { \
  1223. setTargetHotend(0, NR); \
  1224. soft_pwm_amount[NR] = 0; \
  1225. WRITE_HEATER_ ##NR (LOW); \
  1226. }
  1227. #if HAS_TEMP_HOTEND
  1228. DISABLE_HEATER(0);
  1229. #if HOTENDS > 1
  1230. DISABLE_HEATER(1);
  1231. #if HOTENDS > 2
  1232. DISABLE_HEATER(2);
  1233. #if HOTENDS > 3
  1234. DISABLE_HEATER(3);
  1235. #if HOTENDS > 4
  1236. DISABLE_HEATER(4);
  1237. #endif // HOTENDS > 4
  1238. #endif // HOTENDS > 3
  1239. #endif // HOTENDS > 2
  1240. #endif // HOTENDS > 1
  1241. #endif
  1242. #if HAS_TEMP_BED
  1243. target_temperature_bed = 0;
  1244. soft_pwm_amount_bed = 0;
  1245. #if HAS_HEATER_BED
  1246. WRITE_HEATER_BED(LOW);
  1247. #endif
  1248. #endif
  1249. }
  1250. #if ENABLED(PROBING_HEATERS_OFF)
  1251. void Temperature::pause(const bool p) {
  1252. if (p != paused) {
  1253. paused = p;
  1254. if (p) {
  1255. HOTEND_LOOP() start_heater_idle_timer(e, 0); // timeout immediately
  1256. #if HAS_TEMP_BED
  1257. start_bed_idle_timer(0); // timeout immediately
  1258. #endif
  1259. }
  1260. else {
  1261. HOTEND_LOOP() reset_heater_idle_timer(e);
  1262. #if HAS_TEMP_BED
  1263. reset_bed_idle_timer();
  1264. #endif
  1265. }
  1266. }
  1267. }
  1268. #endif // PROBING_HEATERS_OFF
  1269. #if ENABLED(HEATER_0_USES_MAX6675)
  1270. #define MAX6675_HEAT_INTERVAL 250u
  1271. #if ENABLED(MAX6675_IS_MAX31855)
  1272. uint32_t max6675_temp = 2000;
  1273. #define MAX6675_ERROR_MASK 7
  1274. #define MAX6675_DISCARD_BITS 18
  1275. #define MAX6675_SPEED_BITS (_BV(SPR1)) // clock ÷ 64
  1276. #else
  1277. uint16_t max6675_temp = 2000;
  1278. #define MAX6675_ERROR_MASK 4
  1279. #define MAX6675_DISCARD_BITS 3
  1280. #define MAX6675_SPEED_BITS (_BV(SPR0)) // clock ÷ 16
  1281. #endif
  1282. int Temperature::read_max6675() {
  1283. static millis_t next_max6675_ms = 0;
  1284. millis_t ms = millis();
  1285. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1286. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1287. CBI(
  1288. #ifdef PRR
  1289. PRR
  1290. #elif defined(PRR0)
  1291. PRR0
  1292. #endif
  1293. , PRSPI);
  1294. SPCR = _BV(MSTR) | _BV(SPE) | MAX6675_SPEED_BITS;
  1295. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1296. // ensure 100ns delay - a bit extra is fine
  1297. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1298. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1299. // Read a big-endian temperature value
  1300. max6675_temp = 0;
  1301. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1302. max6675_temp |= max6675_spi.receive();
  1303. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1304. }
  1305. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1306. if (max6675_temp & MAX6675_ERROR_MASK) {
  1307. SERIAL_ERROR_START();
  1308. SERIAL_ERRORPGM("Temp measurement error! ");
  1309. #if MAX6675_ERROR_MASK == 7
  1310. SERIAL_ERRORPGM("MAX31855 ");
  1311. if (max6675_temp & 1)
  1312. SERIAL_ERRORLNPGM("Open Circuit");
  1313. else if (max6675_temp & 2)
  1314. SERIAL_ERRORLNPGM("Short to GND");
  1315. else if (max6675_temp & 4)
  1316. SERIAL_ERRORLNPGM("Short to VCC");
  1317. #else
  1318. SERIAL_ERRORLNPGM("MAX6675");
  1319. #endif
  1320. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1321. }
  1322. else
  1323. max6675_temp >>= MAX6675_DISCARD_BITS;
  1324. #if ENABLED(MAX6675_IS_MAX31855)
  1325. // Support negative temperature
  1326. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000;
  1327. #endif
  1328. return (int)max6675_temp;
  1329. }
  1330. #endif // HEATER_0_USES_MAX6675
  1331. /**
  1332. * Get raw temperatures
  1333. */
  1334. void Temperature::set_current_temp_raw() {
  1335. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1336. current_temperature_raw[0] = raw_temp_value[0];
  1337. #endif
  1338. #if HAS_TEMP_1
  1339. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1340. redundant_temperature_raw = raw_temp_value[1];
  1341. #else
  1342. current_temperature_raw[1] = raw_temp_value[1];
  1343. #endif
  1344. #if HAS_TEMP_2
  1345. current_temperature_raw[2] = raw_temp_value[2];
  1346. #if HAS_TEMP_3
  1347. current_temperature_raw[3] = raw_temp_value[3];
  1348. #if HAS_TEMP_4
  1349. current_temperature_raw[4] = raw_temp_value[4];
  1350. #endif
  1351. #endif
  1352. #endif
  1353. #endif
  1354. current_temperature_bed_raw = raw_temp_bed_value;
  1355. temp_meas_ready = true;
  1356. }
  1357. #if ENABLED(PINS_DEBUGGING)
  1358. /**
  1359. * monitors endstops & Z probe for changes
  1360. *
  1361. * If a change is detected then the LED is toggled and
  1362. * a message is sent out the serial port
  1363. *
  1364. * Yes, we could miss a rapid back & forth change but
  1365. * that won't matter because this is all manual.
  1366. *
  1367. */
  1368. void endstop_monitor() {
  1369. static uint16_t old_endstop_bits_local = 0;
  1370. static uint8_t local_LED_status = 0;
  1371. uint16_t current_endstop_bits_local = 0;
  1372. #if HAS_X_MIN
  1373. if (READ(X_MIN_PIN)) SBI(current_endstop_bits_local, X_MIN);
  1374. #endif
  1375. #if HAS_X_MAX
  1376. if (READ(X_MAX_PIN)) SBI(current_endstop_bits_local, X_MAX);
  1377. #endif
  1378. #if HAS_Y_MIN
  1379. if (READ(Y_MIN_PIN)) SBI(current_endstop_bits_local, Y_MIN);
  1380. #endif
  1381. #if HAS_Y_MAX
  1382. if (READ(Y_MAX_PIN)) SBI(current_endstop_bits_local, Y_MAX);
  1383. #endif
  1384. #if HAS_Z_MIN
  1385. if (READ(Z_MIN_PIN)) SBI(current_endstop_bits_local, Z_MIN);
  1386. #endif
  1387. #if HAS_Z_MAX
  1388. if (READ(Z_MAX_PIN)) SBI(current_endstop_bits_local, Z_MAX);
  1389. #endif
  1390. #if HAS_Z_MIN_PROBE_PIN
  1391. if (READ(Z_MIN_PROBE_PIN)) SBI(current_endstop_bits_local, Z_MIN_PROBE);
  1392. #endif
  1393. #if HAS_Z2_MIN
  1394. if (READ(Z2_MIN_PIN)) SBI(current_endstop_bits_local, Z2_MIN);
  1395. #endif
  1396. #if HAS_Z2_MAX
  1397. if (READ(Z2_MAX_PIN)) SBI(current_endstop_bits_local, Z2_MAX);
  1398. #endif
  1399. uint16_t endstop_change = current_endstop_bits_local ^ old_endstop_bits_local;
  1400. if (endstop_change) {
  1401. #if HAS_X_MIN
  1402. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR(" X_MIN:", !!TEST(current_endstop_bits_local, X_MIN));
  1403. #endif
  1404. #if HAS_X_MAX
  1405. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", !!TEST(current_endstop_bits_local, X_MAX));
  1406. #endif
  1407. #if HAS_Y_MIN
  1408. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", !!TEST(current_endstop_bits_local, Y_MIN));
  1409. #endif
  1410. #if HAS_Y_MAX
  1411. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", !!TEST(current_endstop_bits_local, Y_MAX));
  1412. #endif
  1413. #if HAS_Z_MIN
  1414. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", !!TEST(current_endstop_bits_local, Z_MIN));
  1415. #endif
  1416. #if HAS_Z_MAX
  1417. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", !!TEST(current_endstop_bits_local, Z_MAX));
  1418. #endif
  1419. #if HAS_Z_MIN_PROBE_PIN
  1420. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", !!TEST(current_endstop_bits_local, Z_MIN_PROBE));
  1421. #endif
  1422. #if HAS_Z2_MIN
  1423. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", !!TEST(current_endstop_bits_local, Z2_MIN));
  1424. #endif
  1425. #if HAS_Z2_MAX
  1426. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", !!TEST(current_endstop_bits_local, Z2_MAX));
  1427. #endif
  1428. SERIAL_PROTOCOLPGM("\n\n");
  1429. analogWrite(LED_PIN, local_LED_status);
  1430. local_LED_status ^= 255;
  1431. old_endstop_bits_local = current_endstop_bits_local;
  1432. }
  1433. }
  1434. #endif // PINS_DEBUGGING
  1435. /**
  1436. * Timer 0 is shared with millies so don't change the prescaler.
  1437. *
  1438. * This ISR uses the compare method so it runs at the base
  1439. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1440. * in OCR0B above (128 or halfway between OVFs).
  1441. *
  1442. * - Manage PWM to all the heaters and fan
  1443. * - Prepare or Measure one of the raw ADC sensor values
  1444. * - Check new temperature values for MIN/MAX errors (kill on error)
  1445. * - Step the babysteps value for each axis towards 0
  1446. * - For PINS_DEBUGGING, monitor and report endstop pins
  1447. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1448. */
  1449. ISR(TIMER0_COMPB_vect) { Temperature::isr(); }
  1450. volatile bool Temperature::in_temp_isr = false;
  1451. void Temperature::isr() {
  1452. // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR
  1453. // at the end of its run, potentially causing re-entry. This flag prevents it.
  1454. if (in_temp_isr) return;
  1455. in_temp_isr = true;
  1456. // Allow UART and stepper ISRs
  1457. CBI(TIMSK0, OCIE0B); //Disable Temperature ISR
  1458. sei();
  1459. static int8_t temp_count = -1;
  1460. static ADCSensorState adc_sensor_state = StartupDelay;
  1461. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1462. // avoid multiple loads of pwm_count
  1463. uint8_t pwm_count_tmp = pwm_count;
  1464. #if ENABLED(ADC_KEYPAD)
  1465. static unsigned int raw_ADCKey_value = 0;
  1466. #endif
  1467. // Static members for each heater
  1468. #if ENABLED(SLOW_PWM_HEATERS)
  1469. static uint8_t slow_pwm_count = 0;
  1470. #define ISR_STATICS(n) \
  1471. static uint8_t soft_pwm_count_ ## n, \
  1472. state_heater_ ## n = 0, \
  1473. state_timer_heater_ ## n = 0
  1474. #else
  1475. #define ISR_STATICS(n) static uint8_t soft_pwm_count_ ## n = 0
  1476. #endif
  1477. // Statics per heater
  1478. ISR_STATICS(0);
  1479. #if HOTENDS > 1
  1480. ISR_STATICS(1);
  1481. #if HOTENDS > 2
  1482. ISR_STATICS(2);
  1483. #if HOTENDS > 3
  1484. ISR_STATICS(3);
  1485. #if HOTENDS > 4
  1486. ISR_STATICS(4);
  1487. #endif // HOTENDS > 4
  1488. #endif // HOTENDS > 3
  1489. #endif // HOTENDS > 2
  1490. #endif // HOTENDS > 1
  1491. #if HAS_HEATER_BED
  1492. ISR_STATICS(BED);
  1493. #endif
  1494. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1495. static unsigned long raw_filwidth_value = 0;
  1496. #endif
  1497. #if DISABLED(SLOW_PWM_HEATERS)
  1498. constexpr uint8_t pwm_mask =
  1499. #if ENABLED(SOFT_PWM_DITHER)
  1500. _BV(SOFT_PWM_SCALE) - 1
  1501. #else
  1502. 0
  1503. #endif
  1504. ;
  1505. /**
  1506. * Standard PWM modulation
  1507. */
  1508. if (pwm_count_tmp >= 127) {
  1509. pwm_count_tmp -= 127;
  1510. soft_pwm_count_0 = (soft_pwm_count_0 & pwm_mask) + soft_pwm_amount[0];
  1511. WRITE_HEATER_0(soft_pwm_count_0 > pwm_mask ? HIGH : LOW);
  1512. #if HOTENDS > 1
  1513. soft_pwm_count_1 = (soft_pwm_count_1 & pwm_mask) + soft_pwm_amount[1];
  1514. WRITE_HEATER_1(soft_pwm_count_1 > pwm_mask ? HIGH : LOW);
  1515. #if HOTENDS > 2
  1516. soft_pwm_count_2 = (soft_pwm_count_2 & pwm_mask) + soft_pwm_amount[2];
  1517. WRITE_HEATER_2(soft_pwm_count_2 > pwm_mask ? HIGH : LOW);
  1518. #if HOTENDS > 3
  1519. soft_pwm_count_3 = (soft_pwm_count_3 & pwm_mask) + soft_pwm_amount[3];
  1520. WRITE_HEATER_3(soft_pwm_count_3 > pwm_mask ? HIGH : LOW);
  1521. #if HOTENDS > 4
  1522. soft_pwm_count_4 = (soft_pwm_count_4 & pwm_mask) + soft_pwm_amount[4];
  1523. WRITE_HEATER_4(soft_pwm_count_4 > pwm_mask ? HIGH : LOW);
  1524. #endif // HOTENDS > 4
  1525. #endif // HOTENDS > 3
  1526. #endif // HOTENDS > 2
  1527. #endif // HOTENDS > 1
  1528. #if HAS_HEATER_BED
  1529. soft_pwm_count_BED = (soft_pwm_count_BED & pwm_mask) + soft_pwm_amount_bed;
  1530. WRITE_HEATER_BED(soft_pwm_count_BED > pwm_mask ? HIGH : LOW);
  1531. #endif
  1532. #if ENABLED(FAN_SOFT_PWM)
  1533. #if HAS_FAN0
  1534. soft_pwm_count_fan[0] = (soft_pwm_count_fan[0] & pwm_mask) + (soft_pwm_amount_fan[0] >> 1);
  1535. WRITE_FAN(soft_pwm_count_fan[0] > pwm_mask ? HIGH : LOW);
  1536. #endif
  1537. #if HAS_FAN1
  1538. soft_pwm_count_fan[1] = (soft_pwm_count_fan[1] & pwm_mask) + (soft_pwm_amount_fan[1] >> 1);
  1539. WRITE_FAN1(soft_pwm_count_fan[1] > pwm_mask ? HIGH : LOW);
  1540. #endif
  1541. #if HAS_FAN2
  1542. soft_pwm_count_fan[2] = (soft_pwm_count_fan[2] & pwm_mask) + (soft_pwm_amount_fan[2] >> 1);
  1543. WRITE_FAN2(soft_pwm_count_fan[2] > pwm_mask ? HIGH : LOW);
  1544. #endif
  1545. #endif
  1546. }
  1547. else {
  1548. if (soft_pwm_count_0 <= pwm_count_tmp) WRITE_HEATER_0(LOW);
  1549. #if HOTENDS > 1
  1550. if (soft_pwm_count_1 <= pwm_count_tmp) WRITE_HEATER_1(LOW);
  1551. #if HOTENDS > 2
  1552. if (soft_pwm_count_2 <= pwm_count_tmp) WRITE_HEATER_2(LOW);
  1553. #if HOTENDS > 3
  1554. if (soft_pwm_count_3 <= pwm_count_tmp) WRITE_HEATER_3(LOW);
  1555. #if HOTENDS > 4
  1556. if (soft_pwm_count_4 <= pwm_count_tmp) WRITE_HEATER_4(LOW);
  1557. #endif // HOTENDS > 4
  1558. #endif // HOTENDS > 3
  1559. #endif // HOTENDS > 2
  1560. #endif // HOTENDS > 1
  1561. #if HAS_HEATER_BED
  1562. if (soft_pwm_count_BED <= pwm_count_tmp) WRITE_HEATER_BED(LOW);
  1563. #endif
  1564. #if ENABLED(FAN_SOFT_PWM)
  1565. #if HAS_FAN0
  1566. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1567. #endif
  1568. #if HAS_FAN1
  1569. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1570. #endif
  1571. #if HAS_FAN2
  1572. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1573. #endif
  1574. #endif
  1575. }
  1576. // SOFT_PWM_SCALE to frequency:
  1577. //
  1578. // 0: 16000000/64/256/128 = 7.6294 Hz
  1579. // 1: / 64 = 15.2588 Hz
  1580. // 2: / 32 = 30.5176 Hz
  1581. // 3: / 16 = 61.0352 Hz
  1582. // 4: / 8 = 122.0703 Hz
  1583. // 5: / 4 = 244.1406 Hz
  1584. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1585. #else // SLOW_PWM_HEATERS
  1586. /**
  1587. * SLOW PWM HEATERS
  1588. *
  1589. * For relay-driven heaters
  1590. */
  1591. #ifndef MIN_STATE_TIME
  1592. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1593. #endif
  1594. // Macros for Slow PWM timer logic
  1595. #define _SLOW_PWM_ROUTINE(NR, src) \
  1596. soft_pwm_count_ ##NR = src; \
  1597. if (soft_pwm_count_ ##NR > 0) { \
  1598. if (state_timer_heater_ ##NR == 0) { \
  1599. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1600. state_heater_ ##NR = 1; \
  1601. WRITE_HEATER_ ##NR(1); \
  1602. } \
  1603. } \
  1604. else { \
  1605. if (state_timer_heater_ ##NR == 0) { \
  1606. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1607. state_heater_ ##NR = 0; \
  1608. WRITE_HEATER_ ##NR(0); \
  1609. } \
  1610. }
  1611. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm_amount[n])
  1612. #define PWM_OFF_ROUTINE(NR) \
  1613. if (soft_pwm_count_ ##NR < slow_pwm_count) { \
  1614. if (state_timer_heater_ ##NR == 0) { \
  1615. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1616. state_heater_ ##NR = 0; \
  1617. WRITE_HEATER_ ##NR (0); \
  1618. } \
  1619. }
  1620. if (slow_pwm_count == 0) {
  1621. SLOW_PWM_ROUTINE(0);
  1622. #if HOTENDS > 1
  1623. SLOW_PWM_ROUTINE(1);
  1624. #if HOTENDS > 2
  1625. SLOW_PWM_ROUTINE(2);
  1626. #if HOTENDS > 3
  1627. SLOW_PWM_ROUTINE(3);
  1628. #if HOTENDS > 4
  1629. SLOW_PWM_ROUTINE(4);
  1630. #endif // HOTENDS > 4
  1631. #endif // HOTENDS > 3
  1632. #endif // HOTENDS > 2
  1633. #endif // HOTENDS > 1
  1634. #if HAS_HEATER_BED
  1635. _SLOW_PWM_ROUTINE(BED, soft_pwm_amount_bed); // BED
  1636. #endif
  1637. } // slow_pwm_count == 0
  1638. PWM_OFF_ROUTINE(0);
  1639. #if HOTENDS > 1
  1640. PWM_OFF_ROUTINE(1);
  1641. #if HOTENDS > 2
  1642. PWM_OFF_ROUTINE(2);
  1643. #if HOTENDS > 3
  1644. PWM_OFF_ROUTINE(3);
  1645. #if HOTENDS > 4
  1646. PWM_OFF_ROUTINE(4);
  1647. #endif // HOTENDS > 4
  1648. #endif // HOTENDS > 3
  1649. #endif // HOTENDS > 2
  1650. #endif // HOTENDS > 1
  1651. #if HAS_HEATER_BED
  1652. PWM_OFF_ROUTINE(BED); // BED
  1653. #endif
  1654. #if ENABLED(FAN_SOFT_PWM)
  1655. if (pwm_count_tmp >= 127) {
  1656. pwm_count_tmp = 0;
  1657. #if HAS_FAN0
  1658. soft_pwm_count_fan[0] = soft_pwm_amount_fan[0] >> 1;
  1659. WRITE_FAN(soft_pwm_count_fan[0] > 0 ? HIGH : LOW);
  1660. #endif
  1661. #if HAS_FAN1
  1662. soft_pwm_count_fan[1] = soft_pwm_amount_fan[1] >> 1;
  1663. WRITE_FAN1(soft_pwm_count_fan[1] > 0 ? HIGH : LOW);
  1664. #endif
  1665. #if HAS_FAN2
  1666. soft_pwm_count_fan[2] = soft_pwm_amount_fan[2] >> 1;
  1667. WRITE_FAN2(soft_pwm_count_fan[2] > 0 ? HIGH : LOW);
  1668. #endif
  1669. }
  1670. #if HAS_FAN0
  1671. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1672. #endif
  1673. #if HAS_FAN1
  1674. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1675. #endif
  1676. #if HAS_FAN2
  1677. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1678. #endif
  1679. #endif // FAN_SOFT_PWM
  1680. // SOFT_PWM_SCALE to frequency:
  1681. //
  1682. // 0: 16000000/64/256/128 = 7.6294 Hz
  1683. // 1: / 64 = 15.2588 Hz
  1684. // 2: / 32 = 30.5176 Hz
  1685. // 3: / 16 = 61.0352 Hz
  1686. // 4: / 8 = 122.0703 Hz
  1687. // 5: / 4 = 244.1406 Hz
  1688. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1689. // increment slow_pwm_count only every 64th pwm_count,
  1690. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1691. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1692. slow_pwm_count++;
  1693. slow_pwm_count &= 0x7F;
  1694. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1695. #if HOTENDS > 1
  1696. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1697. #if HOTENDS > 2
  1698. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1699. #if HOTENDS > 3
  1700. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1701. #if HOTENDS > 4
  1702. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1703. #endif // HOTENDS > 4
  1704. #endif // HOTENDS > 3
  1705. #endif // HOTENDS > 2
  1706. #endif // HOTENDS > 1
  1707. #if HAS_HEATER_BED
  1708. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1709. #endif
  1710. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1711. #endif // SLOW_PWM_HEATERS
  1712. //
  1713. // Update lcd buttons 488 times per second
  1714. //
  1715. static bool do_buttons;
  1716. if ((do_buttons ^= true)) lcd_buttons_update();
  1717. /**
  1718. * One sensor is sampled on every other call of the ISR.
  1719. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1720. *
  1721. * On each Prepare pass, ADC is started for a sensor pin.
  1722. * On the next pass, the ADC value is read and accumulated.
  1723. *
  1724. * This gives each ADC 0.9765ms to charge up.
  1725. */
  1726. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1727. #ifdef MUX5
  1728. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1729. #else
  1730. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1731. #endif
  1732. switch (adc_sensor_state) {
  1733. case SensorsReady: {
  1734. // All sensors have been read. Stay in this state for a few
  1735. // ISRs to save on calls to temp update/checking code below.
  1736. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  1737. static uint8_t delay_count = 0;
  1738. if (extra_loops > 0) {
  1739. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1740. if (--delay_count) // While delaying...
  1741. adc_sensor_state = (ADCSensorState)(int(SensorsReady) - 1); // retain this state (else, next state will be 0)
  1742. break;
  1743. }
  1744. else
  1745. adc_sensor_state = (ADCSensorState)0; // Fall-through to start first sensor now
  1746. }
  1747. #if HAS_TEMP_0
  1748. case PrepareTemp_0:
  1749. START_ADC(TEMP_0_PIN);
  1750. break;
  1751. case MeasureTemp_0:
  1752. raw_temp_value[0] += ADC;
  1753. break;
  1754. #endif
  1755. #if HAS_TEMP_BED
  1756. case PrepareTemp_BED:
  1757. START_ADC(TEMP_BED_PIN);
  1758. break;
  1759. case MeasureTemp_BED:
  1760. raw_temp_bed_value += ADC;
  1761. break;
  1762. #endif
  1763. #if HAS_TEMP_1
  1764. case PrepareTemp_1:
  1765. START_ADC(TEMP_1_PIN);
  1766. break;
  1767. case MeasureTemp_1:
  1768. raw_temp_value[1] += ADC;
  1769. break;
  1770. #endif
  1771. #if HAS_TEMP_2
  1772. case PrepareTemp_2:
  1773. START_ADC(TEMP_2_PIN);
  1774. break;
  1775. case MeasureTemp_2:
  1776. raw_temp_value[2] += ADC;
  1777. break;
  1778. #endif
  1779. #if HAS_TEMP_3
  1780. case PrepareTemp_3:
  1781. START_ADC(TEMP_3_PIN);
  1782. break;
  1783. case MeasureTemp_3:
  1784. raw_temp_value[3] += ADC;
  1785. break;
  1786. #endif
  1787. #if HAS_TEMP_4
  1788. case PrepareTemp_4:
  1789. START_ADC(TEMP_4_PIN);
  1790. break;
  1791. case MeasureTemp_4:
  1792. raw_temp_value[4] += ADC;
  1793. break;
  1794. #endif
  1795. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1796. case Prepare_FILWIDTH:
  1797. START_ADC(FILWIDTH_PIN);
  1798. break;
  1799. case Measure_FILWIDTH:
  1800. if (ADC > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1801. raw_filwidth_value -= (raw_filwidth_value >> 7); // Subtract 1/128th of the raw_filwidth_value
  1802. raw_filwidth_value += ((unsigned long)ADC << 7); // Add new ADC reading, scaled by 128
  1803. }
  1804. break;
  1805. #endif
  1806. #if ENABLED(ADC_KEYPAD)
  1807. case Prepare_ADC_KEY:
  1808. START_ADC(ADC_KEYPAD_PIN);
  1809. break;
  1810. case Measure_ADC_KEY:
  1811. if (ADCKey_count < 16) {
  1812. raw_ADCKey_value = ADC;
  1813. if (raw_ADCKey_value > 900) {
  1814. //ADC Key release
  1815. ADCKey_count = 0;
  1816. current_ADCKey_raw = 0;
  1817. }
  1818. else {
  1819. current_ADCKey_raw += raw_ADCKey_value;
  1820. ADCKey_count++;
  1821. }
  1822. }
  1823. break;
  1824. #endif // ADC_KEYPAD
  1825. case StartupDelay: break;
  1826. } // switch(adc_sensor_state)
  1827. if (!adc_sensor_state && ++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1828. temp_count = 0;
  1829. // Update the raw values if they've been read. Else we could be updating them during reading.
  1830. if (!temp_meas_ready) set_current_temp_raw();
  1831. // Filament Sensor - can be read any time since IIR filtering is used
  1832. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1833. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1834. #endif
  1835. ZERO(raw_temp_value);
  1836. raw_temp_bed_value = 0;
  1837. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1838. int constexpr temp_dir[] = {
  1839. #if ENABLED(HEATER_0_USES_MAX6675)
  1840. 0
  1841. #else
  1842. TEMPDIR(0)
  1843. #endif
  1844. #if HOTENDS > 1
  1845. , TEMPDIR(1)
  1846. #if HOTENDS > 2
  1847. , TEMPDIR(2)
  1848. #if HOTENDS > 3
  1849. , TEMPDIR(3)
  1850. #if HOTENDS > 4
  1851. , TEMPDIR(4)
  1852. #endif // HOTENDS > 4
  1853. #endif // HOTENDS > 3
  1854. #endif // HOTENDS > 2
  1855. #endif // HOTENDS > 1
  1856. };
  1857. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1858. const int16_t tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1859. const bool heater_on = 0 <
  1860. #if ENABLED(PIDTEMP)
  1861. soft_pwm_amount[e]
  1862. #else
  1863. target_temperature[e]
  1864. #endif
  1865. ;
  1866. if (rawtemp > maxttemp_raw[e] * tdir && heater_on) max_temp_error(e);
  1867. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && heater_on) {
  1868. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1869. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1870. #endif
  1871. min_temp_error(e);
  1872. }
  1873. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1874. else
  1875. consecutive_low_temperature_error[e] = 0;
  1876. #endif
  1877. }
  1878. #if HAS_TEMP_BED
  1879. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1880. #define GEBED <=
  1881. #else
  1882. #define GEBED >=
  1883. #endif
  1884. const bool bed_on = 0 <
  1885. #if ENABLED(PIDTEMPBED)
  1886. soft_pwm_amount_bed
  1887. #else
  1888. target_temperature_bed
  1889. #endif
  1890. ;
  1891. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && bed_on) max_temp_error(-1);
  1892. if (bed_minttemp_raw GEBED current_temperature_bed_raw && bed_on) min_temp_error(-1);
  1893. #endif
  1894. } // temp_count >= OVERSAMPLENR
  1895. // Go to the next state, up to SensorsReady
  1896. adc_sensor_state = (ADCSensorState)(int(adc_sensor_state) + 1);
  1897. if (adc_sensor_state > SensorsReady) adc_sensor_state = (ADCSensorState)0;
  1898. #if ENABLED(BABYSTEPPING)
  1899. LOOP_XYZ(axis) {
  1900. const int curTodo = babystepsTodo[axis]; // get rid of volatile for performance
  1901. if (curTodo) {
  1902. stepper.babystep((AxisEnum)axis, curTodo > 0);
  1903. if (curTodo > 0) babystepsTodo[axis]--;
  1904. else babystepsTodo[axis]++;
  1905. }
  1906. }
  1907. #endif // BABYSTEPPING
  1908. #if ENABLED(PINS_DEBUGGING)
  1909. extern bool endstop_monitor_flag;
  1910. // run the endstop monitor at 15Hz
  1911. static uint8_t endstop_monitor_count = 16; // offset this check from the others
  1912. if (endstop_monitor_flag) {
  1913. endstop_monitor_count += _BV(1); // 15 Hz
  1914. endstop_monitor_count &= 0x7F;
  1915. if (!endstop_monitor_count) endstop_monitor(); // report changes in endstop status
  1916. }
  1917. #endif
  1918. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1919. extern volatile uint8_t e_hit;
  1920. if (e_hit && ENDSTOPS_ENABLED) {
  1921. endstops.update(); // call endstop update routine
  1922. e_hit--;
  1923. }
  1924. #endif
  1925. cli();
  1926. in_temp_isr = false;
  1927. SBI(TIMSK0, OCIE0B); //re-enable Temperature ISR
  1928. }