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

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