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

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