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 103KB

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