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

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