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

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