My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 87KB

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