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

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