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

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