My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

temperature.cpp 108KB

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