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 71KB

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