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

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