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

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