My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 106KB

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