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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 "endstops.h"
  27. #include "../MarlinCore.h"
  28. #include "../lcd/ultralcd.h"
  29. #include "planner.h"
  30. #include "../core/language.h"
  31. #include "../HAL/shared/Delay.h"
  32. #if ENABLED(EXTENSIBLE_UI)
  33. #include "../lcd/extensible_ui/ui_api.h"
  34. #endif
  35. #if ENABLED(MAX6675_IS_MAX31865)
  36. #include "Adafruit_MAX31865.h"
  37. #ifndef MAX31865_CS_PIN
  38. #define MAX31865_CS_PIN MAX6675_SS_PIN // HW:49 SW:65 for example
  39. #endif
  40. #ifndef MAX31865_MOSI_PIN
  41. #define MAX31865_MOSI_PIN MOSI_PIN // 63
  42. #endif
  43. #ifndef MAX31865_MISO_PIN
  44. #define MAX31865_MISO_PIN MAX6675_DO_PIN // 42
  45. #endif
  46. #ifndef MAX31865_SCK_PIN
  47. #define MAX31865_SCK_PIN MAX6675_SCK_PIN // 40
  48. #endif
  49. Adafruit_MAX31865 max31865 = Adafruit_MAX31865(MAX31865_CS_PIN
  50. #if MAX31865_CS_PIN != MAX6675_SS_PIN
  51. , MAX31865_MOSI_PIN // For software SPI also set MOSI/MISO/SCK
  52. , MAX31865_MISO_PIN
  53. , MAX31865_SCK_PIN
  54. #endif
  55. );
  56. #endif
  57. #define MAX6675_SEPARATE_SPI (EITHER(HEATER_0_USES_MAX6675, HEATER_1_USES_MAX6675) && PINS_EXIST(MAX6675_SCK, MAX6675_DO))
  58. #if MAX6675_SEPARATE_SPI
  59. #include "../libs/private_spi.h"
  60. #endif
  61. #if ENABLED(PID_EXTRUSION_SCALING)
  62. #include "stepper.h"
  63. #endif
  64. #if ENABLED(BABYSTEPPING) && DISABLED(INTEGRATED_BABYSTEPPING)
  65. #include "../feature/babystep.h"
  66. #endif
  67. #include "printcounter.h"
  68. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  69. #include "../feature/filwidth.h"
  70. #endif
  71. #if ENABLED(EMERGENCY_PARSER)
  72. #include "../feature/emergency_parser.h"
  73. #endif
  74. #if ENABLED(PRINTER_EVENT_LEDS)
  75. #include "../feature/leds/printer_event_leds.h"
  76. #endif
  77. #if ENABLED(JOYSTICK)
  78. #include "../feature/joystick.h"
  79. #endif
  80. #if ENABLED(SINGLENOZZLE)
  81. #include "tool_change.h"
  82. #endif
  83. #if USE_BEEPER
  84. #include "../libs/buzzer.h"
  85. #endif
  86. #if HOTEND_USES_THERMISTOR
  87. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  88. static const void* heater_ttbl_map[2] = { (void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  89. static constexpr uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  90. #else
  91. #define NEXT_TEMPTABLE(N) ,HEATER_##N##_TEMPTABLE
  92. #define NEXT_TEMPTABLE_LEN(N) ,HEATER_##N##_TEMPTABLE_LEN
  93. static const void* heater_ttbl_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE REPEAT_S(1, HOTENDS, NEXT_TEMPTABLE));
  94. static constexpr uint8_t heater_ttbllen_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE_LEN REPEAT_S(1, HOTENDS, NEXT_TEMPTABLE_LEN));
  95. #endif
  96. #endif
  97. Temperature thermalManager;
  98. const char str_t_thermal_runaway[] PROGMEM = STR_T_THERMAL_RUNAWAY,
  99. str_t_heating_failed[] PROGMEM = STR_T_HEATING_FAILED;
  100. /**
  101. * Macros to include the heater id in temp errors. The compiler's dead-code
  102. * elimination should (hopefully) optimize out the unused strings.
  103. */
  104. #if HAS_HEATED_BED
  105. #define _BED_PSTR(h) (h) == H_BED ? GET_TEXT(MSG_BED) :
  106. #else
  107. #define _BED_PSTR(h)
  108. #endif
  109. #if HAS_HEATED_CHAMBER
  110. #define _CHAMBER_PSTR(h) (h) == H_CHAMBER ? GET_TEXT(MSG_CHAMBER) :
  111. #else
  112. #define _CHAMBER_PSTR(h)
  113. #endif
  114. #define _E_PSTR(h,N) ((HOTENDS) > N && (h) == N) ? PSTR(LCD_STR_E##N) :
  115. #define HEATER_PSTR(h) _BED_PSTR(h) _CHAMBER_PSTR(h) _E_PSTR(h,1) _E_PSTR(h,2) _E_PSTR(h,3) _E_PSTR(h,4) _E_PSTR(h,5) PSTR(LCD_STR_E0)
  116. // public:
  117. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  118. bool Temperature::adaptive_fan_slowing = true;
  119. #endif
  120. #if HOTENDS
  121. hotend_info_t Temperature::temp_hotend[HOTEND_TEMPS]; // = { 0 }
  122. #endif
  123. #if ENABLED(AUTO_POWER_E_FANS)
  124. uint8_t Temperature::autofan_speed[HOTENDS]; // = { 0 }
  125. #endif
  126. #if ENABLED(AUTO_POWER_CHAMBER_FAN)
  127. uint8_t Temperature::chamberfan_speed; // = 0
  128. #endif
  129. #if FAN_COUNT > 0
  130. uint8_t Temperature::fan_speed[FAN_COUNT]; // = { 0 }
  131. #if ENABLED(EXTRA_FAN_SPEED)
  132. uint8_t Temperature::old_fan_speed[FAN_COUNT], Temperature::new_fan_speed[FAN_COUNT];
  133. void Temperature::set_temp_fan_speed(const uint8_t fan, const uint16_t tmp_temp) {
  134. switch (tmp_temp) {
  135. case 1:
  136. set_fan_speed(fan, old_fan_speed[fan]);
  137. break;
  138. case 2:
  139. old_fan_speed[fan] = fan_speed[fan];
  140. set_fan_speed(fan, new_fan_speed[fan]);
  141. break;
  142. default:
  143. new_fan_speed[fan] = _MIN(tmp_temp, 255U);
  144. break;
  145. }
  146. }
  147. #endif
  148. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  149. bool Temperature::fans_paused; // = false;
  150. uint8_t Temperature::saved_fan_speed[FAN_COUNT]; // = { 0 }
  151. #endif
  152. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  153. uint8_t Temperature::fan_speed_scaler[FAN_COUNT] = ARRAY_N(FAN_COUNT, 128, 128, 128, 128, 128, 128);
  154. #endif
  155. /**
  156. * Set the print fan speed for a target extruder
  157. */
  158. void Temperature::set_fan_speed(uint8_t target, uint16_t speed) {
  159. NOMORE(speed, 255U);
  160. #if ENABLED(SINGLENOZZLE)
  161. if (target != active_extruder) {
  162. if (target < EXTRUDERS) singlenozzle_fan_speed[target] = speed;
  163. return;
  164. }
  165. target = 0; // Always use fan index 0 with SINGLENOZZLE
  166. #endif
  167. if (target >= FAN_COUNT) return;
  168. fan_speed[target] = speed;
  169. }
  170. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  171. void Temperature::set_fans_paused(const bool p) {
  172. if (p != fans_paused) {
  173. fans_paused = p;
  174. if (p)
  175. FANS_LOOP(i) { saved_fan_speed[i] = fan_speed[i]; fan_speed[i] = 0; }
  176. else
  177. FANS_LOOP(i) fan_speed[i] = saved_fan_speed[i];
  178. }
  179. }
  180. #endif
  181. #endif // FAN_COUNT > 0
  182. #if WATCH_HOTENDS
  183. hotend_watch_t Temperature::watch_hotend[HOTENDS]; // = { { 0 } }
  184. #endif
  185. #if HEATER_IDLE_HANDLER
  186. hotend_idle_t Temperature::hotend_idle[HOTENDS]; // = { { 0 } }
  187. #endif
  188. #if HAS_HEATED_BED
  189. bed_info_t Temperature::temp_bed; // = { 0 }
  190. // Init min and max temp with extreme values to prevent false errors during startup
  191. #ifdef BED_MINTEMP
  192. int16_t Temperature::mintemp_raw_BED = HEATER_BED_RAW_LO_TEMP;
  193. #endif
  194. #ifdef BED_MAXTEMP
  195. int16_t Temperature::maxtemp_raw_BED = HEATER_BED_RAW_HI_TEMP;
  196. #endif
  197. #if WATCH_BED
  198. bed_watch_t Temperature::watch_bed; // = { 0 }
  199. #endif
  200. #if DISABLED(PIDTEMPBED)
  201. millis_t Temperature::next_bed_check_ms;
  202. #endif
  203. #if HEATER_IDLE_HANDLER
  204. hotend_idle_t Temperature::bed_idle; // = { 0 }
  205. #endif
  206. #endif // HAS_HEATED_BED
  207. #if HAS_TEMP_CHAMBER
  208. chamber_info_t Temperature::temp_chamber; // = { 0 }
  209. #if HAS_HEATED_CHAMBER
  210. #ifdef CHAMBER_MINTEMP
  211. int16_t Temperature::mintemp_raw_CHAMBER = HEATER_CHAMBER_RAW_LO_TEMP;
  212. #endif
  213. #ifdef CHAMBER_MAXTEMP
  214. int16_t Temperature::maxtemp_raw_CHAMBER = HEATER_CHAMBER_RAW_HI_TEMP;
  215. #endif
  216. #if WATCH_CHAMBER
  217. chamber_watch_t Temperature::watch_chamber{0};
  218. #endif
  219. millis_t Temperature::next_chamber_check_ms;
  220. #endif // HAS_HEATED_CHAMBER
  221. #endif // HAS_TEMP_CHAMBER
  222. #if HAS_TEMP_PROBE
  223. probe_info_t Temperature::temp_probe; // = { 0 }
  224. #endif
  225. // Initialized by settings.load()
  226. #if ENABLED(PIDTEMP)
  227. //hotend_pid_t Temperature::pid[HOTENDS];
  228. #endif
  229. #if ENABLED(PREVENT_COLD_EXTRUSION)
  230. bool Temperature::allow_cold_extrude = false;
  231. int16_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  232. #endif
  233. // private:
  234. #if EARLY_WATCHDOG
  235. bool Temperature::inited = false;
  236. #endif
  237. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  238. uint16_t Temperature::redundant_temperature_raw = 0;
  239. float Temperature::redundant_temperature = 0.0;
  240. #endif
  241. volatile bool Temperature::raw_temps_ready = false;
  242. #if ENABLED(PID_EXTRUSION_SCALING)
  243. int32_t Temperature::last_e_position, Temperature::lpq[LPQ_MAX_LEN];
  244. lpq_ptr_t Temperature::lpq_ptr = 0;
  245. #endif
  246. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) < (HEATER_##N##_RAW_HI_TEMP) ? 1 : -1)
  247. #if HOTENDS
  248. // Init mintemp and maxtemp with extreme values to prevent false errors during startup
  249. constexpr temp_range_t sensor_heater_0 { HEATER_0_RAW_LO_TEMP, HEATER_0_RAW_HI_TEMP, 0, 16383 },
  250. sensor_heater_1 { HEATER_1_RAW_LO_TEMP, HEATER_1_RAW_HI_TEMP, 0, 16383 },
  251. sensor_heater_2 { HEATER_2_RAW_LO_TEMP, HEATER_2_RAW_HI_TEMP, 0, 16383 },
  252. sensor_heater_3 { HEATER_3_RAW_LO_TEMP, HEATER_3_RAW_HI_TEMP, 0, 16383 },
  253. sensor_heater_4 { HEATER_4_RAW_LO_TEMP, HEATER_4_RAW_HI_TEMP, 0, 16383 },
  254. sensor_heater_5 { HEATER_5_RAW_LO_TEMP, HEATER_5_RAW_HI_TEMP, 0, 16383 },
  255. sensor_heater_6 { HEATER_6_RAW_LO_TEMP, HEATER_6_RAW_HI_TEMP, 0, 16383 },
  256. sensor_heater_7 { HEATER_7_RAW_LO_TEMP, HEATER_7_RAW_HI_TEMP, 0, 16383 };
  257. temp_range_t Temperature::temp_range[HOTENDS] = ARRAY_BY_HOTENDS(sensor_heater_0, sensor_heater_1, sensor_heater_2, sensor_heater_3, sensor_heater_4, sensor_heater_5, sensor_heater_6, sensor_heater_7);
  258. #endif
  259. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  260. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  261. #endif
  262. #ifdef MILLISECONDS_PREHEAT_TIME
  263. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  264. #endif
  265. #if HAS_AUTO_FAN
  266. millis_t Temperature::next_auto_fan_check_ms = 0;
  267. #endif
  268. #if ENABLED(FAN_SOFT_PWM)
  269. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  270. Temperature::soft_pwm_count_fan[FAN_COUNT];
  271. #endif
  272. #if ENABLED(PROBING_HEATERS_OFF)
  273. bool Temperature::paused;
  274. #endif
  275. // public:
  276. #if HAS_ADC_BUTTONS
  277. uint32_t Temperature::current_ADCKey_raw = HAL_ADC_RANGE;
  278. uint8_t Temperature::ADCKey_count = 0;
  279. #endif
  280. #if ENABLED(PID_EXTRUSION_SCALING)
  281. int16_t Temperature::lpq_len; // Initialized in configuration_store
  282. #endif
  283. #if HAS_PID_HEATING
  284. inline void say_default_() { SERIAL_ECHOPGM("#define DEFAULT_"); }
  285. /**
  286. * PID Autotuning (M303)
  287. *
  288. * Alternately heat and cool the nozzle, observing its behavior to
  289. * determine the best PID values to achieve a stable temperature.
  290. * Needs sufficient heater power to make some overshoot at target
  291. * temperature to succeed.
  292. */
  293. void Temperature::PID_autotune(const float &target, const heater_ind_t heater, const int8_t ncycles, const bool set_result/*=false*/) {
  294. float current_temp = 0.0;
  295. int cycles = 0;
  296. bool heating = true;
  297. millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
  298. long t_high = 0, t_low = 0;
  299. long bias, d;
  300. PID_t tune_pid = { 0, 0, 0 };
  301. float maxT = 0, minT = 10000;
  302. const bool isbed = (heater == H_BED);
  303. #if HAS_PID_FOR_BOTH
  304. #define GHV(B,H) (isbed ? (B) : (H))
  305. #define SHV(B,H) do{ if (isbed) temp_bed.soft_pwm_amount = B; else temp_hotend[heater].soft_pwm_amount = H; }while(0)
  306. #define ONHEATINGSTART() (isbed ? printerEventLEDs.onBedHeatingStart() : printerEventLEDs.onHotendHeatingStart())
  307. #define ONHEATING(S,C,T) (isbed ? printerEventLEDs.onBedHeating(S,C,T) : printerEventLEDs.onHotendHeating(S,C,T))
  308. #elif ENABLED(PIDTEMPBED)
  309. #define GHV(B,H) B
  310. #define SHV(B,H) (temp_bed.soft_pwm_amount = B)
  311. #define ONHEATINGSTART() printerEventLEDs.onBedHeatingStart()
  312. #define ONHEATING(S,C,T) printerEventLEDs.onBedHeating(S,C,T)
  313. #else
  314. #define GHV(B,H) H
  315. #define SHV(B,H) (temp_hotend[heater].soft_pwm_amount = H)
  316. #define ONHEATINGSTART() printerEventLEDs.onHotendHeatingStart()
  317. #define ONHEATING(S,C,T) printerEventLEDs.onHotendHeating(S,C,T)
  318. #endif
  319. #if WATCH_BED || WATCH_HOTENDS
  320. #define HAS_TP_BED BOTH(THERMAL_PROTECTION_BED, PIDTEMPBED)
  321. #if HAS_TP_BED && BOTH(THERMAL_PROTECTION_HOTENDS, PIDTEMP)
  322. #define GTV(B,H) (isbed ? (B) : (H))
  323. #elif HAS_TP_BED
  324. #define GTV(B,H) (B)
  325. #else
  326. #define GTV(B,H) (H)
  327. #endif
  328. const uint16_t watch_temp_period = GTV(WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD);
  329. const uint8_t watch_temp_increase = GTV(WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE);
  330. const float watch_temp_target = target - float(watch_temp_increase + GTV(TEMP_BED_HYSTERESIS, TEMP_HYSTERESIS) + 1);
  331. millis_t temp_change_ms = next_temp_ms + watch_temp_period * 1000UL;
  332. float next_watch_temp = 0.0;
  333. bool heated = false;
  334. #endif
  335. #if HAS_AUTO_FAN
  336. next_auto_fan_check_ms = next_temp_ms + 2500UL;
  337. #endif
  338. if (target > GHV(BED_MAXTEMP - 10, temp_range[heater].maxtemp - 15)) {
  339. SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH);
  340. #if ENABLED(EXTENSIBLE_UI)
  341. ExtUI::OnPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH);
  342. #endif
  343. return;
  344. }
  345. SERIAL_ECHOLNPGM(STR_PID_AUTOTUNE_START);
  346. disable_all_heaters();
  347. SHV(bias = d = (MAX_BED_POWER) >> 1, bias = d = (PID_MAX) >> 1);
  348. wait_for_heatup = true; // Can be interrupted with M108
  349. #if ENABLED(PRINTER_EVENT_LEDS)
  350. const float start_temp = GHV(temp_bed.celsius, temp_hotend[heater].celsius);
  351. LEDColor color = ONHEATINGSTART();
  352. #endif
  353. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  354. adaptive_fan_slowing = false;
  355. #endif
  356. // PID Tuning loop
  357. while (wait_for_heatup) {
  358. const millis_t ms = millis();
  359. if (raw_temps_ready) { // temp sample ready
  360. updateTemperaturesFromRawValues();
  361. // Get the current temperature and constrain it
  362. current_temp = GHV(temp_bed.celsius, temp_hotend[heater].celsius);
  363. NOLESS(maxT, current_temp);
  364. NOMORE(minT, current_temp);
  365. #if ENABLED(PRINTER_EVENT_LEDS)
  366. ONHEATING(start_temp, current_temp, target);
  367. #endif
  368. #if HAS_AUTO_FAN
  369. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  370. checkExtruderAutoFans();
  371. next_auto_fan_check_ms = ms + 2500UL;
  372. }
  373. #endif
  374. if (heating && current_temp > target) {
  375. if (ELAPSED(ms, t2 + 5000UL)) {
  376. heating = false;
  377. SHV((bias - d) >> 1, (bias - d) >> 1);
  378. t1 = ms;
  379. t_high = t1 - t2;
  380. maxT = target;
  381. }
  382. }
  383. if (!heating && current_temp < target) {
  384. if (ELAPSED(ms, t1 + 5000UL)) {
  385. heating = true;
  386. t2 = ms;
  387. t_low = t2 - t1;
  388. if (cycles > 0) {
  389. const long max_pow = GHV(MAX_BED_POWER, PID_MAX);
  390. bias += (d * (t_high - t_low)) / (t_low + t_high);
  391. LIMIT(bias, 20, max_pow - 20);
  392. d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias;
  393. SERIAL_ECHOPAIR(STR_BIAS, bias, STR_D_COLON, d, STR_T_MIN, minT, STR_T_MAX, maxT);
  394. if (cycles > 2) {
  395. const float Ku = (4.0f * d) / (float(M_PI) * (maxT - minT) * 0.5f),
  396. Tu = float(t_low + t_high) * 0.001f,
  397. pf = isbed ? 0.2f : 0.6f,
  398. df = isbed ? 1.0f / 3.0f : 1.0f / 8.0f;
  399. SERIAL_ECHOPAIR(STR_KU, Ku, STR_TU, Tu);
  400. if (isbed) { // Do not remove this otherwise PID autotune won't work right for the bed!
  401. tune_pid.Kp = Ku * 0.2f;
  402. tune_pid.Ki = 2 * tune_pid.Kp / Tu;
  403. tune_pid.Kd = tune_pid.Kp * Tu / 3;
  404. SERIAL_ECHOLNPGM("\n" " No overshoot"); // Works far better for the bed. Classic and some have bad ringing.
  405. SERIAL_ECHOLNPAIR(STR_KP, tune_pid.Kp, STR_KI, tune_pid.Ki, STR_KD, tune_pid.Kd);
  406. }
  407. else {
  408. tune_pid.Kp = Ku * pf;
  409. tune_pid.Kd = tune_pid.Kp * Tu * df;
  410. tune_pid.Ki = 2 * tune_pid.Kp / Tu;
  411. SERIAL_ECHOLNPGM("\n" STR_CLASSIC_PID);
  412. SERIAL_ECHOLNPAIR(STR_KP, tune_pid.Kp, STR_KI, tune_pid.Ki, STR_KD, tune_pid.Kd);
  413. }
  414. /**
  415. tune_pid.Kp = 0.33 * Ku;
  416. tune_pid.Ki = tune_pid.Kp / Tu;
  417. tune_pid.Kd = tune_pid.Kp * Tu / 3;
  418. SERIAL_ECHOLNPGM(" Some overshoot");
  419. SERIAL_ECHOLNPAIR(" Kp: ", tune_pid.Kp, " Ki: ", tune_pid.Ki, " Kd: ", tune_pid.Kd, " No overshoot");
  420. tune_pid.Kp = 0.2 * Ku;
  421. tune_pid.Ki = 2 * tune_pid.Kp / Tu;
  422. tune_pid.Kd = tune_pid.Kp * Tu / 3;
  423. SERIAL_ECHOPAIR(" Kp: ", tune_pid.Kp, " Ki: ", tune_pid.Ki, " Kd: ", tune_pid.Kd);
  424. */
  425. }
  426. }
  427. SHV((bias + d) >> 1, (bias + d) >> 1);
  428. cycles++;
  429. minT = target;
  430. }
  431. }
  432. }
  433. // Did the temperature overshoot very far?
  434. #ifndef MAX_OVERSHOOT_PID_AUTOTUNE
  435. #define MAX_OVERSHOOT_PID_AUTOTUNE 30
  436. #endif
  437. if (current_temp > target + MAX_OVERSHOOT_PID_AUTOTUNE) {
  438. SERIAL_ECHOLNPGM(STR_PID_TEMP_TOO_HIGH);
  439. #if ENABLED(EXTENSIBLE_UI)
  440. ExtUI::OnPidTuning(ExtUI::result_t::PID_TEMP_TOO_HIGH);
  441. #endif
  442. break;
  443. }
  444. // Report heater states every 2 seconds
  445. if (ELAPSED(ms, next_temp_ms)) {
  446. #if HAS_TEMP_SENSOR
  447. print_heater_states(isbed ? active_extruder : heater);
  448. SERIAL_EOL();
  449. #endif
  450. next_temp_ms = ms + 2000UL;
  451. // Make sure heating is actually working
  452. #if WATCH_BED || WATCH_HOTENDS
  453. if (
  454. #if WATCH_BED && WATCH_HOTENDS
  455. true
  456. #elif WATCH_HOTENDS
  457. !isbed
  458. #else
  459. isbed
  460. #endif
  461. ) {
  462. if (!heated) { // If not yet reached target...
  463. if (current_temp > next_watch_temp) { // Over the watch temp?
  464. next_watch_temp = current_temp + watch_temp_increase; // - set the next temp to watch for
  465. temp_change_ms = ms + watch_temp_period * 1000UL; // - move the expiration timer up
  466. if (current_temp > watch_temp_target) heated = true; // - Flag if target temperature reached
  467. }
  468. else if (ELAPSED(ms, temp_change_ms)) // Watch timer expired
  469. _temp_error(heater, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  470. }
  471. else if (current_temp < target - (MAX_OVERSHOOT_PID_AUTOTUNE)) // Heated, then temperature fell too far?
  472. _temp_error(heater, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  473. }
  474. #endif
  475. } // every 2 seconds
  476. // Timeout after MAX_CYCLE_TIME_PID_AUTOTUNE minutes since the last undershoot/overshoot cycle
  477. #ifndef MAX_CYCLE_TIME_PID_AUTOTUNE
  478. #define MAX_CYCLE_TIME_PID_AUTOTUNE 20L
  479. #endif
  480. if (((ms - t1) + (ms - t2)) > (MAX_CYCLE_TIME_PID_AUTOTUNE * 60L * 1000L)) {
  481. #if ENABLED(EXTENSIBLE_UI)
  482. ExtUI::OnPidTuning(ExtUI::result_t::PID_TUNING_TIMEOUT);
  483. #endif
  484. SERIAL_ECHOLNPGM(STR_PID_TIMEOUT);
  485. break;
  486. }
  487. if (cycles > ncycles && cycles > 2) {
  488. SERIAL_ECHOLNPGM(STR_PID_AUTOTUNE_FINISHED);
  489. #if HAS_PID_FOR_BOTH
  490. const char * const estring = GHV(PSTR("bed"), NUL_STR);
  491. say_default_(); serialprintPGM(estring); SERIAL_ECHOLNPAIR("Kp ", tune_pid.Kp);
  492. say_default_(); serialprintPGM(estring); SERIAL_ECHOLNPAIR("Ki ", tune_pid.Ki);
  493. say_default_(); serialprintPGM(estring); SERIAL_ECHOLNPAIR("Kd ", tune_pid.Kd);
  494. #elif ENABLED(PIDTEMP)
  495. say_default_(); SERIAL_ECHOLNPAIR("Kp ", tune_pid.Kp);
  496. say_default_(); SERIAL_ECHOLNPAIR("Ki ", tune_pid.Ki);
  497. say_default_(); SERIAL_ECHOLNPAIR("Kd ", tune_pid.Kd);
  498. #else
  499. say_default_(); SERIAL_ECHOLNPAIR("bedKp ", tune_pid.Kp);
  500. say_default_(); SERIAL_ECHOLNPAIR("bedKi ", tune_pid.Ki);
  501. say_default_(); SERIAL_ECHOLNPAIR("bedKd ", tune_pid.Kd);
  502. #endif
  503. #define _SET_BED_PID() do { \
  504. temp_bed.pid.Kp = tune_pid.Kp; \
  505. temp_bed.pid.Ki = scalePID_i(tune_pid.Ki); \
  506. temp_bed.pid.Kd = scalePID_d(tune_pid.Kd); \
  507. }while(0)
  508. #define _SET_EXTRUDER_PID() do { \
  509. PID_PARAM(Kp, heater) = tune_pid.Kp; \
  510. PID_PARAM(Ki, heater) = scalePID_i(tune_pid.Ki); \
  511. PID_PARAM(Kd, heater) = scalePID_d(tune_pid.Kd); \
  512. updatePID(); }while(0)
  513. // Use the result? (As with "M303 U1")
  514. if (set_result) {
  515. #if HAS_PID_FOR_BOTH
  516. if (isbed) _SET_BED_PID(); else _SET_EXTRUDER_PID();
  517. #elif ENABLED(PIDTEMP)
  518. _SET_EXTRUDER_PID();
  519. #else
  520. _SET_BED_PID();
  521. #endif
  522. }
  523. #if ENABLED(PRINTER_EVENT_LEDS)
  524. printerEventLEDs.onPidTuningDone(color);
  525. #endif
  526. #if ENABLED(EXTENSIBLE_UI)
  527. ExtUI::OnPidTuning(ExtUI::result_t::PID_DONE);
  528. #endif
  529. goto EXIT_M303;
  530. }
  531. ui.update();
  532. }
  533. disable_all_heaters();
  534. #if ENABLED(PRINTER_EVENT_LEDS)
  535. printerEventLEDs.onPidTuningDone(color);
  536. #endif
  537. #if ENABLED(EXTENSIBLE_UI)
  538. ExtUI::OnPidTuning(ExtUI::result_t::PID_DONE);
  539. #endif
  540. EXIT_M303:
  541. #if ENABLED(NO_FAN_SLOWING_IN_PID_TUNING)
  542. adaptive_fan_slowing = true;
  543. #endif
  544. return;
  545. }
  546. #endif // HAS_PID_HEATING
  547. /**
  548. * Class and Instance Methods
  549. */
  550. int16_t Temperature::getHeaterPower(const heater_ind_t heater_id) {
  551. switch (heater_id) {
  552. #if HAS_HEATED_BED
  553. case H_BED: return temp_bed.soft_pwm_amount;
  554. #endif
  555. #if HAS_HEATED_CHAMBER
  556. case H_CHAMBER: return temp_chamber.soft_pwm_amount;
  557. #endif
  558. default:
  559. return (0
  560. #if HOTENDS
  561. + temp_hotend[heater_id].soft_pwm_amount
  562. #endif
  563. );
  564. }
  565. }
  566. #define _EFANOVERLAP(A,B) _FANOVERLAP(E##A,B)
  567. #if HAS_AUTO_FAN
  568. #define CHAMBER_FAN_INDEX HOTENDS
  569. void Temperature::checkExtruderAutoFans() {
  570. #define _EFAN(B,A) _EFANOVERLAP(A,B) ? B :
  571. static const uint8_t fanBit[] PROGMEM = {
  572. 0
  573. #if HOTENDS > 1
  574. #define _NEXT_FAN(N) , REPEAT2(N,_EFAN,N) N
  575. RREPEAT_S(1, HOTENDS, _NEXT_FAN)
  576. #endif
  577. #if HAS_AUTO_CHAMBER_FAN
  578. #define _CFAN(B) _FANOVERLAP(CHAMBER,B) ? B :
  579. , REPEAT(HOTENDS,_CFAN) (HOTENDS)
  580. #endif
  581. };
  582. uint8_t fanState = 0;
  583. HOTEND_LOOP()
  584. if (temp_hotend[e].celsius >= EXTRUDER_AUTO_FAN_TEMPERATURE)
  585. SBI(fanState, pgm_read_byte(&fanBit[e]));
  586. #if HAS_AUTO_CHAMBER_FAN
  587. if (temp_chamber.celsius >= CHAMBER_AUTO_FAN_TEMPERATURE)
  588. SBI(fanState, pgm_read_byte(&fanBit[CHAMBER_FAN_INDEX]));
  589. #endif
  590. #define _UPDATE_AUTO_FAN(P,D,A) do{ \
  591. if (PWM_PIN(P##_AUTO_FAN_PIN) && A < 255) \
  592. analogWrite(pin_t(P##_AUTO_FAN_PIN), D ? A : 0); \
  593. else \
  594. WRITE(P##_AUTO_FAN_PIN, D); \
  595. }while(0)
  596. uint8_t fanDone = 0;
  597. for (uint8_t f = 0; f < COUNT(fanBit); f++) {
  598. const uint8_t realFan = pgm_read_byte(&fanBit[f]);
  599. if (TEST(fanDone, realFan)) continue;
  600. const bool fan_on = TEST(fanState, realFan);
  601. switch (f) {
  602. #if ENABLED(AUTO_POWER_CHAMBER_FAN)
  603. case CHAMBER_FAN_INDEX:
  604. chamberfan_speed = fan_on ? CHAMBER_AUTO_FAN_SPEED : 0;
  605. break;
  606. #endif
  607. default:
  608. #if ENABLED(AUTO_POWER_E_FANS)
  609. autofan_speed[realFan] = fan_on ? EXTRUDER_AUTO_FAN_SPEED : 0;
  610. #endif
  611. break;
  612. }
  613. switch (f) {
  614. #if HAS_AUTO_FAN_0
  615. case 0: _UPDATE_AUTO_FAN(E0, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  616. #endif
  617. #if HAS_AUTO_FAN_1
  618. case 1: _UPDATE_AUTO_FAN(E1, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  619. #endif
  620. #if HAS_AUTO_FAN_2
  621. case 2: _UPDATE_AUTO_FAN(E2, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  622. #endif
  623. #if HAS_AUTO_FAN_3
  624. case 3: _UPDATE_AUTO_FAN(E3, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  625. #endif
  626. #if HAS_AUTO_FAN_4
  627. case 4: _UPDATE_AUTO_FAN(E4, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  628. #endif
  629. #if HAS_AUTO_FAN_5
  630. case 5: _UPDATE_AUTO_FAN(E5, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  631. #endif
  632. #if HAS_AUTO_FAN_6
  633. case 6: _UPDATE_AUTO_FAN(E6, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  634. #endif
  635. #if HAS_AUTO_FAN_7
  636. case 7: _UPDATE_AUTO_FAN(E7, fan_on, EXTRUDER_AUTO_FAN_SPEED); break;
  637. #endif
  638. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_E
  639. case CHAMBER_FAN_INDEX: _UPDATE_AUTO_FAN(CHAMBER, fan_on, CHAMBER_AUTO_FAN_SPEED); break;
  640. #endif
  641. }
  642. SBI(fanDone, realFan);
  643. }
  644. }
  645. #endif // HAS_AUTO_FAN
  646. //
  647. // Temperature Error Handlers
  648. //
  649. inline void loud_kill(PGM_P const lcd_msg, const heater_ind_t heater) {
  650. Running = false;
  651. #if USE_BEEPER
  652. for (uint8_t i = 20; i--;) {
  653. WRITE(BEEPER_PIN, HIGH); delay(25);
  654. WRITE(BEEPER_PIN, LOW); delay(80);
  655. }
  656. WRITE(BEEPER_PIN, HIGH);
  657. #endif
  658. kill(lcd_msg, HEATER_PSTR(heater));
  659. }
  660. void Temperature::_temp_error(const heater_ind_t heater, PGM_P const serial_msg, PGM_P const lcd_msg) {
  661. static uint8_t killed = 0;
  662. if (IsRunning()
  663. #if BOGUS_TEMPERATURE_GRACE_PERIOD
  664. && killed == 2
  665. #endif
  666. ) {
  667. SERIAL_ERROR_START();
  668. serialprintPGM(serial_msg);
  669. SERIAL_ECHOPGM(STR_STOPPED_HEATER);
  670. if (heater >= 0) SERIAL_ECHO((int)heater);
  671. #if HAS_HEATED_CHAMBER
  672. else if (heater == H_CHAMBER) SERIAL_ECHOPGM(STR_HEATER_CHAMBER);
  673. #endif
  674. else SERIAL_ECHOPGM(STR_HEATER_BED);
  675. SERIAL_EOL();
  676. }
  677. disable_all_heaters(); // always disable (even for bogus temp)
  678. #if BOGUS_TEMPERATURE_GRACE_PERIOD
  679. const millis_t ms = millis();
  680. static millis_t expire_ms;
  681. switch (killed) {
  682. case 0:
  683. expire_ms = ms + BOGUS_TEMPERATURE_GRACE_PERIOD;
  684. ++killed;
  685. break;
  686. case 1:
  687. if (ELAPSED(ms, expire_ms)) ++killed;
  688. break;
  689. case 2:
  690. loud_kill(lcd_msg, heater);
  691. ++killed;
  692. break;
  693. }
  694. #elif defined(BOGUS_TEMPERATURE_GRACE_PERIOD)
  695. UNUSED(killed);
  696. #else
  697. if (!killed) { killed = 1; loud_kill(lcd_msg, heater); }
  698. #endif
  699. }
  700. void Temperature::max_temp_error(const heater_ind_t heater) {
  701. _temp_error(heater, PSTR(STR_T_MAXTEMP), GET_TEXT(MSG_ERR_MAXTEMP));
  702. }
  703. void Temperature::min_temp_error(const heater_ind_t heater) {
  704. _temp_error(heater, PSTR(STR_T_MINTEMP), GET_TEXT(MSG_ERR_MINTEMP));
  705. }
  706. #if HOTENDS
  707. float Temperature::get_pid_output_hotend(const uint8_t E_NAME) {
  708. const uint8_t ee = HOTEND_INDEX;
  709. #if ENABLED(PIDTEMP)
  710. #if DISABLED(PID_OPENLOOP)
  711. static hotend_pid_t work_pid[HOTENDS];
  712. static float temp_iState[HOTENDS] = { 0 },
  713. temp_dState[HOTENDS] = { 0 };
  714. static bool pid_reset[HOTENDS] = { false };
  715. const float pid_error = temp_hotend[ee].target - temp_hotend[ee].celsius;
  716. float pid_output;
  717. if (temp_hotend[ee].target == 0
  718. || pid_error < -(PID_FUNCTIONAL_RANGE)
  719. #if HEATER_IDLE_HANDLER
  720. || hotend_idle[ee].timed_out
  721. #endif
  722. ) {
  723. pid_output = 0;
  724. pid_reset[ee] = true;
  725. }
  726. else if (pid_error > PID_FUNCTIONAL_RANGE) {
  727. pid_output = BANG_MAX;
  728. pid_reset[ee] = true;
  729. }
  730. else {
  731. if (pid_reset[ee]) {
  732. temp_iState[ee] = 0.0;
  733. work_pid[ee].Kd = 0.0;
  734. pid_reset[ee] = false;
  735. }
  736. work_pid[ee].Kd = work_pid[ee].Kd + PID_K2 * (PID_PARAM(Kd, ee) * (temp_dState[ee] - temp_hotend[ee].celsius) - work_pid[ee].Kd);
  737. const float max_power_over_i_gain = float(PID_MAX) / PID_PARAM(Ki, ee) - float(MIN_POWER);
  738. temp_iState[ee] = constrain(temp_iState[ee] + pid_error, 0, max_power_over_i_gain);
  739. work_pid[ee].Kp = PID_PARAM(Kp, ee) * pid_error;
  740. work_pid[ee].Ki = PID_PARAM(Ki, ee) * temp_iState[ee];
  741. pid_output = work_pid[ee].Kp + work_pid[ee].Ki + work_pid[ee].Kd + float(MIN_POWER);
  742. #if ENABLED(PID_EXTRUSION_SCALING)
  743. #if HOTENDS == 1
  744. constexpr bool this_hotend = true;
  745. #else
  746. const bool this_hotend = (ee == active_extruder);
  747. #endif
  748. work_pid[ee].Kc = 0;
  749. if (this_hotend) {
  750. const long e_position = stepper.position(E_AXIS);
  751. if (e_position > last_e_position) {
  752. lpq[lpq_ptr] = e_position - last_e_position;
  753. last_e_position = e_position;
  754. }
  755. else
  756. lpq[lpq_ptr] = 0;
  757. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  758. work_pid[ee].Kc = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, ee);
  759. pid_output += work_pid[ee].Kc;
  760. }
  761. #endif // PID_EXTRUSION_SCALING
  762. #if ENABLED(PID_FAN_SCALING)
  763. if (thermalManager.fan_speed[active_extruder] > PID_FAN_SCALING_MIN_SPEED) {
  764. work_pid[ee].Kf = PID_PARAM(Kf, ee) + (PID_FAN_SCALING_LIN_FACTOR) * thermalManager.fan_speed[active_extruder];
  765. pid_output += work_pid[ee].Kf;
  766. }
  767. //pid_output -= work_pid[ee].Ki;
  768. //pid_output += work_pid[ee].Ki * work_pid[ee].Kf
  769. #endif // PID_FAN_SCALING
  770. LIMIT(pid_output, 0, PID_MAX);
  771. }
  772. temp_dState[ee] = temp_hotend[ee].celsius;
  773. #else // PID_OPENLOOP
  774. const float pid_output = constrain(temp_hotend[ee].target, 0, PID_MAX);
  775. #endif // PID_OPENLOOP
  776. #if ENABLED(PID_DEBUG)
  777. if (ee == active_extruder) {
  778. SERIAL_ECHO_START();
  779. SERIAL_ECHOPAIR(
  780. STR_PID_DEBUG, ee,
  781. STR_PID_DEBUG_INPUT, temp_hotend[ee].celsius,
  782. STR_PID_DEBUG_OUTPUT, pid_output
  783. );
  784. #if DISABLED(PID_OPENLOOP)
  785. {
  786. SERIAL_ECHOPAIR(
  787. STR_PID_DEBUG_PTERM, work_pid[ee].Kp,
  788. STR_PID_DEBUG_ITERM, work_pid[ee].Ki,
  789. STR_PID_DEBUG_DTERM, work_pid[ee].Kd
  790. #if ENABLED(PID_EXTRUSION_SCALING)
  791. , STR_PID_DEBUG_CTERM, work_pid[ee].Kc
  792. #endif
  793. );
  794. }
  795. #endif
  796. SERIAL_EOL();
  797. }
  798. #endif // PID_DEBUG
  799. #else // No PID enabled
  800. #if HEATER_IDLE_HANDLER
  801. const bool is_idling = hotend_idle[ee].timed_out;
  802. #else
  803. constexpr bool is_idling = false;
  804. #endif
  805. const float pid_output = (!is_idling && temp_hotend[ee].celsius < temp_hotend[ee].target) ? BANG_MAX : 0;
  806. #endif
  807. return pid_output;
  808. }
  809. #endif // HOTENDS
  810. #if ENABLED(PIDTEMPBED)
  811. float Temperature::get_pid_output_bed() {
  812. #if DISABLED(PID_OPENLOOP)
  813. static PID_t work_pid{0};
  814. static float temp_iState = 0, temp_dState = 0;
  815. static bool pid_reset = true;
  816. float pid_output = 0;
  817. const float max_power_over_i_gain = float(MAX_BED_POWER) / temp_bed.pid.Ki - float(MIN_BED_POWER),
  818. pid_error = temp_bed.target - temp_bed.celsius;
  819. if (!temp_bed.target || pid_error < -(PID_FUNCTIONAL_RANGE)) {
  820. pid_output = 0;
  821. pid_reset = true;
  822. }
  823. else if (pid_error > PID_FUNCTIONAL_RANGE) {
  824. pid_output = MAX_BED_POWER;
  825. pid_reset = true;
  826. }
  827. else {
  828. if (pid_reset) {
  829. temp_iState = 0.0;
  830. work_pid.Kd = 0.0;
  831. pid_reset = false;
  832. }
  833. temp_iState = constrain(temp_iState + pid_error, 0, max_power_over_i_gain);
  834. work_pid.Kp = temp_bed.pid.Kp * pid_error;
  835. work_pid.Ki = temp_bed.pid.Ki * temp_iState;
  836. work_pid.Kd = work_pid.Kd + PID_K2 * (temp_bed.pid.Kd * (temp_dState - temp_bed.celsius) - work_pid.Kd);
  837. temp_dState = temp_bed.celsius;
  838. pid_output = constrain(work_pid.Kp + work_pid.Ki + work_pid.Kd + float(MIN_BED_POWER), 0, MAX_BED_POWER);
  839. }
  840. #else // PID_OPENLOOP
  841. const float pid_output = constrain(temp_bed.target, 0, MAX_BED_POWER);
  842. #endif // PID_OPENLOOP
  843. #if ENABLED(PID_BED_DEBUG)
  844. {
  845. SERIAL_ECHO_START();
  846. SERIAL_ECHOLNPAIR(
  847. " PID_BED_DEBUG : Input ", temp_bed.celsius, " Output ", pid_output,
  848. #if DISABLED(PID_OPENLOOP)
  849. STR_PID_DEBUG_PTERM, work_pid.Kp,
  850. STR_PID_DEBUG_ITERM, work_pid.Ki,
  851. STR_PID_DEBUG_DTERM, work_pid.Kd,
  852. #endif
  853. );
  854. }
  855. #endif
  856. return pid_output;
  857. }
  858. #endif // PIDTEMPBED
  859. /**
  860. * Manage heating activities for extruder hot-ends and a heated bed
  861. * - Acquire updated temperature readings
  862. * - Also resets the watchdog timer
  863. * - Invoke thermal runaway protection
  864. * - Manage extruder auto-fan
  865. * - Apply filament width to the extrusion rate (may move)
  866. * - Update the heated bed PID output value
  867. */
  868. void Temperature::manage_heater() {
  869. #if EARLY_WATCHDOG
  870. // If thermal manager is still not running, make sure to at least reset the watchdog!
  871. if (!inited) return watchdog_refresh();
  872. #endif
  873. #if ENABLED(EMERGENCY_PARSER)
  874. if (emergency_parser.killed_by_M112) kill(M112_KILL_STR, nullptr, true);
  875. #endif
  876. if (!raw_temps_ready) return;
  877. updateTemperaturesFromRawValues(); // also resets the watchdog
  878. #if ENABLED(HEATER_0_USES_MAX6675)
  879. if (temp_hotend[0].celsius > _MIN(HEATER_0_MAXTEMP, HEATER_0_MAX6675_TMAX - 1.0)) max_temp_error(H_E0);
  880. if (temp_hotend[0].celsius < _MAX(HEATER_0_MINTEMP, HEATER_0_MAX6675_TMIN + .01)) min_temp_error(H_E0);
  881. #endif
  882. #if ENABLED(HEATER_1_USES_MAX6675)
  883. if (temp_hotend[1].celsius > _MIN(HEATER_1_MAXTEMP, HEATER_1_MAX6675_TMAX - 1.0)) max_temp_error(H_E1);
  884. if (temp_hotend[1].celsius < _MAX(HEATER_1_MINTEMP, HEATER_1_MAX6675_TMIN + .01)) min_temp_error(H_E1);
  885. #endif
  886. millis_t ms = millis();
  887. #if HOTENDS
  888. HOTEND_LOOP() {
  889. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  890. if (degHotend(e) > temp_range[e].maxtemp)
  891. _temp_error((heater_ind_t)e, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  892. #endif
  893. #if HEATER_IDLE_HANDLER
  894. hotend_idle[e].update(ms);
  895. #endif
  896. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  897. // Check for thermal runaway
  898. thermal_runaway_protection(tr_state_machine[e], temp_hotend[e].celsius, temp_hotend[e].target, (heater_ind_t)e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  899. #endif
  900. temp_hotend[e].soft_pwm_amount = (temp_hotend[e].celsius > temp_range[e].mintemp || is_preheating(e)) && temp_hotend[e].celsius < temp_range[e].maxtemp ? (int)get_pid_output_hotend(e) >> 1 : 0;
  901. #if WATCH_HOTENDS
  902. // Make sure temperature is increasing
  903. if (watch_hotend[e].next_ms && ELAPSED(ms, watch_hotend[e].next_ms)) { // Time to check this extruder?
  904. if (degHotend(e) < watch_hotend[e].target) // Failed to increase enough?
  905. _temp_error((heater_ind_t)e, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  906. else // Start again if the target is still far off
  907. start_watching_hotend(e);
  908. }
  909. #endif
  910. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  911. // Make sure measured temperatures are close together
  912. if (ABS(temp_hotend[0].celsius - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  913. _temp_error(H_E0, PSTR(STR_REDUNDANCY), GET_TEXT(MSG_ERR_REDUNDANT_TEMP));
  914. #endif
  915. } // HOTEND_LOOP
  916. #endif // HOTENDS
  917. #if HAS_AUTO_FAN
  918. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  919. checkExtruderAutoFans();
  920. next_auto_fan_check_ms = ms + 2500UL;
  921. }
  922. #endif
  923. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  924. /**
  925. * Dynamically set the volumetric multiplier based
  926. * on the delayed Filament Width measurement.
  927. */
  928. filwidth.update_volumetric();
  929. #endif
  930. #if HAS_HEATED_BED
  931. #if ENABLED(THERMAL_PROTECTION_BED)
  932. if (degBed() > BED_MAXTEMP)
  933. _temp_error(H_BED, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  934. #endif
  935. #if WATCH_BED
  936. // Make sure temperature is increasing
  937. if (watch_bed.elapsed(ms)) { // Time to check the bed?
  938. if (degBed() < watch_bed.target) // Failed to increase enough?
  939. _temp_error(H_BED, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  940. else // Start again if the target is still far off
  941. start_watching_bed();
  942. }
  943. #endif // WATCH_BED
  944. #define PAUSE_CHANGE_REQD BOTH(PROBING_HEATERS_OFF, BED_LIMIT_SWITCHING)
  945. #if PAUSE_CHANGE_REQD
  946. static bool last_pause_state;
  947. #endif
  948. do {
  949. #if DISABLED(PIDTEMPBED)
  950. if (PENDING(ms, next_bed_check_ms)
  951. #if PAUSE_CHANGE_REQD
  952. && paused == last_pause_state
  953. #endif
  954. ) break;
  955. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  956. #if PAUSE_CHANGE_REQD
  957. last_pause_state = paused;
  958. #endif
  959. #endif
  960. #if HEATER_IDLE_HANDLER
  961. bed_idle.update(ms);
  962. #endif
  963. #if HAS_THERMALLY_PROTECTED_BED
  964. thermal_runaway_protection(tr_state_machine_bed, temp_bed.celsius, temp_bed.target, H_BED, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS);
  965. #endif
  966. #if HEATER_IDLE_HANDLER
  967. if (bed_idle.timed_out) {
  968. temp_bed.soft_pwm_amount = 0;
  969. #if DISABLED(PIDTEMPBED)
  970. WRITE_HEATER_BED(LOW);
  971. #endif
  972. }
  973. else
  974. #endif
  975. {
  976. #if ENABLED(PIDTEMPBED)
  977. temp_bed.soft_pwm_amount = WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  978. #else
  979. // Check if temperature is within the correct band
  980. if (WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP)) {
  981. #if ENABLED(BED_LIMIT_SWITCHING)
  982. if (temp_bed.celsius >= temp_bed.target + BED_HYSTERESIS)
  983. temp_bed.soft_pwm_amount = 0;
  984. else if (temp_bed.celsius <= temp_bed.target - (BED_HYSTERESIS))
  985. temp_bed.soft_pwm_amount = MAX_BED_POWER >> 1;
  986. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  987. temp_bed.soft_pwm_amount = temp_bed.celsius < temp_bed.target ? MAX_BED_POWER >> 1 : 0;
  988. #endif
  989. }
  990. else {
  991. temp_bed.soft_pwm_amount = 0;
  992. WRITE_HEATER_BED(LOW);
  993. }
  994. #endif
  995. }
  996. } while (false);
  997. #endif // HAS_HEATED_BED
  998. #if HAS_HEATED_CHAMBER
  999. #ifndef CHAMBER_CHECK_INTERVAL
  1000. #define CHAMBER_CHECK_INTERVAL 1000UL
  1001. #endif
  1002. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  1003. if (degChamber() > CHAMBER_MAXTEMP)
  1004. _temp_error(H_CHAMBER, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  1005. #endif
  1006. #if WATCH_CHAMBER
  1007. // Make sure temperature is increasing
  1008. if (watch_chamber.elapsed(ms)) { // Time to check the chamber?
  1009. if (degChamber() < watch_chamber.target) // Failed to increase enough?
  1010. _temp_error(H_CHAMBER, str_t_heating_failed, GET_TEXT(MSG_HEATING_FAILED_LCD));
  1011. else
  1012. start_watching_chamber(); // Start again if the target is still far off
  1013. }
  1014. #endif
  1015. if (ELAPSED(ms, next_chamber_check_ms)) {
  1016. next_chamber_check_ms = ms + CHAMBER_CHECK_INTERVAL;
  1017. if (WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP)) {
  1018. #if ENABLED(CHAMBER_LIMIT_SWITCHING)
  1019. if (temp_chamber.celsius >= temp_chamber.target + TEMP_CHAMBER_HYSTERESIS)
  1020. temp_chamber.soft_pwm_amount = 0;
  1021. else if (temp_chamber.celsius <= temp_chamber.target - (TEMP_CHAMBER_HYSTERESIS))
  1022. temp_chamber.soft_pwm_amount = MAX_CHAMBER_POWER >> 1;
  1023. #else
  1024. temp_chamber.soft_pwm_amount = temp_chamber.celsius < temp_chamber.target ? MAX_CHAMBER_POWER >> 1 : 0;
  1025. #endif
  1026. }
  1027. else {
  1028. temp_chamber.soft_pwm_amount = 0;
  1029. WRITE_HEATER_CHAMBER(LOW);
  1030. }
  1031. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  1032. thermal_runaway_protection(tr_state_machine_chamber, temp_chamber.celsius, temp_chamber.target, H_CHAMBER, THERMAL_PROTECTION_CHAMBER_PERIOD, THERMAL_PROTECTION_CHAMBER_HYSTERESIS);
  1033. #endif
  1034. }
  1035. // TODO: Implement true PID pwm
  1036. //temp_bed.soft_pwm_amount = WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP) ? (int)get_pid_output_chamber() >> 1 : 0;
  1037. #endif // HAS_HEATED_CHAMBER
  1038. UNUSED(ms);
  1039. }
  1040. #define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
  1041. #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / float(HAL_ADC_RANGE) / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
  1042. /**
  1043. * Bisect search for the range of the 'raw' value, then interpolate
  1044. * proportionally between the under and over values.
  1045. */
  1046. #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
  1047. uint8_t l = 0, r = LEN, m; \
  1048. for (;;) { \
  1049. m = (l + r) >> 1; \
  1050. if (!m) return short(pgm_read_word(&TBL[0][1])); \
  1051. if (m == l || m == r) return short(pgm_read_word(&TBL[LEN-1][1])); \
  1052. short v00 = pgm_read_word(&TBL[m-1][0]), \
  1053. v10 = pgm_read_word(&TBL[m-0][0]); \
  1054. if (raw < v00) r = m; \
  1055. else if (raw > v10) l = m; \
  1056. else { \
  1057. const short v01 = short(pgm_read_word(&TBL[m-1][1])), \
  1058. v11 = short(pgm_read_word(&TBL[m-0][1])); \
  1059. return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00); \
  1060. } \
  1061. } \
  1062. }while(0)
  1063. #if HAS_USER_THERMISTORS
  1064. user_thermistor_t Temperature::user_thermistor[USER_THERMISTORS]; // Initialized by settings.load()
  1065. void Temperature::reset_user_thermistors() {
  1066. user_thermistor_t user_thermistor[USER_THERMISTORS] = {
  1067. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1068. { true, 0, 0, HOTEND0_PULLUP_RESISTOR_OHMS, HOTEND0_RESISTANCE_25C_OHMS, 0, 0, HOTEND0_BETA, 0 },
  1069. #endif
  1070. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1071. { true, 0, 0, HOTEND1_PULLUP_RESISTOR_OHMS, HOTEND1_RESISTANCE_25C_OHMS, 0, 0, HOTEND1_BETA, 0 },
  1072. #endif
  1073. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1074. { true, 0, 0, HOTEND2_PULLUP_RESISTOR_OHMS, HOTEND2_RESISTANCE_25C_OHMS, 0, 0, HOTEND2_BETA, 0 },
  1075. #endif
  1076. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1077. { true, 0, 0, HOTEND3_PULLUP_RESISTOR_OHMS, HOTEND3_RESISTANCE_25C_OHMS, 0, 0, HOTEND3_BETA, 0 },
  1078. #endif
  1079. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1080. { true, 0, 0, HOTEND4_PULLUP_RESISTOR_OHMS, HOTEND4_RESISTANCE_25C_OHMS, 0, 0, HOTEND4_BETA, 0 },
  1081. #endif
  1082. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1083. { true, 0, 0, HOTEND5_PULLUP_RESISTOR_OHMS, HOTEND5_RESISTANCE_25C_OHMS, 0, 0, HOTEND5_BETA, 0 },
  1084. #endif
  1085. #if ENABLED(HEATER_6_USER_THERMISTOR)
  1086. { true, 0, 0, HOTEND6_PULLUP_RESISTOR_OHMS, HOTEND6_RESISTANCE_25C_OHMS, 0, 0, HOTEND6_BETA, 0 },
  1087. #endif
  1088. #if ENABLED(HEATER_7_USER_THERMISTOR)
  1089. { true, 0, 0, HOTEND7_PULLUP_RESISTOR_OHMS, HOTEND7_RESISTANCE_25C_OHMS, 0, 0, HOTEND7_BETA, 0 },
  1090. #endif
  1091. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1092. { true, 0, 0, BED_PULLUP_RESISTOR_OHMS, BED_RESISTANCE_25C_OHMS, 0, 0, BED_BETA, 0 },
  1093. #endif
  1094. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1095. { true, 0, 0, CHAMBER_PULLUP_RESISTOR_OHMS, CHAMBER_RESISTANCE_25C_OHMS, 0, 0, CHAMBER_BETA, 0 }
  1096. #endif
  1097. };
  1098. COPY(thermalManager.user_thermistor, user_thermistor);
  1099. }
  1100. void Temperature::log_user_thermistor(const uint8_t t_index, const bool eprom/*=false*/) {
  1101. if (eprom)
  1102. SERIAL_ECHOPGM(" M305 ");
  1103. else
  1104. SERIAL_ECHO_START();
  1105. SERIAL_CHAR('P');
  1106. SERIAL_CHAR('0' + t_index);
  1107. const user_thermistor_t &t = user_thermistor[t_index];
  1108. SERIAL_ECHOPAIR_F(" R", t.series_res, 1);
  1109. SERIAL_ECHOPAIR_F_P(SP_T_STR, t.res_25, 1);
  1110. SERIAL_ECHOPAIR_F(" B", t.beta, 1);
  1111. SERIAL_ECHOPAIR_F(" C", t.sh_c_coeff, 9);
  1112. SERIAL_ECHOPGM(" ; ");
  1113. serialprintPGM(
  1114. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1115. t_index == CTI_HOTEND_0 ? PSTR("HOTEND 0") :
  1116. #endif
  1117. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1118. t_index == CTI_HOTEND_1 ? PSTR("HOTEND 1") :
  1119. #endif
  1120. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1121. t_index == CTI_HOTEND_2 ? PSTR("HOTEND 2") :
  1122. #endif
  1123. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1124. t_index == CTI_HOTEND_3 ? PSTR("HOTEND 3") :
  1125. #endif
  1126. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1127. t_index == CTI_HOTEND_4 ? PSTR("HOTEND 4") :
  1128. #endif
  1129. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1130. t_index == CTI_HOTEND_5 ? PSTR("HOTEND 5") :
  1131. #endif
  1132. #if ENABLED(HEATER_6_USER_THERMISTOR)
  1133. t_index == CTI_HOTEND_6 ? PSTR("HOTEND 6") :
  1134. #endif
  1135. #if ENABLED(HEATER_7_USER_THERMISTOR)
  1136. t_index == CTI_HOTEND_7 ? PSTR("HOTEND 7") :
  1137. #endif
  1138. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1139. t_index == CTI_BED ? PSTR("BED") :
  1140. #endif
  1141. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1142. t_index == CTI_CHAMBER ? PSTR("CHAMBER") :
  1143. #endif
  1144. nullptr
  1145. );
  1146. SERIAL_EOL();
  1147. }
  1148. float Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int raw) {
  1149. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1150. // static uint32_t clocks_total = 0;
  1151. // static uint32_t calls = 0;
  1152. // uint32_t tcnt5 = TCNT5;
  1153. //#endif
  1154. if (!WITHIN(t_index, 0, COUNT(user_thermistor) - 1)) return 25;
  1155. user_thermistor_t &t = user_thermistor[t_index];
  1156. if (t.pre_calc) { // pre-calculate some variables
  1157. t.pre_calc = false;
  1158. t.res_25_recip = 1.0f / t.res_25;
  1159. t.res_25_log = logf(t.res_25);
  1160. t.beta_recip = 1.0f / t.beta;
  1161. t.sh_alpha = RECIPROCAL(THERMISTOR_RESISTANCE_NOMINAL_C - (THERMISTOR_ABS_ZERO_C))
  1162. - (t.beta_recip * t.res_25_log) - (t.sh_c_coeff * cu(t.res_25_log));
  1163. }
  1164. // maximum adc value .. take into account the over sampling
  1165. const int adc_max = MAX_RAW_THERMISTOR_VALUE,
  1166. adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
  1167. const float adc_inverse = (adc_max - adc_raw) - 0.5f,
  1168. resistance = t.series_res * (adc_raw + 0.5f) / adc_inverse,
  1169. log_resistance = logf(resistance);
  1170. float value = t.sh_alpha;
  1171. value += log_resistance * t.beta_recip;
  1172. if (t.sh_c_coeff != 0)
  1173. value += t.sh_c_coeff * cu(log_resistance);
  1174. value = 1.0f / value;
  1175. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1176. // int32_t clocks = TCNT5 - tcnt5;
  1177. // if (clocks >= 0) {
  1178. // clocks_total += clocks;
  1179. // calls++;
  1180. // }
  1181. //#endif
  1182. // Return degrees C (up to 999, as the LCD only displays 3 digits)
  1183. return _MIN(value + THERMISTOR_ABS_ZERO_C, 999);
  1184. }
  1185. #endif
  1186. #if HOTENDS
  1187. // Derived from RepRap FiveD extruder::getTemperature()
  1188. // For hot end temperature measurement.
  1189. float Temperature::analog_to_celsius_hotend(const int raw, const uint8_t e) {
  1190. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1191. if (e > HOTENDS)
  1192. #else
  1193. if (e >= HOTENDS)
  1194. #endif
  1195. {
  1196. SERIAL_ERROR_START();
  1197. SERIAL_ECHO((int)e);
  1198. SERIAL_ECHOLNPGM(STR_INVALID_EXTRUDER_NUM);
  1199. kill();
  1200. return 0;
  1201. }
  1202. switch (e) {
  1203. case 0:
  1204. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1205. return user_thermistor_to_deg_c(CTI_HOTEND_0, raw);
  1206. #elif ENABLED(HEATER_0_USES_MAX6675)
  1207. return (
  1208. #if ENABLED(MAX6675_IS_MAX31865)
  1209. max31865.temperature(100, 400) // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1210. #else
  1211. raw * 0.25
  1212. #endif
  1213. );
  1214. #elif ENABLED(HEATER_0_USES_AD595)
  1215. return TEMP_AD595(raw);
  1216. #elif ENABLED(HEATER_0_USES_AD8495)
  1217. return TEMP_AD8495(raw);
  1218. #else
  1219. break;
  1220. #endif
  1221. case 1:
  1222. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1223. return user_thermistor_to_deg_c(CTI_HOTEND_1, raw);
  1224. #elif ENABLED(HEATER_1_USES_MAX6675)
  1225. return raw * 0.25;
  1226. #elif ENABLED(HEATER_1_USES_AD595)
  1227. return TEMP_AD595(raw);
  1228. #elif ENABLED(HEATER_1_USES_AD8495)
  1229. return TEMP_AD8495(raw);
  1230. #else
  1231. break;
  1232. #endif
  1233. case 2:
  1234. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1235. return user_thermistor_to_deg_c(CTI_HOTEND_2, raw);
  1236. #elif ENABLED(HEATER_2_USES_AD595)
  1237. return TEMP_AD595(raw);
  1238. #elif ENABLED(HEATER_2_USES_AD8495)
  1239. return TEMP_AD8495(raw);
  1240. #else
  1241. break;
  1242. #endif
  1243. case 3:
  1244. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1245. return user_thermistor_to_deg_c(CTI_HOTEND_3, raw);
  1246. #elif ENABLED(HEATER_3_USES_AD595)
  1247. return TEMP_AD595(raw);
  1248. #elif ENABLED(HEATER_3_USES_AD8495)
  1249. return TEMP_AD8495(raw);
  1250. #else
  1251. break;
  1252. #endif
  1253. case 4:
  1254. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1255. return user_thermistor_to_deg_c(CTI_HOTEND_4, raw);
  1256. #elif ENABLED(HEATER_4_USES_AD595)
  1257. return TEMP_AD595(raw);
  1258. #elif ENABLED(HEATER_4_USES_AD8495)
  1259. return TEMP_AD8495(raw);
  1260. #else
  1261. break;
  1262. #endif
  1263. case 5:
  1264. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1265. return user_thermistor_to_deg_c(CTI_HOTEND_5, raw);
  1266. #elif ENABLED(HEATER_5_USES_AD595)
  1267. return TEMP_AD595(raw);
  1268. #elif ENABLED(HEATER_5_USES_AD8495)
  1269. return TEMP_AD8495(raw);
  1270. #else
  1271. break;
  1272. #endif
  1273. case 6:
  1274. #if ENABLED(HEATER_6_USER_THERMISTOR)
  1275. return user_thermistor_to_deg_c(CTI_HOTEND_6, raw);
  1276. #elif ENABLED(HEATER_6_USES_AD595)
  1277. return TEMP_AD595(raw);
  1278. #elif ENABLED(HEATER_6_USES_AD8495)
  1279. return TEMP_AD8495(raw);
  1280. #else
  1281. break;
  1282. #endif
  1283. case 7:
  1284. #if ENABLED(HEATER_7_USER_THERMISTOR)
  1285. return user_thermistor_to_deg_c(CTI_HOTEND_7, raw);
  1286. #elif ENABLED(HEATER_7_USES_AD595)
  1287. return TEMP_AD595(raw);
  1288. #elif ENABLED(HEATER_7_USES_AD8495)
  1289. return TEMP_AD8495(raw);
  1290. #else
  1291. break;
  1292. #endif
  1293. default: break;
  1294. }
  1295. #if HOTEND_USES_THERMISTOR
  1296. // Thermistor with conversion table?
  1297. const short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  1298. SCAN_THERMISTOR_TABLE((*tt), heater_ttbllen_map[e]);
  1299. #endif
  1300. return 0;
  1301. }
  1302. #endif // HOTENDS
  1303. #if HAS_HEATED_BED
  1304. // Derived from RepRap FiveD extruder::getTemperature()
  1305. // For bed temperature measurement.
  1306. float Temperature::analog_to_celsius_bed(const int raw) {
  1307. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1308. return user_thermistor_to_deg_c(CTI_BED, raw);
  1309. #elif ENABLED(HEATER_BED_USES_THERMISTOR)
  1310. SCAN_THERMISTOR_TABLE(BED_TEMPTABLE, BED_TEMPTABLE_LEN);
  1311. #elif ENABLED(HEATER_BED_USES_AD595)
  1312. return TEMP_AD595(raw);
  1313. #elif ENABLED(HEATER_BED_USES_AD8495)
  1314. return TEMP_AD8495(raw);
  1315. #else
  1316. UNUSED(raw);
  1317. return 0;
  1318. #endif
  1319. }
  1320. #endif // HAS_HEATED_BED
  1321. #if HAS_TEMP_CHAMBER
  1322. // Derived from RepRap FiveD extruder::getTemperature()
  1323. // For chamber temperature measurement.
  1324. float Temperature::analog_to_celsius_chamber(const int raw) {
  1325. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1326. return user_thermistor_to_deg_c(CTI_CHAMBER, raw);
  1327. #elif ENABLED(HEATER_CHAMBER_USES_THERMISTOR)
  1328. SCAN_THERMISTOR_TABLE(CHAMBER_TEMPTABLE, CHAMBER_TEMPTABLE_LEN);
  1329. #elif ENABLED(HEATER_CHAMBER_USES_AD595)
  1330. return TEMP_AD595(raw);
  1331. #elif ENABLED(HEATER_CHAMBER_USES_AD8495)
  1332. return TEMP_AD8495(raw);
  1333. #else
  1334. UNUSED(raw);
  1335. return 0;
  1336. #endif
  1337. }
  1338. #endif // HAS_TEMP_CHAMBER
  1339. #if HAS_TEMP_PROBE
  1340. // Derived from RepRap FiveD extruder::getTemperature()
  1341. // For probe temperature measurement.
  1342. float Temperature::analog_to_celsius_probe(const int raw) {
  1343. #if ENABLED(PROBE_USER_THERMISTOR)
  1344. return user_thermistor_to_deg_c(CTI_PROBE, raw);
  1345. #elif ENABLED(PROBE_USES_THERMISTOR)
  1346. SCAN_THERMISTOR_TABLE(PROBE_TEMPTABLE, PROBE_TEMPTABLE_LEN);
  1347. #elif ENABLED(PROBE_USES_AD595)
  1348. return TEMP_AD595(raw);
  1349. #elif ENABLED(PROBE_USES_AD8495)
  1350. return TEMP_AD8495(raw);
  1351. #else
  1352. UNUSED(raw);
  1353. return 0;
  1354. #endif
  1355. }
  1356. #endif // HAS_TEMP_PROBE
  1357. /**
  1358. * Get the raw values into the actual temperatures.
  1359. * The raw values are created in interrupt context,
  1360. * and this function is called from normal context
  1361. * as it would block the stepper routine.
  1362. */
  1363. void Temperature::updateTemperaturesFromRawValues() {
  1364. #if ENABLED(HEATER_0_USES_MAX6675)
  1365. temp_hotend[0].raw = READ_MAX6675(0);
  1366. #endif
  1367. #if ENABLED(HEATER_1_USES_MAX6675)
  1368. temp_hotend[1].raw = READ_MAX6675(1);
  1369. #endif
  1370. #if HOTENDS
  1371. HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e);
  1372. #endif
  1373. #if HAS_HEATED_BED
  1374. temp_bed.celsius = analog_to_celsius_bed(temp_bed.raw);
  1375. #endif
  1376. #if HAS_TEMP_CHAMBER
  1377. temp_chamber.celsius = analog_to_celsius_chamber(temp_chamber.raw);
  1378. #endif
  1379. #if HAS_TEMP_PROBE
  1380. temp_probe.celsius = analog_to_celsius_probe(temp_probe.raw);
  1381. #endif
  1382. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1383. redundant_temperature = analog_to_celsius_hotend(redundant_temperature_raw, 1);
  1384. #endif
  1385. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1386. filwidth.update_measured_mm();
  1387. #endif
  1388. // Reset the watchdog on good temperature measurement
  1389. watchdog_refresh();
  1390. raw_temps_ready = false;
  1391. }
  1392. #if MAX6675_SEPARATE_SPI
  1393. SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  1394. #endif
  1395. // Init fans according to whether they're native PWM or Software PWM
  1396. #ifdef ALFAWISE_UX0
  1397. #define _INIT_SOFT_FAN(P) OUT_WRITE_OD(P, FAN_INVERTING ? LOW : HIGH)
  1398. #else
  1399. #define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH)
  1400. #endif
  1401. #if ENABLED(FAN_SOFT_PWM)
  1402. #define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P)
  1403. #else
  1404. #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0)
  1405. #endif
  1406. #if ENABLED(FAST_PWM_FAN)
  1407. #define SET_FAST_PWM_FREQ(P) set_pwm_frequency(P, FAST_PWM_FAN_FREQUENCY)
  1408. #else
  1409. #define SET_FAST_PWM_FREQ(P) NOOP
  1410. #endif
  1411. #define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0)
  1412. #if EXTRUDER_AUTO_FAN_SPEED != 255
  1413. #define INIT_E_AUTO_FAN_PIN(P) do{ if (P == FAN1_PIN || P == FAN2_PIN) { SET_PWM(P); SET_FAST_PWM_FREQ(FAST_PWM_FAN_FREQUENCY); } else SET_OUTPUT(P); }while(0)
  1414. #else
  1415. #define INIT_E_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1416. #endif
  1417. #if CHAMBER_AUTO_FAN_SPEED != 255
  1418. #define INIT_CHAMBER_AUTO_FAN_PIN(P) do{ if (P == FAN1_PIN || P == FAN2_PIN) { SET_PWM(P); SET_FAST_PWM_FREQ(FAST_PWM_FAN_FREQUENCY); } else SET_OUTPUT(P); }while(0)
  1419. #else
  1420. #define INIT_CHAMBER_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1421. #endif
  1422. /**
  1423. * Initialize the temperature manager
  1424. * The manager is implemented by periodic calls to manage_heater()
  1425. */
  1426. void Temperature::init() {
  1427. #if ENABLED(MAX6675_IS_MAX31865)
  1428. max31865.begin(MAX31865_2WIRE); // MAX31865_2WIRE, MAX31865_3WIRE, MAX31865_4WIRE
  1429. #endif
  1430. #if EARLY_WATCHDOG
  1431. // Flag that the thermalManager should be running
  1432. if (inited) return;
  1433. inited = true;
  1434. #endif
  1435. #if MB(RUMBA)
  1436. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  1437. #define _AD(N) ANY(HEATER_##N##_USES_AD595, HEATER_##N##_USES_AD8495)
  1438. #if _AD(0) || _AD(1) || _AD(2) || _AD(BED) || _AD(CHAMBER)
  1439. MCUCR = _BV(JTD);
  1440. MCUCR = _BV(JTD);
  1441. #endif
  1442. #endif
  1443. #if BOTH(PIDTEMP, PID_EXTRUSION_SCALING)
  1444. last_e_position = 0;
  1445. #endif
  1446. #if HAS_HEATER_0
  1447. #ifdef ALFAWISE_UX0
  1448. OUT_WRITE_OD(HEATER_0_PIN, HEATER_0_INVERTING);
  1449. #else
  1450. OUT_WRITE(HEATER_0_PIN, HEATER_0_INVERTING);
  1451. #endif
  1452. #endif
  1453. #if HAS_HEATER_1
  1454. OUT_WRITE(HEATER_1_PIN, HEATER_1_INVERTING);
  1455. #endif
  1456. #if HAS_HEATER_2
  1457. OUT_WRITE(HEATER_2_PIN, HEATER_2_INVERTING);
  1458. #endif
  1459. #if HAS_HEATER_3
  1460. OUT_WRITE(HEATER_3_PIN, HEATER_3_INVERTING);
  1461. #endif
  1462. #if HAS_HEATER_4
  1463. OUT_WRITE(HEATER_4_PIN, HEATER_4_INVERTING);
  1464. #endif
  1465. #if HAS_HEATER_5
  1466. OUT_WRITE(HEATER_5_PIN, HEATER_5_INVERTING);
  1467. #endif
  1468. #if HAS_HEATER_6
  1469. OUT_WRITE(HEATER_6_PIN, HEATER_6_INVERTING);
  1470. #endif
  1471. #if HAS_HEATER_7
  1472. OUT_WRITE(HEATER_7_PIN, HEATER_7_INVERTING);
  1473. #endif
  1474. #if HAS_HEATED_BED
  1475. #ifdef ALFAWISE_UX0
  1476. OUT_WRITE_OD(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1477. #else
  1478. OUT_WRITE(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1479. #endif
  1480. #endif
  1481. #if HAS_HEATED_CHAMBER
  1482. OUT_WRITE(HEATER_CHAMBER_PIN, HEATER_CHAMBER_INVERTING);
  1483. #endif
  1484. #if HAS_FAN0
  1485. INIT_FAN_PIN(FAN_PIN);
  1486. #endif
  1487. #if HAS_FAN1
  1488. INIT_FAN_PIN(FAN1_PIN);
  1489. #endif
  1490. #if HAS_FAN2
  1491. INIT_FAN_PIN(FAN2_PIN);
  1492. #endif
  1493. #if HAS_FAN3
  1494. INIT_FAN_PIN(FAN3_PIN);
  1495. #endif
  1496. #if HAS_FAN4
  1497. INIT_FAN_PIN(FAN4_PIN);
  1498. #endif
  1499. #if HAS_FAN5
  1500. INIT_FAN_PIN(FAN5_PIN);
  1501. #endif
  1502. #if HAS_FAN6
  1503. INIT_FAN_PIN(FAN6_PIN);
  1504. #endif
  1505. #if HAS_FAN7
  1506. INIT_FAN_PIN(FAN7_PIN);
  1507. #endif
  1508. #if ENABLED(USE_CONTROLLER_FAN)
  1509. INIT_FAN_PIN(CONTROLLER_FAN_PIN);
  1510. #endif
  1511. #if MAX6675_SEPARATE_SPI
  1512. OUT_WRITE(SCK_PIN, LOW);
  1513. OUT_WRITE(MOSI_PIN, HIGH);
  1514. SET_INPUT_PULLUP(MISO_PIN);
  1515. max6675_spi.init();
  1516. OUT_WRITE(SS_PIN, HIGH);
  1517. OUT_WRITE(MAX6675_SS_PIN, HIGH);
  1518. #endif
  1519. #if ENABLED(HEATER_1_USES_MAX6675)
  1520. OUT_WRITE(MAX6675_SS2_PIN, HIGH);
  1521. #endif
  1522. HAL_adc_init();
  1523. #if HAS_TEMP_ADC_0
  1524. HAL_ANALOG_SELECT(TEMP_0_PIN);
  1525. #endif
  1526. #if HAS_TEMP_ADC_1
  1527. HAL_ANALOG_SELECT(TEMP_1_PIN);
  1528. #endif
  1529. #if HAS_TEMP_ADC_2
  1530. HAL_ANALOG_SELECT(TEMP_2_PIN);
  1531. #endif
  1532. #if HAS_TEMP_ADC_3
  1533. HAL_ANALOG_SELECT(TEMP_3_PIN);
  1534. #endif
  1535. #if HAS_TEMP_ADC_4
  1536. HAL_ANALOG_SELECT(TEMP_4_PIN);
  1537. #endif
  1538. #if HAS_TEMP_ADC_5
  1539. HAL_ANALOG_SELECT(TEMP_5_PIN);
  1540. #endif
  1541. #if HAS_TEMP_ADC_6
  1542. HAL_ANALOG_SELECT(TEMP_6_PIN);
  1543. #endif
  1544. #if HAS_TEMP_ADC_7
  1545. HAL_ANALOG_SELECT(TEMP_7_PIN);
  1546. #endif
  1547. #if HAS_JOY_ADC_X
  1548. HAL_ANALOG_SELECT(JOY_X_PIN);
  1549. #endif
  1550. #if HAS_JOY_ADC_Y
  1551. HAL_ANALOG_SELECT(JOY_Y_PIN);
  1552. #endif
  1553. #if HAS_JOY_ADC_Z
  1554. HAL_ANALOG_SELECT(JOY_Z_PIN);
  1555. #endif
  1556. #if HAS_JOY_ADC_EN
  1557. SET_INPUT_PULLUP(JOY_EN_PIN);
  1558. #endif
  1559. #if HAS_HEATED_BED
  1560. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  1561. #endif
  1562. #if HAS_TEMP_CHAMBER
  1563. HAL_ANALOG_SELECT(TEMP_CHAMBER_PIN);
  1564. #endif
  1565. #if HAS_TEMP_PROBE
  1566. HAL_ANALOG_SELECT(TEMP_PROBE_PIN);
  1567. #endif
  1568. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1569. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  1570. #endif
  1571. #if HAS_ADC_BUTTONS
  1572. HAL_ANALOG_SELECT(ADC_KEYPAD_PIN);
  1573. #endif
  1574. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  1575. ENABLE_TEMPERATURE_INTERRUPT();
  1576. #if HAS_AUTO_FAN_0
  1577. INIT_E_AUTO_FAN_PIN(E0_AUTO_FAN_PIN);
  1578. #endif
  1579. #if HAS_AUTO_FAN_1 && !_EFANOVERLAP(1,0)
  1580. INIT_E_AUTO_FAN_PIN(E1_AUTO_FAN_PIN);
  1581. #endif
  1582. #if HAS_AUTO_FAN_2 && !(_EFANOVERLAP(2,0) || _EFANOVERLAP(2,1))
  1583. INIT_E_AUTO_FAN_PIN(E2_AUTO_FAN_PIN);
  1584. #endif
  1585. #if HAS_AUTO_FAN_3 && !(_EFANOVERLAP(3,0) || _EFANOVERLAP(3,1) || _EFANOVERLAP(3,2))
  1586. INIT_E_AUTO_FAN_PIN(E3_AUTO_FAN_PIN);
  1587. #endif
  1588. #if HAS_AUTO_FAN_4 && !(_EFANOVERLAP(4,0) || _EFANOVERLAP(4,1) || _EFANOVERLAP(4,2) || _EFANOVERLAP(4,3))
  1589. INIT_E_AUTO_FAN_PIN(E4_AUTO_FAN_PIN);
  1590. #endif
  1591. #if HAS_AUTO_FAN_5 && !(_EFANOVERLAP(5,0) || _EFANOVERLAP(5,1) || _EFANOVERLAP(5,2) || _EFANOVERLAP(5,3) || _EFANOVERLAP(5,4))
  1592. INIT_E_AUTO_FAN_PIN(E5_AUTO_FAN_PIN);
  1593. #endif
  1594. #if HAS_AUTO_FAN_6 && !(_EFANOVERLAP(6,0) || _EFANOVERLAP(6,1) || _EFANOVERLAP(6,2) || _EFANOVERLAP(6,3) || _EFANOVERLAP(6,4) || _EFANOVERLAP(6,5))
  1595. INIT_E_AUTO_FAN_PIN(E6_AUTO_FAN_PIN);
  1596. #endif
  1597. #if HAS_AUTO_FAN_7 && !(_EFANOVERLAP(7,0) || _EFANOVERLAP(7,1) || _EFANOVERLAP(7,2) || _EFANOVERLAP(7,3) || _EFANOVERLAP(7,4) || _EFANOVERLAP(7,5) || _EFANOVERLAP(7,6))
  1598. INIT_E_AUTO_FAN_PIN(E7_AUTO_FAN_PIN);
  1599. #endif
  1600. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_E
  1601. INIT_CHAMBER_AUTO_FAN_PIN(CHAMBER_AUTO_FAN_PIN);
  1602. #endif
  1603. // Wait for temperature measurement to settle
  1604. delay(250);
  1605. #if HOTENDS
  1606. #define _TEMP_MIN_E(NR) do{ \
  1607. temp_range[NR].mintemp = HEATER_ ##NR## _MINTEMP; \
  1608. while (analog_to_celsius_hotend(temp_range[NR].raw_min, NR) < HEATER_ ##NR## _MINTEMP) \
  1609. temp_range[NR].raw_min += TEMPDIR(NR) * (OVERSAMPLENR); \
  1610. }while(0)
  1611. #define _TEMP_MAX_E(NR) do{ \
  1612. temp_range[NR].maxtemp = HEATER_ ##NR## _MAXTEMP; \
  1613. while (analog_to_celsius_hotend(temp_range[NR].raw_max, NR) > HEATER_ ##NR## _MAXTEMP) \
  1614. temp_range[NR].raw_max -= TEMPDIR(NR) * (OVERSAMPLENR); \
  1615. }while(0)
  1616. #ifdef HEATER_0_MINTEMP
  1617. _TEMP_MIN_E(0);
  1618. #endif
  1619. #ifdef HEATER_0_MAXTEMP
  1620. _TEMP_MAX_E(0);
  1621. #endif
  1622. #if HOTENDS > 1
  1623. #ifdef HEATER_1_MINTEMP
  1624. _TEMP_MIN_E(1);
  1625. #endif
  1626. #ifdef HEATER_1_MAXTEMP
  1627. _TEMP_MAX_E(1);
  1628. #endif
  1629. #if HOTENDS > 2
  1630. #ifdef HEATER_2_MINTEMP
  1631. _TEMP_MIN_E(2);
  1632. #endif
  1633. #ifdef HEATER_2_MAXTEMP
  1634. _TEMP_MAX_E(2);
  1635. #endif
  1636. #if HOTENDS > 3
  1637. #ifdef HEATER_3_MINTEMP
  1638. _TEMP_MIN_E(3);
  1639. #endif
  1640. #ifdef HEATER_3_MAXTEMP
  1641. _TEMP_MAX_E(3);
  1642. #endif
  1643. #if HOTENDS > 4
  1644. #ifdef HEATER_4_MINTEMP
  1645. _TEMP_MIN_E(4);
  1646. #endif
  1647. #ifdef HEATER_4_MAXTEMP
  1648. _TEMP_MAX_E(4);
  1649. #endif
  1650. #if HOTENDS > 5
  1651. #ifdef HEATER_5_MINTEMP
  1652. _TEMP_MIN_E(5);
  1653. #endif
  1654. #ifdef HEATER_5_MAXTEMP
  1655. _TEMP_MAX_E(5);
  1656. #endif
  1657. #if HOTENDS > 6
  1658. #ifdef HEATER_6_MINTEMP
  1659. _TEMP_MIN_E(6);
  1660. #endif
  1661. #ifdef HEATER_6_MAXTEMP
  1662. _TEMP_MAX_E(6);
  1663. #endif
  1664. #if HOTENDS > 7
  1665. #ifdef HEATER_7_MINTEMP
  1666. _TEMP_MIN_E(7);
  1667. #endif
  1668. #ifdef HEATER_7_MAXTEMP
  1669. _TEMP_MAX_E(7);
  1670. #endif
  1671. #endif // HOTENDS > 7
  1672. #endif // HOTENDS > 6
  1673. #endif // HOTENDS > 5
  1674. #endif // HOTENDS > 4
  1675. #endif // HOTENDS > 3
  1676. #endif // HOTENDS > 2
  1677. #endif // HOTENDS > 1
  1678. #endif // HOTENDS
  1679. #if HAS_HEATED_BED
  1680. #ifdef BED_MINTEMP
  1681. while (analog_to_celsius_bed(mintemp_raw_BED) < BED_MINTEMP) mintemp_raw_BED += TEMPDIR(BED) * (OVERSAMPLENR);
  1682. #endif
  1683. #ifdef BED_MAXTEMP
  1684. while (analog_to_celsius_bed(maxtemp_raw_BED) > BED_MAXTEMP) maxtemp_raw_BED -= TEMPDIR(BED) * (OVERSAMPLENR);
  1685. #endif
  1686. #endif // HAS_HEATED_BED
  1687. #if HAS_HEATED_CHAMBER
  1688. #ifdef CHAMBER_MINTEMP
  1689. while (analog_to_celsius_chamber(mintemp_raw_CHAMBER) < CHAMBER_MINTEMP) mintemp_raw_CHAMBER += TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1690. #endif
  1691. #ifdef CHAMBER_MAXTEMP
  1692. while (analog_to_celsius_chamber(maxtemp_raw_CHAMBER) > CHAMBER_MAXTEMP) maxtemp_raw_CHAMBER -= TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1693. #endif
  1694. #endif
  1695. #if ENABLED(PROBING_HEATERS_OFF)
  1696. paused = false;
  1697. #endif
  1698. }
  1699. #if WATCH_HOTENDS
  1700. /**
  1701. * Start Heating Sanity Check for hotends that are below
  1702. * their target temperature by a configurable margin.
  1703. * This is called when the temperature is set. (M104, M109)
  1704. */
  1705. void Temperature::start_watching_hotend(const uint8_t E_NAME) {
  1706. const uint8_t ee = HOTEND_INDEX;
  1707. watch_hotend[ee].restart(degHotend(ee), degTargetHotend(ee));
  1708. }
  1709. #endif
  1710. #if WATCH_BED
  1711. /**
  1712. * Start Heating Sanity Check for hotends that are below
  1713. * their target temperature by a configurable margin.
  1714. * This is called when the temperature is set. (M140, M190)
  1715. */
  1716. void Temperature::start_watching_bed() {
  1717. watch_bed.restart(degBed(), degTargetBed());
  1718. }
  1719. #endif
  1720. #if WATCH_CHAMBER
  1721. /**
  1722. * Start Heating Sanity Check for chamber that is below
  1723. * its target temperature by a configurable margin.
  1724. * This is called when the temperature is set. (M141, M191)
  1725. */
  1726. void Temperature::start_watching_chamber() {
  1727. watch_chamber.restart(degChamber(), degTargetChamber());
  1728. }
  1729. #endif
  1730. #if HAS_THERMAL_PROTECTION
  1731. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1732. Temperature::tr_state_machine_t Temperature::tr_state_machine[HOTENDS]; // = { { TRInactive, 0 } };
  1733. #endif
  1734. #if HAS_THERMALLY_PROTECTED_BED
  1735. Temperature::tr_state_machine_t Temperature::tr_state_machine_bed; // = { TRInactive, 0 };
  1736. #endif
  1737. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  1738. Temperature::tr_state_machine_t Temperature::tr_state_machine_chamber; // = { TRInactive, 0 };
  1739. #endif
  1740. void Temperature::thermal_runaway_protection(Temperature::tr_state_machine_t &sm, const float &current, const float &target, const heater_ind_t heater_id, const uint16_t period_seconds, const uint16_t hysteresis_degc) {
  1741. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1742. /**
  1743. SERIAL_ECHO_START();
  1744. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1745. if (heater_id == H_CHAMBER) SERIAL_ECHOPGM("chamber");
  1746. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1747. SERIAL_ECHOPAIR(" ; State:", sm.state, " ; Timer:", sm.timer, " ; Temperature:", current, " ; Target Temp:", target);
  1748. if (heater_id >= 0)
  1749. SERIAL_ECHOPAIR(" ; Idle Timeout:", hotend_idle[heater_id].timed_out);
  1750. else
  1751. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle.timed_out);
  1752. SERIAL_EOL();
  1753. //*/
  1754. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1755. #if HEATER_IDLE_HANDLER
  1756. // If the heater idle timeout expires, restart
  1757. if ((heater_id >= 0 && hotend_idle[heater_id].timed_out)
  1758. #if HAS_HEATED_BED
  1759. || (heater_id < 0 && bed_idle.timed_out)
  1760. #endif
  1761. ) {
  1762. sm.state = TRInactive;
  1763. tr_target_temperature[heater_index] = 0;
  1764. }
  1765. else
  1766. #endif
  1767. {
  1768. // If the target temperature changes, restart
  1769. if (tr_target_temperature[heater_index] != target) {
  1770. tr_target_temperature[heater_index] = target;
  1771. sm.state = target > 0 ? TRFirstHeating : TRInactive;
  1772. }
  1773. }
  1774. switch (sm.state) {
  1775. // Inactive state waits for a target temperature to be set
  1776. case TRInactive: break;
  1777. // When first heating, wait for the temperature to be reached then go to Stable state
  1778. case TRFirstHeating:
  1779. if (current < tr_target_temperature[heater_index]) break;
  1780. sm.state = TRStable;
  1781. // While the temperature is stable watch for a bad temperature
  1782. case TRStable:
  1783. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  1784. if (adaptive_fan_slowing && heater_id >= 0) {
  1785. const int fan_index = _MIN(heater_id, FAN_COUNT - 1);
  1786. if (fan_speed[fan_index] == 0 || current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.25f))
  1787. fan_speed_scaler[fan_index] = 128;
  1788. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.3335f))
  1789. fan_speed_scaler[fan_index] = 96;
  1790. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.5f))
  1791. fan_speed_scaler[fan_index] = 64;
  1792. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.8f))
  1793. fan_speed_scaler[fan_index] = 32;
  1794. else
  1795. fan_speed_scaler[fan_index] = 0;
  1796. }
  1797. #endif
  1798. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1799. sm.timer = millis() + period_seconds * 1000UL;
  1800. break;
  1801. }
  1802. else if (PENDING(millis(), sm.timer)) break;
  1803. sm.state = TRRunaway;
  1804. case TRRunaway:
  1805. _temp_error(heater_id, str_t_thermal_runaway, GET_TEXT(MSG_THERMAL_RUNAWAY));
  1806. }
  1807. }
  1808. #endif // HAS_THERMAL_PROTECTION
  1809. void Temperature::disable_all_heaters() {
  1810. #if ENABLED(AUTOTEMP)
  1811. planner.autotemp_enabled = false;
  1812. #endif
  1813. #if HOTENDS
  1814. HOTEND_LOOP() setTargetHotend(0, e);
  1815. #endif
  1816. #if HAS_HEATED_BED
  1817. setTargetBed(0);
  1818. #endif
  1819. #if HAS_HEATED_CHAMBER
  1820. setTargetChamber(0);
  1821. #endif
  1822. // Unpause and reset everything
  1823. #if ENABLED(PROBING_HEATERS_OFF)
  1824. pause(false);
  1825. #endif
  1826. #define DISABLE_HEATER(N) { \
  1827. setTargetHotend(0, N); \
  1828. temp_hotend[N].soft_pwm_amount = 0; \
  1829. WRITE_HEATER_##N(LOW); \
  1830. }
  1831. #if HAS_TEMP_HOTEND
  1832. REPEAT(HOTENDS, DISABLE_HEATER);
  1833. #endif
  1834. #if HAS_HEATED_BED
  1835. temp_bed.target = 0;
  1836. temp_bed.soft_pwm_amount = 0;
  1837. WRITE_HEATER_BED(LOW);
  1838. #endif
  1839. #if HAS_HEATED_CHAMBER
  1840. temp_chamber.target = 0;
  1841. temp_chamber.soft_pwm_amount = 0;
  1842. WRITE_HEATER_CHAMBER(LOW);
  1843. #endif
  1844. }
  1845. #if ENABLED(PRINTJOB_TIMER_AUTOSTART)
  1846. bool Temperature::over_autostart_threshold() {
  1847. #if HOTENDS
  1848. HOTEND_LOOP() if (degTargetHotend(e) > (EXTRUDE_MINTEMP) / 2) return true;
  1849. #endif
  1850. #if HAS_HEATED_BED
  1851. if (degTargetBed() > BED_MINTEMP) return true;
  1852. #endif
  1853. #if HAS_HEATED_CHAMBER
  1854. if (degTargetChamber() > CHAMBER_MINTEMP) return true;
  1855. #endif
  1856. return false;
  1857. }
  1858. void Temperature::check_timer_autostart(const bool can_start, const bool can_stop) {
  1859. if (over_autostart_threshold()) {
  1860. if (can_start) startOrResumeJob();
  1861. }
  1862. else if (can_stop) {
  1863. print_job_timer.stop();
  1864. ui.reset_status();
  1865. }
  1866. }
  1867. #endif
  1868. #if ENABLED(PROBING_HEATERS_OFF)
  1869. void Temperature::pause(const bool p) {
  1870. if (p != paused) {
  1871. paused = p;
  1872. if (p) {
  1873. HOTEND_LOOP() hotend_idle[e].expire(); // timeout immediately
  1874. #if HAS_HEATED_BED
  1875. bed_idle.expire(); // timeout immediately
  1876. #endif
  1877. }
  1878. else {
  1879. HOTEND_LOOP() reset_hotend_idle_timer(e);
  1880. #if HAS_HEATED_BED
  1881. reset_bed_idle_timer();
  1882. #endif
  1883. }
  1884. }
  1885. }
  1886. #endif // PROBING_HEATERS_OFF
  1887. #if HAS_MAX6675
  1888. int Temperature::read_max6675(
  1889. #if COUNT_6675 > 1
  1890. const uint8_t hindex
  1891. #endif
  1892. ) {
  1893. #if COUNT_6675 == 1
  1894. constexpr uint8_t hindex = 0;
  1895. #else
  1896. // Needed to return the correct temp when this is called too soon
  1897. static uint16_t max6675_temp_previous[COUNT_6675] = { 0 };
  1898. #endif
  1899. #define MAX6675_HEAT_INTERVAL 250UL
  1900. #if ENABLED(MAX6675_IS_MAX31855)
  1901. static uint32_t max6675_temp = 2000;
  1902. #define MAX6675_ERROR_MASK 7
  1903. #define MAX6675_DISCARD_BITS 18
  1904. #define MAX6675_SPEED_BITS 3 // (_BV(SPR1)) // clock ÷ 64
  1905. #else
  1906. static uint16_t max6675_temp = 2000;
  1907. #define MAX6675_ERROR_MASK 4
  1908. #define MAX6675_DISCARD_BITS 3
  1909. #define MAX6675_SPEED_BITS 2 // (_BV(SPR0)) // clock ÷ 16
  1910. #endif
  1911. // Return last-read value between readings
  1912. static millis_t next_max6675_ms[COUNT_6675] = { 0 };
  1913. millis_t ms = millis();
  1914. if (PENDING(ms, next_max6675_ms[hindex]))
  1915. return int(
  1916. #if COUNT_6675 == 1
  1917. max6675_temp
  1918. #else
  1919. max6675_temp_previous[hindex] // Need to return the correct previous value
  1920. #endif
  1921. );
  1922. next_max6675_ms[hindex] = ms + MAX6675_HEAT_INTERVAL;
  1923. #if ENABLED(MAX6675_IS_MAX31865)
  1924. max6675_temp = int(max31865.temperature(100, 400)); // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1925. #endif
  1926. //
  1927. // TODO: spiBegin, spiRec and spiInit doesn't work when soft spi is used.
  1928. //
  1929. #if !MAX6675_SEPARATE_SPI
  1930. spiBegin();
  1931. spiInit(MAX6675_SPEED_BITS);
  1932. #endif
  1933. #if COUNT_6675 > 1
  1934. #define WRITE_MAX6675(V) do{ switch (hindex) { case 1: WRITE(MAX6675_SS2_PIN, V); break; default: WRITE(MAX6675_SS_PIN, V); } }while(0)
  1935. #define SET_OUTPUT_MAX6675() do{ switch (hindex) { case 1: SET_OUTPUT(MAX6675_SS2_PIN); break; default: SET_OUTPUT(MAX6675_SS_PIN); } }while(0)
  1936. #elif ENABLED(HEATER_1_USES_MAX6675)
  1937. #define WRITE_MAX6675(V) WRITE(MAX6675_SS2_PIN, V)
  1938. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS2_PIN)
  1939. #else
  1940. #define WRITE_MAX6675(V) WRITE(MAX6675_SS_PIN, V)
  1941. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS_PIN)
  1942. #endif
  1943. SET_OUTPUT_MAX6675();
  1944. WRITE_MAX6675(LOW); // enable TT_MAX6675
  1945. DELAY_NS(100); // Ensure 100ns delay
  1946. // Read a big-endian temperature value
  1947. max6675_temp = 0;
  1948. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1949. max6675_temp |= (
  1950. #if MAX6675_SEPARATE_SPI
  1951. max6675_spi.receive()
  1952. #else
  1953. spiRec()
  1954. #endif
  1955. );
  1956. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1957. }
  1958. WRITE_MAX6675(HIGH); // disable TT_MAX6675
  1959. if (max6675_temp & MAX6675_ERROR_MASK) {
  1960. SERIAL_ERROR_START();
  1961. SERIAL_ECHOPGM("Temp measurement error! ");
  1962. #if MAX6675_ERROR_MASK == 7
  1963. SERIAL_ECHOPGM("MAX31855 ");
  1964. if (max6675_temp & 1)
  1965. SERIAL_ECHOLNPGM("Open Circuit");
  1966. else if (max6675_temp & 2)
  1967. SERIAL_ECHOLNPGM("Short to GND");
  1968. else if (max6675_temp & 4)
  1969. SERIAL_ECHOLNPGM("Short to VCC");
  1970. #else
  1971. SERIAL_ECHOLNPGM("MAX6675");
  1972. #endif
  1973. // Thermocouple open
  1974. max6675_temp = 4 * (
  1975. #if COUNT_6675 > 1
  1976. hindex ? HEATER_1_MAX6675_TMAX : HEATER_0_MAX6675_TMAX
  1977. #elif ENABLED(HEATER_1_USES_MAX6675)
  1978. HEATER_1_MAX6675_TMAX
  1979. #else
  1980. HEATER_0_MAX6675_TMAX
  1981. #endif
  1982. );
  1983. }
  1984. else
  1985. max6675_temp >>= MAX6675_DISCARD_BITS;
  1986. #if ENABLED(MAX6675_IS_MAX31855)
  1987. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000; // Support negative temperature
  1988. #endif
  1989. #if COUNT_6675 > 1
  1990. max6675_temp_previous[hindex] = max6675_temp;
  1991. #endif
  1992. return int(max6675_temp);
  1993. }
  1994. #endif // HAS_MAX6675
  1995. /**
  1996. * Update raw temperatures
  1997. */
  1998. void Temperature::update_raw_temperatures() {
  1999. #if HAS_TEMP_ADC_0 && DISABLED(HEATER_0_USES_MAX6675)
  2000. temp_hotend[0].update();
  2001. #endif
  2002. #if HAS_TEMP_ADC_1
  2003. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2004. redundant_temperature_raw = temp_hotend[1].acc;
  2005. #elif DISABLED(HEATER_1_USES_MAX6675)
  2006. temp_hotend[1].update();
  2007. #endif
  2008. #endif
  2009. #if HAS_TEMP_ADC_2
  2010. temp_hotend[2].update();
  2011. #endif
  2012. #if HAS_TEMP_ADC_3
  2013. temp_hotend[3].update();
  2014. #endif
  2015. #if HAS_TEMP_ADC_4
  2016. temp_hotend[4].update();
  2017. #endif
  2018. #if HAS_TEMP_ADC_5
  2019. temp_hotend[5].update();
  2020. #endif
  2021. #if HAS_TEMP_ADC_6
  2022. temp_hotend[6].update();
  2023. #endif
  2024. #if HAS_TEMP_ADC_7
  2025. temp_hotend[7].update();
  2026. #endif
  2027. #if HAS_HEATED_BED
  2028. temp_bed.update();
  2029. #endif
  2030. #if HAS_TEMP_CHAMBER
  2031. temp_chamber.update();
  2032. #endif
  2033. #if HAS_TEMP_PROBE
  2034. temp_probe.update();
  2035. #endif
  2036. #if HAS_JOY_ADC_X
  2037. joystick.x.update();
  2038. #endif
  2039. #if HAS_JOY_ADC_Y
  2040. joystick.y.update();
  2041. #endif
  2042. #if HAS_JOY_ADC_Z
  2043. joystick.z.update();
  2044. #endif
  2045. raw_temps_ready = true;
  2046. }
  2047. void Temperature::readings_ready() {
  2048. // Update the raw values if they've been read. Else we could be updating them during reading.
  2049. if (!raw_temps_ready) update_raw_temperatures();
  2050. // Filament Sensor - can be read any time since IIR filtering is used
  2051. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  2052. filwidth.reading_ready();
  2053. #endif
  2054. #if HOTENDS
  2055. HOTEND_LOOP() temp_hotend[e].reset();
  2056. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2057. temp_hotend[1].reset();
  2058. #endif
  2059. #endif
  2060. #if HAS_HEATED_BED
  2061. temp_bed.reset();
  2062. #endif
  2063. #if HAS_TEMP_CHAMBER
  2064. temp_chamber.reset();
  2065. #endif
  2066. #if HAS_TEMP_PROBE
  2067. temp_probe.reset();
  2068. #endif
  2069. #if HAS_JOY_ADC_X
  2070. joystick.x.reset();
  2071. #endif
  2072. #if HAS_JOY_ADC_Y
  2073. joystick.y.reset();
  2074. #endif
  2075. #if HAS_JOY_ADC_Z
  2076. joystick.z.reset();
  2077. #endif
  2078. #if HOTENDS
  2079. static constexpr int8_t temp_dir[] = {
  2080. #if ENABLED(HEATER_0_USES_MAX6675)
  2081. 0
  2082. #else
  2083. TEMPDIR(0)
  2084. #endif
  2085. #if HOTENDS > 1
  2086. #define _TEMPDIR(N) , TEMPDIR(N)
  2087. #if ENABLED(HEATER_1_USES_MAX6675)
  2088. , 0
  2089. #else
  2090. _TEMPDIR(1)
  2091. #endif
  2092. #if HOTENDS > 2
  2093. REPEAT_S(2, HOTENDS, _TEMPDIR)
  2094. #endif // HOTENDS > 2
  2095. #endif // HOTENDS > 1
  2096. };
  2097. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  2098. const int8_t tdir = temp_dir[e];
  2099. if (tdir) {
  2100. const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
  2101. const bool heater_on = (temp_hotend[e].target > 0
  2102. #if ENABLED(PIDTEMP)
  2103. || temp_hotend[e].soft_pwm_amount > 0
  2104. #endif
  2105. );
  2106. if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_ind_t)e);
  2107. if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
  2108. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  2109. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  2110. #endif
  2111. min_temp_error((heater_ind_t)e);
  2112. }
  2113. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  2114. else
  2115. consecutive_low_temperature_error[e] = 0;
  2116. #endif
  2117. }
  2118. }
  2119. #endif // HOTENDS
  2120. #if HAS_HEATED_BED
  2121. #if TEMPDIR(BED) < 0
  2122. #define BEDCMP(A,B) ((A)<=(B))
  2123. #else
  2124. #define BEDCMP(A,B) ((A)>=(B))
  2125. #endif
  2126. const bool bed_on = (temp_bed.target > 0)
  2127. #if ENABLED(PIDTEMPBED)
  2128. || (temp_bed.soft_pwm_amount > 0)
  2129. #endif
  2130. ;
  2131. if (BEDCMP(temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
  2132. if (bed_on && BEDCMP(mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
  2133. #endif
  2134. #if HAS_HEATED_CHAMBER
  2135. #if TEMPDIR(CHAMBER) < 0
  2136. #define CHAMBERCMP(A,B) ((A)<=(B))
  2137. #else
  2138. #define CHAMBERCMP(A,B) ((A)>=(B))
  2139. #endif
  2140. const bool chamber_on = (temp_chamber.target > 0);
  2141. if (CHAMBERCMP(temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
  2142. if (chamber_on && CHAMBERCMP(mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
  2143. #endif
  2144. }
  2145. /**
  2146. * Timer 0 is shared with millies so don't change the prescaler.
  2147. *
  2148. * On AVR this ISR uses the compare method so it runs at the base
  2149. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  2150. * in OCR0B above (128 or halfway between OVFs).
  2151. *
  2152. * - Manage PWM to all the heaters and fan
  2153. * - Prepare or Measure one of the raw ADC sensor values
  2154. * - Check new temperature values for MIN/MAX errors (kill on error)
  2155. * - Step the babysteps value for each axis towards 0
  2156. * - For PINS_DEBUGGING, monitor and report endstop pins
  2157. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  2158. * - Call planner.tick to count down its "ignore" time
  2159. */
  2160. HAL_TEMP_TIMER_ISR() {
  2161. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  2162. Temperature::tick();
  2163. HAL_timer_isr_epilogue(TEMP_TIMER_NUM);
  2164. }
  2165. #if ENABLED(SLOW_PWM_HEATERS) && !defined(MIN_STATE_TIME)
  2166. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  2167. #endif
  2168. class SoftPWM {
  2169. public:
  2170. uint8_t count;
  2171. inline bool add(const uint8_t mask, const uint8_t amount) {
  2172. count = (count & mask) + amount; return (count > mask);
  2173. }
  2174. #if ENABLED(SLOW_PWM_HEATERS)
  2175. bool state_heater;
  2176. uint8_t state_timer_heater;
  2177. inline void dec() { if (state_timer_heater > 0) state_timer_heater--; }
  2178. inline bool ready(const bool v) {
  2179. const bool rdy = !state_timer_heater;
  2180. if (rdy && state_heater != v) {
  2181. state_heater = v;
  2182. state_timer_heater = MIN_STATE_TIME;
  2183. }
  2184. return rdy;
  2185. }
  2186. #endif
  2187. };
  2188. /**
  2189. * Handle various ~1KHz tasks associated with temperature
  2190. * - Heater PWM (~1KHz with scaler)
  2191. * - LCD Button polling (~500Hz)
  2192. * - Start / Read one ADC sensor
  2193. * - Advance Babysteps
  2194. * - Endstop polling
  2195. * - Planner clean buffer
  2196. */
  2197. void Temperature::tick() {
  2198. static int8_t temp_count = -1;
  2199. static ADCSensorState adc_sensor_state = StartupDelay;
  2200. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  2201. // avoid multiple loads of pwm_count
  2202. uint8_t pwm_count_tmp = pwm_count;
  2203. #if HAS_ADC_BUTTONS
  2204. static unsigned int raw_ADCKey_value = 0;
  2205. static bool ADCKey_pressed = false;
  2206. #endif
  2207. #if HOTENDS
  2208. static SoftPWM soft_pwm_hotend[HOTENDS];
  2209. #endif
  2210. #if HAS_HEATED_BED
  2211. static SoftPWM soft_pwm_bed;
  2212. #endif
  2213. #if HAS_HEATED_CHAMBER
  2214. static SoftPWM soft_pwm_chamber;
  2215. #endif
  2216. #if DISABLED(SLOW_PWM_HEATERS)
  2217. #if HOTENDS || HAS_HEATED_BED || HAS_HEATED_CHAMBER
  2218. constexpr uint8_t pwm_mask =
  2219. #if ENABLED(SOFT_PWM_DITHER)
  2220. _BV(SOFT_PWM_SCALE) - 1
  2221. #else
  2222. 0
  2223. #endif
  2224. ;
  2225. #define _PWM_MOD(N,S,T) do{ \
  2226. const bool on = S.add(pwm_mask, T.soft_pwm_amount); \
  2227. WRITE_HEATER_##N(on); \
  2228. }while(0)
  2229. #endif
  2230. /**
  2231. * Standard heater PWM modulation
  2232. */
  2233. if (pwm_count_tmp >= 127) {
  2234. pwm_count_tmp -= 127;
  2235. #if HOTENDS
  2236. #define _PWM_MOD_E(N) _PWM_MOD(N,soft_pwm_hotend[N],temp_hotend[N]);
  2237. REPEAT(HOTENDS, _PWM_MOD_E);
  2238. #endif
  2239. #if HAS_HEATED_BED
  2240. _PWM_MOD(BED,soft_pwm_bed,temp_bed);
  2241. #endif
  2242. #if HAS_HEATED_CHAMBER
  2243. _PWM_MOD(CHAMBER,soft_pwm_chamber,temp_chamber);
  2244. #endif
  2245. #if ENABLED(FAN_SOFT_PWM)
  2246. #define _FAN_PWM(N) do{ \
  2247. uint8_t &spcf = soft_pwm_count_fan[N]; \
  2248. spcf = (spcf & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \
  2249. WRITE_FAN(N, spcf > pwm_mask ? HIGH : LOW); \
  2250. }while(0)
  2251. #if HAS_FAN0
  2252. _FAN_PWM(0);
  2253. #endif
  2254. #if HAS_FAN1
  2255. _FAN_PWM(1);
  2256. #endif
  2257. #if HAS_FAN2
  2258. _FAN_PWM(2);
  2259. #endif
  2260. #if HAS_FAN3
  2261. _FAN_PWM(3);
  2262. #endif
  2263. #if HAS_FAN4
  2264. _FAN_PWM(4);
  2265. #endif
  2266. #if HAS_FAN5
  2267. _FAN_PWM(5);
  2268. #endif
  2269. #if HAS_FAN6
  2270. _FAN_PWM(6);
  2271. #endif
  2272. #if HAS_FAN7
  2273. _FAN_PWM(7);
  2274. #endif
  2275. #endif
  2276. }
  2277. else {
  2278. #define _PWM_LOW(N,S) do{ if (S.count <= pwm_count_tmp) WRITE_HEATER_##N(LOW); }while(0)
  2279. #if HOTENDS
  2280. #define _PWM_LOW_E(N) _PWM_LOW(N, soft_pwm_hotend[N]);
  2281. REPEAT(HOTENDS, _PWM_LOW_E);
  2282. #endif
  2283. #if HAS_HEATED_BED
  2284. _PWM_LOW(BED, soft_pwm_bed);
  2285. #endif
  2286. #if HAS_HEATED_CHAMBER
  2287. _PWM_LOW(CHAMBER, soft_pwm_chamber);
  2288. #endif
  2289. #if ENABLED(FAN_SOFT_PWM)
  2290. #if HAS_FAN0
  2291. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2292. #endif
  2293. #if HAS_FAN1
  2294. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2295. #endif
  2296. #if HAS_FAN2
  2297. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2298. #endif
  2299. #if HAS_FAN3
  2300. if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW);
  2301. #endif
  2302. #if HAS_FAN4
  2303. if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW);
  2304. #endif
  2305. #if HAS_FAN5
  2306. if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW);
  2307. #endif
  2308. #if HAS_FAN6
  2309. if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW);
  2310. #endif
  2311. #if HAS_FAN7
  2312. if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW);
  2313. #endif
  2314. #endif
  2315. }
  2316. // SOFT_PWM_SCALE to frequency:
  2317. //
  2318. // 0: 16000000/64/256/128 = 7.6294 Hz
  2319. // 1: / 64 = 15.2588 Hz
  2320. // 2: / 32 = 30.5176 Hz
  2321. // 3: / 16 = 61.0352 Hz
  2322. // 4: / 8 = 122.0703 Hz
  2323. // 5: / 4 = 244.1406 Hz
  2324. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2325. #else // SLOW_PWM_HEATERS
  2326. /**
  2327. * SLOW PWM HEATERS
  2328. *
  2329. * For relay-driven heaters
  2330. */
  2331. #define _SLOW_SET(NR,PWM,V) do{ if (PWM.ready(V)) WRITE_HEATER_##NR(V); }while(0)
  2332. #define _SLOW_PWM(NR,PWM,SRC) do{ PWM.count = SRC.soft_pwm_amount; _SLOW_SET(NR,PWM,(PWM.count > 0)); }while(0)
  2333. #define _PWM_OFF(NR,PWM) do{ if (PWM.count < slow_pwm_count) _SLOW_SET(NR,PWM,0); }while(0)
  2334. static uint8_t slow_pwm_count = 0;
  2335. if (slow_pwm_count == 0) {
  2336. #if HOTENDS
  2337. #define _SLOW_PWM_E(N) _SLOW_PWM(N, soft_pwm_hotend[N], temp_hotend[N]);
  2338. REPEAT(HOTENDS, _SLOW_PWM_E);
  2339. #endif
  2340. #if HAS_HEATED_BED
  2341. _SLOW_PWM(BED, soft_pwm_bed, temp_bed);
  2342. #endif
  2343. } // slow_pwm_count == 0
  2344. #if HOTENDS
  2345. #define _PWM_OFF_E(N) _PWM_OFF(N, soft_pwm_hotend[N]);
  2346. REPEAT(HOTENDS, _PWM_OFF_E);
  2347. #endif
  2348. #if HAS_HEATED_BED
  2349. _PWM_OFF(BED, soft_pwm_bed);
  2350. #endif
  2351. #if ENABLED(FAN_SOFT_PWM)
  2352. if (pwm_count_tmp >= 127) {
  2353. pwm_count_tmp = 0;
  2354. #define _PWM_FAN(N) do{ \
  2355. soft_pwm_count_fan[N] = soft_pwm_amount_fan[N] >> 1; \
  2356. WRITE_FAN(N, soft_pwm_count_fan[N] > 0 ? HIGH : LOW); \
  2357. }while(0)
  2358. #if HAS_FAN0
  2359. _PWM_FAN(0);
  2360. #endif
  2361. #if HAS_FAN1
  2362. _PWM_FAN(1);
  2363. #endif
  2364. #if HAS_FAN2
  2365. _PWM_FAN(2);
  2366. #endif
  2367. #if HAS_FAN3
  2368. _FAN_PWM(3);
  2369. #endif
  2370. #if HAS_FAN4
  2371. _FAN_PWM(4);
  2372. #endif
  2373. #if HAS_FAN5
  2374. _FAN_PWM(5);
  2375. #endif
  2376. #if HAS_FAN6
  2377. _FAN_PWM(6);
  2378. #endif
  2379. #if HAS_FAN7
  2380. _FAN_PWM(7);
  2381. #endif
  2382. }
  2383. #if HAS_FAN0
  2384. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2385. #endif
  2386. #if HAS_FAN1
  2387. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2388. #endif
  2389. #if HAS_FAN2
  2390. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2391. #endif
  2392. #if HAS_FAN3
  2393. if (soft_pwm_count_fan[3] <= pwm_count_tmp) WRITE_FAN(3, LOW);
  2394. #endif
  2395. #if HAS_FAN4
  2396. if (soft_pwm_count_fan[4] <= pwm_count_tmp) WRITE_FAN(4, LOW);
  2397. #endif
  2398. #if HAS_FAN5
  2399. if (soft_pwm_count_fan[5] <= pwm_count_tmp) WRITE_FAN(5, LOW);
  2400. #endif
  2401. #if HAS_FAN6
  2402. if (soft_pwm_count_fan[6] <= pwm_count_tmp) WRITE_FAN(6, LOW);
  2403. #endif
  2404. #if HAS_FAN7
  2405. if (soft_pwm_count_fan[7] <= pwm_count_tmp) WRITE_FAN(7, LOW);
  2406. #endif
  2407. #endif // FAN_SOFT_PWM
  2408. // SOFT_PWM_SCALE to frequency:
  2409. //
  2410. // 0: 16000000/64/256/128 = 7.6294 Hz
  2411. // 1: / 64 = 15.2588 Hz
  2412. // 2: / 32 = 30.5176 Hz
  2413. // 3: / 16 = 61.0352 Hz
  2414. // 4: / 8 = 122.0703 Hz
  2415. // 5: / 4 = 244.1406 Hz
  2416. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2417. // increment slow_pwm_count only every 64th pwm_count,
  2418. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  2419. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  2420. slow_pwm_count++;
  2421. slow_pwm_count &= 0x7F;
  2422. #if HOTENDS
  2423. HOTEND_LOOP() soft_pwm_hotend[e].dec();
  2424. #endif
  2425. #if HAS_HEATED_BED
  2426. soft_pwm_bed.dec();
  2427. #endif
  2428. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  2429. #endif // SLOW_PWM_HEATERS
  2430. //
  2431. // Update lcd buttons 488 times per second
  2432. //
  2433. static bool do_buttons;
  2434. if ((do_buttons ^= true)) ui.update_buttons();
  2435. /**
  2436. * One sensor is sampled on every other call of the ISR.
  2437. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  2438. *
  2439. * On each Prepare pass, ADC is started for a sensor pin.
  2440. * On the next pass, the ADC value is read and accumulated.
  2441. *
  2442. * This gives each ADC 0.9765ms to charge up.
  2443. */
  2444. #define ACCUMULATE_ADC(obj) do{ \
  2445. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; \
  2446. else obj.sample(HAL_READ_ADC()); \
  2447. }while(0)
  2448. ADCSensorState next_sensor_state = adc_sensor_state < SensorsReady ? (ADCSensorState)(int(adc_sensor_state) + 1) : StartSampling;
  2449. switch (adc_sensor_state) {
  2450. case SensorsReady: {
  2451. // All sensors have been read. Stay in this state for a few
  2452. // ISRs to save on calls to temp update/checking code below.
  2453. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  2454. static uint8_t delay_count = 0;
  2455. if (extra_loops > 0) {
  2456. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  2457. if (--delay_count) // While delaying...
  2458. next_sensor_state = SensorsReady; // retain this state (else, next state will be 0)
  2459. break;
  2460. }
  2461. else {
  2462. adc_sensor_state = StartSampling; // Fall-through to start sampling
  2463. next_sensor_state = (ADCSensorState)(int(StartSampling) + 1);
  2464. }
  2465. }
  2466. case StartSampling: // Start of sampling loops. Do updates/checks.
  2467. if (++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  2468. temp_count = 0;
  2469. readings_ready();
  2470. }
  2471. break;
  2472. #if HAS_TEMP_ADC_0
  2473. case PrepareTemp_0: HAL_START_ADC(TEMP_0_PIN); break;
  2474. case MeasureTemp_0: ACCUMULATE_ADC(temp_hotend[0]); break;
  2475. #endif
  2476. #if HAS_HEATED_BED
  2477. case PrepareTemp_BED: HAL_START_ADC(TEMP_BED_PIN); break;
  2478. case MeasureTemp_BED: ACCUMULATE_ADC(temp_bed); break;
  2479. #endif
  2480. #if HAS_TEMP_CHAMBER
  2481. case PrepareTemp_CHAMBER: HAL_START_ADC(TEMP_CHAMBER_PIN); break;
  2482. case MeasureTemp_CHAMBER: ACCUMULATE_ADC(temp_chamber); break;
  2483. #endif
  2484. #if HAS_TEMP_PROBE
  2485. case PrepareTemp_PROBE: HAL_START_ADC(TEMP_PROBE_PIN); break;
  2486. case MeasureTemp_PROBE: ACCUMULATE_ADC(temp_probe); break;
  2487. #endif
  2488. #if HAS_TEMP_ADC_1
  2489. case PrepareTemp_1: HAL_START_ADC(TEMP_1_PIN); break;
  2490. case MeasureTemp_1: ACCUMULATE_ADC(temp_hotend[1]); break;
  2491. #endif
  2492. #if HAS_TEMP_ADC_2
  2493. case PrepareTemp_2: HAL_START_ADC(TEMP_2_PIN); break;
  2494. case MeasureTemp_2: ACCUMULATE_ADC(temp_hotend[2]); break;
  2495. #endif
  2496. #if HAS_TEMP_ADC_3
  2497. case PrepareTemp_3: HAL_START_ADC(TEMP_3_PIN); break;
  2498. case MeasureTemp_3: ACCUMULATE_ADC(temp_hotend[3]); break;
  2499. #endif
  2500. #if HAS_TEMP_ADC_4
  2501. case PrepareTemp_4: HAL_START_ADC(TEMP_4_PIN); break;
  2502. case MeasureTemp_4: ACCUMULATE_ADC(temp_hotend[4]); break;
  2503. #endif
  2504. #if HAS_TEMP_ADC_5
  2505. case PrepareTemp_5: HAL_START_ADC(TEMP_5_PIN); break;
  2506. case MeasureTemp_5: ACCUMULATE_ADC(temp_hotend[5]); break;
  2507. #endif
  2508. #if HAS_TEMP_ADC_6
  2509. case PrepareTemp_6: HAL_START_ADC(TEMP_6_PIN); break;
  2510. case MeasureTemp_6: ACCUMULATE_ADC(temp_hotend[6]); break;
  2511. #endif
  2512. #if HAS_TEMP_ADC_7
  2513. case PrepareTemp_7: HAL_START_ADC(TEMP_7_PIN); break;
  2514. case MeasureTemp_7: ACCUMULATE_ADC(temp_hotend[7]); break;
  2515. #endif
  2516. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  2517. case Prepare_FILWIDTH: HAL_START_ADC(FILWIDTH_PIN); break;
  2518. case Measure_FILWIDTH:
  2519. if (!HAL_ADC_READY())
  2520. next_sensor_state = adc_sensor_state; // redo this state
  2521. else
  2522. filwidth.accumulate(HAL_READ_ADC());
  2523. break;
  2524. #endif
  2525. #if HAS_JOY_ADC_X
  2526. case PrepareJoy_X: HAL_START_ADC(JOY_X_PIN); break;
  2527. case MeasureJoy_X: ACCUMULATE_ADC(joystick.x); break;
  2528. #endif
  2529. #if HAS_JOY_ADC_Y
  2530. case PrepareJoy_Y: HAL_START_ADC(JOY_Y_PIN); break;
  2531. case MeasureJoy_Y: ACCUMULATE_ADC(joystick.y); break;
  2532. #endif
  2533. #if HAS_JOY_ADC_Z
  2534. case PrepareJoy_Z: HAL_START_ADC(JOY_Z_PIN); break;
  2535. case MeasureJoy_Z: ACCUMULATE_ADC(joystick.z); break;
  2536. #endif
  2537. #if HAS_ADC_BUTTONS
  2538. #ifndef ADC_BUTTON_DEBOUNCE_DELAY
  2539. #define ADC_BUTTON_DEBOUNCE_DELAY 16
  2540. #endif
  2541. case Prepare_ADC_KEY: HAL_START_ADC(ADC_KEYPAD_PIN); break;
  2542. case Measure_ADC_KEY:
  2543. if (!HAL_ADC_READY())
  2544. next_sensor_state = adc_sensor_state; // redo this state
  2545. else if (ADCKey_count < ADC_BUTTON_DEBOUNCE_DELAY) {
  2546. raw_ADCKey_value = HAL_READ_ADC();
  2547. if (raw_ADCKey_value <= 900UL * HAL_ADC_RANGE / 1024UL) {
  2548. NOMORE(current_ADCKey_raw, raw_ADCKey_value);
  2549. ADCKey_count++;
  2550. }
  2551. else { //ADC Key release
  2552. if (ADCKey_count > 0) ADCKey_count++; else ADCKey_pressed = false;
  2553. if (ADCKey_pressed) {
  2554. ADCKey_count = 0;
  2555. current_ADCKey_raw = HAL_ADC_RANGE;
  2556. }
  2557. }
  2558. }
  2559. if (ADCKey_count == ADC_BUTTON_DEBOUNCE_DELAY) ADCKey_pressed = true;
  2560. break;
  2561. #endif // HAS_ADC_BUTTONS
  2562. case StartupDelay: break;
  2563. } // switch(adc_sensor_state)
  2564. // Go to the next state
  2565. adc_sensor_state = next_sensor_state;
  2566. //
  2567. // Additional ~1KHz Tasks
  2568. //
  2569. #if ENABLED(BABYSTEPPING) && DISABLED(INTEGRATED_BABYSTEPPING)
  2570. babystep.task();
  2571. #endif
  2572. // Poll endstops state, if required
  2573. endstops.poll();
  2574. // Periodically call the planner timer
  2575. planner.tick();
  2576. }
  2577. #if HAS_TEMP_SENSOR
  2578. #include "../gcode/gcode.h"
  2579. static void print_heater_state(const float &c, const float &t
  2580. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2581. , const float r
  2582. #endif
  2583. , const heater_ind_t e=INDEX_NONE
  2584. ) {
  2585. char k;
  2586. switch (e) {
  2587. #if HAS_TEMP_CHAMBER
  2588. case H_CHAMBER: k = 'C'; break;
  2589. #endif
  2590. #if HAS_TEMP_PROBE
  2591. case H_PROBE: k = 'P'; break;
  2592. #endif
  2593. #if HAS_TEMP_HOTEND
  2594. default: k = 'T'; break;
  2595. #if HAS_HEATED_BED
  2596. case H_BED: k = 'B'; break;
  2597. #endif
  2598. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2599. case H_REDUNDANT: k = 'R'; break;
  2600. #endif
  2601. #elif HAS_HEATED_BED
  2602. default: k = 'B'; break;
  2603. #endif
  2604. }
  2605. SERIAL_CHAR(' ');
  2606. SERIAL_CHAR(k);
  2607. #if HOTENDS > 1
  2608. if (e >= 0) SERIAL_CHAR('0' + e);
  2609. #endif
  2610. SERIAL_CHAR(':');
  2611. SERIAL_ECHO(c);
  2612. SERIAL_ECHOPAIR(" /" , t);
  2613. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2614. SERIAL_ECHOPAIR(" (", r * RECIPROCAL(OVERSAMPLENR));
  2615. SERIAL_CHAR(')');
  2616. #endif
  2617. delay(2);
  2618. }
  2619. void Temperature::print_heater_states(const uint8_t target_extruder
  2620. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2621. , const bool include_r/*=false*/
  2622. #endif
  2623. ) {
  2624. #if HAS_TEMP_HOTEND
  2625. print_heater_state(degHotend(target_extruder), degTargetHotend(target_extruder)
  2626. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2627. , rawHotendTemp(target_extruder)
  2628. #endif
  2629. );
  2630. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2631. if (include_r) print_heater_state(redundant_temperature, degTargetHotend(target_extruder)
  2632. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2633. , redundant_temperature_raw
  2634. #endif
  2635. , H_REDUNDANT
  2636. );
  2637. #endif
  2638. #endif
  2639. #if HAS_HEATED_BED
  2640. print_heater_state(degBed(), degTargetBed()
  2641. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2642. , rawBedTemp()
  2643. #endif
  2644. , H_BED
  2645. );
  2646. #endif
  2647. #if HAS_TEMP_CHAMBER
  2648. print_heater_state(degChamber()
  2649. #if HAS_HEATED_CHAMBER
  2650. , degTargetChamber()
  2651. #else
  2652. , 0
  2653. #endif
  2654. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2655. , rawChamberTemp()
  2656. #endif
  2657. , H_CHAMBER
  2658. );
  2659. #endif // HAS_TEMP_CHAMBER
  2660. #if HAS_TEMP_PROBE
  2661. print_heater_state(degProbe(), 0
  2662. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2663. , rawProbeTemp()
  2664. #endif
  2665. , H_PROBE
  2666. );
  2667. #endif // HAS_TEMP_PROBE
  2668. #if HOTENDS > 1
  2669. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e)
  2670. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2671. , rawHotendTemp(e)
  2672. #endif
  2673. , (heater_ind_t)e
  2674. );
  2675. #endif
  2676. SERIAL_ECHOPAIR(" @:", getHeaterPower((heater_ind_t)target_extruder));
  2677. #if HAS_HEATED_BED
  2678. SERIAL_ECHOPAIR(" B@:", getHeaterPower(H_BED));
  2679. #endif
  2680. #if HAS_HEATED_CHAMBER
  2681. SERIAL_ECHOPAIR(" C@:", getHeaterPower(H_CHAMBER));
  2682. #endif
  2683. #if HOTENDS > 1
  2684. HOTEND_LOOP() {
  2685. SERIAL_ECHOPAIR(" @", e);
  2686. SERIAL_CHAR(':');
  2687. SERIAL_ECHO(getHeaterPower((heater_ind_t)e));
  2688. }
  2689. #endif
  2690. }
  2691. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  2692. uint8_t Temperature::auto_report_temp_interval;
  2693. millis_t Temperature::next_temp_report_ms;
  2694. void Temperature::auto_report_temperatures() {
  2695. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  2696. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  2697. PORT_REDIRECT(SERIAL_BOTH);
  2698. print_heater_states(active_extruder);
  2699. SERIAL_EOL();
  2700. }
  2701. }
  2702. #endif // AUTO_REPORT_TEMPERATURES
  2703. #if HOTENDS && HAS_DISPLAY
  2704. void Temperature::set_heating_message(const uint8_t e) {
  2705. const bool heating = isHeatingHotend(e);
  2706. ui.status_printf_P(0,
  2707. #if HOTENDS > 1
  2708. PSTR("E%c " S_FMT), '1' + e
  2709. #else
  2710. PSTR("E " S_FMT)
  2711. #endif
  2712. , heating ? GET_TEXT(MSG_HEATING) : GET_TEXT(MSG_COOLING)
  2713. );
  2714. }
  2715. #endif
  2716. #if HAS_TEMP_HOTEND
  2717. #ifndef MIN_COOLING_SLOPE_DEG
  2718. #define MIN_COOLING_SLOPE_DEG 1.50
  2719. #endif
  2720. #ifndef MIN_COOLING_SLOPE_TIME
  2721. #define MIN_COOLING_SLOPE_TIME 60
  2722. #endif
  2723. bool Temperature::wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling/*=true*/
  2724. #if G26_CLICK_CAN_CANCEL
  2725. , const bool click_to_cancel/*=false*/
  2726. #endif
  2727. ) {
  2728. #if TEMP_RESIDENCY_TIME > 0
  2729. millis_t residency_start_ms = 0;
  2730. bool first_loop = true;
  2731. // Loop until the temperature has stabilized
  2732. #define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_RESIDENCY_TIME) * 1000UL))
  2733. #else
  2734. // Loop until the temperature is very close target
  2735. #define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
  2736. #endif
  2737. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2738. KEEPALIVE_STATE(NOT_BUSY);
  2739. #endif
  2740. #if ENABLED(PRINTER_EVENT_LEDS)
  2741. const float start_temp = degHotend(target_extruder);
  2742. printerEventLEDs.onHotendHeatingStart();
  2743. #endif
  2744. float target_temp = -1.0, old_temp = 9999.0;
  2745. bool wants_to_cool = false;
  2746. wait_for_heatup = true;
  2747. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2748. do {
  2749. // Target temperature might be changed during the loop
  2750. if (target_temp != degTargetHotend(target_extruder)) {
  2751. wants_to_cool = isCoolingHotend(target_extruder);
  2752. target_temp = degTargetHotend(target_extruder);
  2753. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2754. if (no_wait_for_cooling && wants_to_cool) break;
  2755. }
  2756. now = millis();
  2757. if (ELAPSED(now, next_temp_ms)) { // Print temp & remaining time every 1s while waiting
  2758. next_temp_ms = now + 1000UL;
  2759. print_heater_states(target_extruder);
  2760. #if TEMP_RESIDENCY_TIME > 0
  2761. SERIAL_ECHOPGM(" W:");
  2762. if (residency_start_ms)
  2763. SERIAL_ECHO(long((((TEMP_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2764. else
  2765. SERIAL_CHAR('?');
  2766. #endif
  2767. SERIAL_EOL();
  2768. }
  2769. idle();
  2770. gcode.reset_stepper_timeout(); // Keep steppers powered
  2771. const float temp = degHotend(target_extruder);
  2772. #if ENABLED(PRINTER_EVENT_LEDS)
  2773. // Gradually change LED strip from violet to red as nozzle heats up
  2774. if (!wants_to_cool) printerEventLEDs.onHotendHeating(start_temp, temp, target_temp);
  2775. #endif
  2776. #if TEMP_RESIDENCY_TIME > 0
  2777. const float temp_diff = ABS(target_temp - temp);
  2778. if (!residency_start_ms) {
  2779. // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
  2780. if (temp_diff < TEMP_WINDOW) {
  2781. residency_start_ms = now;
  2782. if (first_loop) residency_start_ms += (TEMP_RESIDENCY_TIME) * 1000UL;
  2783. }
  2784. }
  2785. else if (temp_diff > TEMP_HYSTERESIS) {
  2786. // Restart the timer whenever the temperature falls outside the hysteresis.
  2787. residency_start_ms = now;
  2788. }
  2789. first_loop = false;
  2790. #endif
  2791. // Prevent a wait-forever situation if R is misused i.e. M109 R0
  2792. if (wants_to_cool) {
  2793. // break after MIN_COOLING_SLOPE_TIME seconds
  2794. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG
  2795. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2796. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG)) break;
  2797. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME;
  2798. old_temp = temp;
  2799. }
  2800. }
  2801. #if G26_CLICK_CAN_CANCEL
  2802. if (click_to_cancel && ui.use_click()) {
  2803. wait_for_heatup = false;
  2804. ui.quick_feedback();
  2805. }
  2806. #endif
  2807. } while (wait_for_heatup && TEMP_CONDITIONS);
  2808. if (wait_for_heatup) {
  2809. ui.reset_status();
  2810. #if ENABLED(PRINTER_EVENT_LEDS)
  2811. printerEventLEDs.onHeatingDone();
  2812. #endif
  2813. }
  2814. return wait_for_heatup;
  2815. }
  2816. #endif // HAS_TEMP_HOTEND
  2817. #if HAS_HEATED_BED
  2818. #ifndef MIN_COOLING_SLOPE_DEG_BED
  2819. #define MIN_COOLING_SLOPE_DEG_BED 1.50
  2820. #endif
  2821. #ifndef MIN_COOLING_SLOPE_TIME_BED
  2822. #define MIN_COOLING_SLOPE_TIME_BED 60
  2823. #endif
  2824. bool Temperature::wait_for_bed(const bool no_wait_for_cooling/*=true*/
  2825. #if G26_CLICK_CAN_CANCEL
  2826. , const bool click_to_cancel/*=false*/
  2827. #endif
  2828. ) {
  2829. #if TEMP_BED_RESIDENCY_TIME > 0
  2830. millis_t residency_start_ms = 0;
  2831. bool first_loop = true;
  2832. // Loop until the temperature has stabilized
  2833. #define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_BED_RESIDENCY_TIME) * 1000UL))
  2834. #else
  2835. // Loop until the temperature is very close target
  2836. #define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
  2837. #endif
  2838. float target_temp = -1, old_temp = 9999;
  2839. bool wants_to_cool = false;
  2840. wait_for_heatup = true;
  2841. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2842. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2843. KEEPALIVE_STATE(NOT_BUSY);
  2844. #endif
  2845. #if ENABLED(PRINTER_EVENT_LEDS)
  2846. const float start_temp = degBed();
  2847. printerEventLEDs.onBedHeatingStart();
  2848. #endif
  2849. do {
  2850. // Target temperature might be changed during the loop
  2851. if (target_temp != degTargetBed()) {
  2852. wants_to_cool = isCoolingBed();
  2853. target_temp = degTargetBed();
  2854. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2855. if (no_wait_for_cooling && wants_to_cool) break;
  2856. }
  2857. now = millis();
  2858. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2859. next_temp_ms = now + 1000UL;
  2860. print_heater_states(active_extruder);
  2861. #if TEMP_BED_RESIDENCY_TIME > 0
  2862. SERIAL_ECHOPGM(" W:");
  2863. if (residency_start_ms)
  2864. SERIAL_ECHO(long((((TEMP_BED_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2865. else
  2866. SERIAL_CHAR('?');
  2867. #endif
  2868. SERIAL_EOL();
  2869. }
  2870. idle();
  2871. gcode.reset_stepper_timeout(); // Keep steppers powered
  2872. const float temp = degBed();
  2873. #if ENABLED(PRINTER_EVENT_LEDS)
  2874. // Gradually change LED strip from blue to violet as bed heats up
  2875. if (!wants_to_cool) printerEventLEDs.onBedHeating(start_temp, temp, target_temp);
  2876. #endif
  2877. #if TEMP_BED_RESIDENCY_TIME > 0
  2878. const float temp_diff = ABS(target_temp - temp);
  2879. if (!residency_start_ms) {
  2880. // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time.
  2881. if (temp_diff < TEMP_BED_WINDOW) {
  2882. residency_start_ms = now;
  2883. if (first_loop) residency_start_ms += (TEMP_BED_RESIDENCY_TIME) * 1000UL;
  2884. }
  2885. }
  2886. else if (temp_diff > TEMP_BED_HYSTERESIS) {
  2887. // Restart the timer whenever the temperature falls outside the hysteresis.
  2888. residency_start_ms = now;
  2889. }
  2890. #endif // TEMP_BED_RESIDENCY_TIME > 0
  2891. // Prevent a wait-forever situation if R is misused i.e. M190 R0
  2892. if (wants_to_cool) {
  2893. // Break after MIN_COOLING_SLOPE_TIME_BED seconds
  2894. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_BED
  2895. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2896. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_BED)) break;
  2897. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_BED;
  2898. old_temp = temp;
  2899. }
  2900. }
  2901. #if G26_CLICK_CAN_CANCEL
  2902. if (click_to_cancel && ui.use_click()) {
  2903. wait_for_heatup = false;
  2904. ui.quick_feedback();
  2905. }
  2906. #endif
  2907. #if TEMP_BED_RESIDENCY_TIME > 0
  2908. first_loop = false;
  2909. #endif
  2910. } while (wait_for_heatup && TEMP_BED_CONDITIONS);
  2911. if (wait_for_heatup) ui.reset_status();
  2912. return wait_for_heatup;
  2913. }
  2914. void Temperature::wait_for_bed_heating() {
  2915. if (isHeatingBed()) {
  2916. SERIAL_ECHOLNPGM("Wait for bed heating...");
  2917. LCD_MESSAGEPGM(MSG_BED_HEATING);
  2918. wait_for_bed();
  2919. ui.reset_status();
  2920. }
  2921. }
  2922. #endif // HAS_HEATED_BED
  2923. #if HAS_HEATED_CHAMBER
  2924. #ifndef MIN_COOLING_SLOPE_DEG_CHAMBER
  2925. #define MIN_COOLING_SLOPE_DEG_CHAMBER 1.50
  2926. #endif
  2927. #ifndef MIN_COOLING_SLOPE_TIME_CHAMBER
  2928. #define MIN_COOLING_SLOPE_TIME_CHAMBER 60
  2929. #endif
  2930. bool Temperature::wait_for_chamber(const bool no_wait_for_cooling/*=true*/) {
  2931. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2932. millis_t residency_start_ms = 0;
  2933. bool first_loop = true;
  2934. // Loop until the temperature has stabilized
  2935. #define TEMP_CHAMBER_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL))
  2936. #else
  2937. // Loop until the temperature is very close target
  2938. #define TEMP_CHAMBER_CONDITIONS (wants_to_cool ? isCoolingChamber() : isHeatingChamber())
  2939. #endif
  2940. float target_temp = -1, old_temp = 9999;
  2941. bool wants_to_cool = false;
  2942. wait_for_heatup = true;
  2943. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2944. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2945. KEEPALIVE_STATE(NOT_BUSY);
  2946. #endif
  2947. do {
  2948. // Target temperature might be changed during the loop
  2949. if (target_temp != degTargetChamber()) {
  2950. wants_to_cool = isCoolingChamber();
  2951. target_temp = degTargetChamber();
  2952. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2953. if (no_wait_for_cooling && wants_to_cool) break;
  2954. }
  2955. now = millis();
  2956. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2957. next_temp_ms = now + 1000UL;
  2958. print_heater_states(active_extruder);
  2959. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2960. SERIAL_ECHOPGM(" W:");
  2961. if (residency_start_ms)
  2962. SERIAL_ECHO(long((((TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2963. else
  2964. SERIAL_CHAR('?');
  2965. #endif
  2966. SERIAL_EOL();
  2967. }
  2968. idle();
  2969. gcode.reset_stepper_timeout(); // Keep steppers powered
  2970. const float temp = degChamber();
  2971. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2972. const float temp_diff = ABS(target_temp - temp);
  2973. if (!residency_start_ms) {
  2974. // Start the TEMP_CHAMBER_RESIDENCY_TIME timer when we reach target temp for the first time.
  2975. if (temp_diff < TEMP_CHAMBER_WINDOW) {
  2976. residency_start_ms = now;
  2977. if (first_loop) residency_start_ms += (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL;
  2978. }
  2979. }
  2980. else if (temp_diff > TEMP_CHAMBER_HYSTERESIS) {
  2981. // Restart the timer whenever the temperature falls outside the hysteresis.
  2982. residency_start_ms = now;
  2983. }
  2984. first_loop = false;
  2985. #endif // TEMP_CHAMBER_RESIDENCY_TIME > 0
  2986. // Prevent a wait-forever situation if R is misused i.e. M191 R0
  2987. if (wants_to_cool) {
  2988. // Break after MIN_COOLING_SLOPE_TIME_CHAMBER seconds
  2989. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_CHAMBER
  2990. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2991. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_CHAMBER)) break;
  2992. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_CHAMBER;
  2993. old_temp = temp;
  2994. }
  2995. }
  2996. } while (wait_for_heatup && TEMP_CHAMBER_CONDITIONS);
  2997. if (wait_for_heatup) ui.reset_status();
  2998. return wait_for_heatup;
  2999. }
  3000. #endif // HAS_HEATED_CHAMBER
  3001. #endif // HAS_TEMP_SENSOR