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

temperature.cpp 108KB

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