My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 100KB

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