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

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