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

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