My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

temperature.cpp 101KB

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