My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 85KB

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