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

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