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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * temperature.cpp - temperature control
  24. */
  25. #include "temperature.h"
  26. #include "endstops.h"
  27. #include "../MarlinCore.h"
  28. #include "../lcd/ultralcd.h"
  29. #include "planner.h"
  30. #include "../core/language.h"
  31. #include "../HAL/shared/Delay.h"
  32. #if ENABLED(MAX6675_IS_MAX31865)
  33. #include "Adafruit_MAX31865.h"
  34. #ifndef MAX31865_CS_PIN
  35. #define MAX31865_CS_PIN MAX6675_SS_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 MAX6675_DO_PIN // 42
  42. #endif
  43. #ifndef MAX31865_SCK_PIN
  44. #define MAX31865_SCK_PIN MAX6675_SCK_PIN // 40
  45. #endif
  46. Adafruit_MAX31865 max31865 = Adafruit_MAX31865(MAX31865_CS_PIN
  47. #if MAX31865_CS_PIN != MAX6675_SS_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. #if ENABLED(PID_FAN_SCALING)
  744. if (thermalManager.fan_speed[active_extruder] > PID_FAN_SCALING_MIN_SPEED) {
  745. work_pid[ee].Kf = PID_PARAM(Kf, ee) + (PID_FAN_SCALING_LIN_FACTOR) * thermalManager.fan_speed[active_extruder];
  746. pid_output += work_pid[ee].Kf;
  747. }
  748. //pid_output -= work_pid[ee].Ki;
  749. //pid_output += work_pid[ee].Ki * work_pid[ee].Kf
  750. #endif // PID_FAN_SCALING
  751. LIMIT(pid_output, 0, PID_MAX);
  752. }
  753. temp_dState[ee] = temp_hotend[ee].celsius;
  754. #else // PID_OPENLOOP
  755. const float pid_output = constrain(temp_hotend[ee].target, 0, PID_MAX);
  756. #endif // PID_OPENLOOP
  757. #if ENABLED(PID_DEBUG)
  758. if (ee == active_extruder) {
  759. SERIAL_ECHO_START();
  760. SERIAL_ECHOPAIR(
  761. MSG_PID_DEBUG, ee,
  762. MSG_PID_DEBUG_INPUT, temp_hotend[ee].celsius,
  763. MSG_PID_DEBUG_OUTPUT, pid_output
  764. );
  765. #if DISABLED(PID_OPENLOOP)
  766. {
  767. SERIAL_ECHOPAIR(
  768. MSG_PID_DEBUG_PTERM, work_pid[ee].Kp,
  769. MSG_PID_DEBUG_ITERM, work_pid[ee].Ki,
  770. MSG_PID_DEBUG_DTERM, work_pid[ee].Kd
  771. #if ENABLED(PID_EXTRUSION_SCALING)
  772. , MSG_PID_DEBUG_CTERM, work_pid[ee].Kc
  773. #endif
  774. );
  775. }
  776. #endif
  777. SERIAL_EOL();
  778. }
  779. #endif // PID_DEBUG
  780. #else // No PID enabled
  781. #if HEATER_IDLE_HANDLER
  782. const bool is_idling = hotend_idle[ee].timed_out;
  783. #else
  784. constexpr bool is_idling = false;
  785. #endif
  786. const float pid_output = (!is_idling && temp_hotend[ee].celsius < temp_hotend[ee].target) ? BANG_MAX : 0;
  787. #endif
  788. return pid_output;
  789. }
  790. #endif // HOTENDS
  791. #if ENABLED(PIDTEMPBED)
  792. float Temperature::get_pid_output_bed() {
  793. #if DISABLED(PID_OPENLOOP)
  794. static PID_t work_pid{0};
  795. static float temp_iState = 0, temp_dState = 0;
  796. static bool pid_reset = true;
  797. float pid_output = 0;
  798. const float max_power_over_i_gain = float(MAX_BED_POWER) / temp_bed.pid.Ki - float(MIN_BED_POWER),
  799. pid_error = temp_bed.target - temp_bed.celsius;
  800. if (!temp_bed.target || pid_error < -(PID_FUNCTIONAL_RANGE)) {
  801. pid_output = 0;
  802. pid_reset = true;
  803. }
  804. else if (pid_error > PID_FUNCTIONAL_RANGE) {
  805. pid_output = MAX_BED_POWER;
  806. pid_reset = true;
  807. }
  808. else {
  809. if (pid_reset) {
  810. temp_iState = 0.0;
  811. work_pid.Kd = 0.0;
  812. pid_reset = false;
  813. }
  814. temp_iState = constrain(temp_iState + pid_error, 0, max_power_over_i_gain);
  815. work_pid.Kp = temp_bed.pid.Kp * pid_error;
  816. work_pid.Ki = temp_bed.pid.Ki * temp_iState;
  817. work_pid.Kd = work_pid.Kd + PID_K2 * (temp_bed.pid.Kd * (temp_dState - temp_bed.celsius) - work_pid.Kd);
  818. temp_dState = temp_bed.celsius;
  819. pid_output = constrain(work_pid.Kp + work_pid.Ki + work_pid.Kd + float(MIN_BED_POWER), 0, MAX_BED_POWER);
  820. }
  821. #else // PID_OPENLOOP
  822. const float pid_output = constrain(temp_bed.target, 0, MAX_BED_POWER);
  823. #endif // PID_OPENLOOP
  824. #if ENABLED(PID_BED_DEBUG)
  825. {
  826. SERIAL_ECHO_START();
  827. SERIAL_ECHOLNPAIR(
  828. " PID_BED_DEBUG : Input ", temp_bed.celsius, " Output ", pid_output,
  829. #if DISABLED(PID_OPENLOOP)
  830. MSG_PID_DEBUG_PTERM, work_pid.Kp,
  831. MSG_PID_DEBUG_ITERM, work_pid.Ki,
  832. MSG_PID_DEBUG_DTERM, work_pid.Kd,
  833. #endif
  834. );
  835. }
  836. #endif
  837. return pid_output;
  838. }
  839. #endif // PIDTEMPBED
  840. /**
  841. * Manage heating activities for extruder hot-ends and a heated bed
  842. * - Acquire updated temperature readings
  843. * - Also resets the watchdog timer
  844. * - Invoke thermal runaway protection
  845. * - Manage extruder auto-fan
  846. * - Apply filament width to the extrusion rate (may move)
  847. * - Update the heated bed PID output value
  848. */
  849. void Temperature::manage_heater() {
  850. #if EARLY_WATCHDOG
  851. // If thermal manager is still not running, make sure to at least reset the watchdog!
  852. if (!inited) return watchdog_refresh();
  853. #endif
  854. #if BOTH(PROBING_HEATERS_OFF, BED_LIMIT_SWITCHING)
  855. static bool last_pause_state;
  856. #endif
  857. #if ENABLED(EMERGENCY_PARSER)
  858. if (emergency_parser.killed_by_M112) kill();
  859. #endif
  860. if (!temp_meas_ready) return;
  861. updateTemperaturesFromRawValues(); // also resets the watchdog
  862. #if ENABLED(HEATER_0_USES_MAX6675)
  863. if (temp_hotend[0].celsius > _MIN(HEATER_0_MAXTEMP, HEATER_0_MAX6675_TMAX - 1.0)) max_temp_error(H_E0);
  864. if (temp_hotend[0].celsius < _MAX(HEATER_0_MINTEMP, HEATER_0_MAX6675_TMIN + .01)) min_temp_error(H_E0);
  865. #endif
  866. #if ENABLED(HEATER_1_USES_MAX6675)
  867. if (temp_hotend[1].celsius > _MIN(HEATER_1_MAXTEMP, HEATER_1_MAX6675_TMAX - 1.0)) max_temp_error(H_E1);
  868. if (temp_hotend[1].celsius < _MAX(HEATER_1_MINTEMP, HEATER_1_MAX6675_TMIN + .01)) min_temp_error(H_E1);
  869. #endif
  870. millis_t ms = millis();
  871. #if HOTENDS
  872. HOTEND_LOOP() {
  873. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  874. if (degHotend(e) > temp_range[e].maxtemp)
  875. _temp_error((heater_ind_t)e, PSTR(MSG_T_THERMAL_RUNAWAY), GET_TEXT(MSG_THERMAL_RUNAWAY));
  876. #endif
  877. #if HEATER_IDLE_HANDLER
  878. hotend_idle[e].update(ms);
  879. #endif
  880. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  881. // Check for thermal runaway
  882. 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);
  883. #endif
  884. 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;
  885. #if WATCH_HOTENDS
  886. // Make sure temperature is increasing
  887. if (watch_hotend[e].next_ms && ELAPSED(ms, watch_hotend[e].next_ms)) { // Time to check this extruder?
  888. if (degHotend(e) < watch_hotend[e].target) // Failed to increase enough?
  889. _temp_error((heater_ind_t)e, PSTR(MSG_T_HEATING_FAILED), GET_TEXT(MSG_HEATING_FAILED_LCD));
  890. else // Start again if the target is still far off
  891. start_watching_hotend(e);
  892. }
  893. #endif
  894. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  895. // Make sure measured temperatures are close together
  896. if (ABS(temp_hotend[0].celsius - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  897. _temp_error(H_E0, PSTR(MSG_REDUNDANCY), GET_TEXT(MSG_ERR_REDUNDANT_TEMP));
  898. #endif
  899. } // HOTEND_LOOP
  900. #endif // HOTENDS
  901. #if HAS_AUTO_FAN
  902. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  903. checkExtruderAutoFans();
  904. next_auto_fan_check_ms = ms + 2500UL;
  905. }
  906. #endif
  907. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  908. /**
  909. * Dynamically set the volumetric multiplier based
  910. * on the delayed Filament Width measurement.
  911. */
  912. filwidth.update_volumetric();
  913. #endif
  914. #if HAS_HEATED_BED
  915. #if ENABLED(THERMAL_PROTECTION_BED)
  916. if (degBed() > BED_MAXTEMP)
  917. _temp_error(H_BED, PSTR(MSG_T_THERMAL_RUNAWAY), GET_TEXT(MSG_THERMAL_RUNAWAY));
  918. #endif
  919. #if WATCH_BED
  920. // Make sure temperature is increasing
  921. if (watch_bed.elapsed(ms)) { // Time to check the bed?
  922. if (degBed() < watch_bed.target) // Failed to increase enough?
  923. _temp_error(H_BED, PSTR(MSG_T_HEATING_FAILED), GET_TEXT(MSG_HEATING_FAILED_LCD));
  924. else // Start again if the target is still far off
  925. start_watching_bed();
  926. }
  927. #endif // WATCH_BED
  928. do {
  929. #if DISABLED(PIDTEMPBED)
  930. if (PENDING(ms, next_bed_check_ms)
  931. #if BOTH(PROBING_HEATERS_OFF, BED_LIMIT_SWITCHING)
  932. && paused == last_pause_state
  933. #endif
  934. ) break;
  935. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  936. #if BOTH(PROBING_HEATERS_OFF, BED_LIMIT_SWITCHING)
  937. last_pause_state = paused;
  938. #endif
  939. #endif
  940. #if HEATER_IDLE_HANDLER
  941. bed_idle.update(ms);
  942. #endif
  943. #if HAS_THERMALLY_PROTECTED_BED
  944. thermal_runaway_protection(tr_state_machine_bed, temp_bed.celsius, temp_bed.target, H_BED, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS);
  945. #endif
  946. #if HEATER_IDLE_HANDLER
  947. if (bed_idle.timed_out) {
  948. temp_bed.soft_pwm_amount = 0;
  949. #if DISABLED(PIDTEMPBED)
  950. WRITE_HEATER_BED(LOW);
  951. #endif
  952. }
  953. else
  954. #endif
  955. {
  956. #if ENABLED(PIDTEMPBED)
  957. temp_bed.soft_pwm_amount = WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  958. #else
  959. // Check if temperature is within the correct band
  960. if (WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP)) {
  961. #if ENABLED(BED_LIMIT_SWITCHING)
  962. if (temp_bed.celsius >= temp_bed.target + BED_HYSTERESIS)
  963. temp_bed.soft_pwm_amount = 0;
  964. else if (temp_bed.celsius <= temp_bed.target - (BED_HYSTERESIS))
  965. temp_bed.soft_pwm_amount = MAX_BED_POWER >> 1;
  966. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  967. temp_bed.soft_pwm_amount = temp_bed.celsius < temp_bed.target ? MAX_BED_POWER >> 1 : 0;
  968. #endif
  969. }
  970. else {
  971. temp_bed.soft_pwm_amount = 0;
  972. WRITE_HEATER_BED(LOW);
  973. }
  974. #endif
  975. }
  976. } while (false);
  977. #endif // HAS_HEATED_BED
  978. #if HAS_HEATED_CHAMBER
  979. #ifndef CHAMBER_CHECK_INTERVAL
  980. #define CHAMBER_CHECK_INTERVAL 1000UL
  981. #endif
  982. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  983. if (degChamber() > CHAMBER_MAXTEMP)
  984. _temp_error(H_CHAMBER, PSTR(MSG_T_THERMAL_RUNAWAY), GET_TEXT(MSG_THERMAL_RUNAWAY));
  985. #endif
  986. #if WATCH_CHAMBER
  987. // Make sure temperature is increasing
  988. if (watch_chamber.elapsed(ms)) { // Time to check the chamber?
  989. if (degChamber() < watch_chamber.target) // Failed to increase enough?
  990. _temp_error(H_CHAMBER, PSTR(MSG_T_HEATING_FAILED), GET_TEXT(MSG_HEATING_FAILED_LCD));
  991. else
  992. start_watching_chamber(); // Start again if the target is still far off
  993. }
  994. #endif
  995. if (ELAPSED(ms, next_chamber_check_ms)) {
  996. next_chamber_check_ms = ms + CHAMBER_CHECK_INTERVAL;
  997. if (WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP)) {
  998. #if ENABLED(CHAMBER_LIMIT_SWITCHING)
  999. if (temp_chamber.celsius >= temp_chamber.target + TEMP_CHAMBER_HYSTERESIS)
  1000. temp_chamber.soft_pwm_amount = 0;
  1001. else if (temp_chamber.celsius <= temp_chamber.target - (TEMP_CHAMBER_HYSTERESIS))
  1002. temp_chamber.soft_pwm_amount = MAX_CHAMBER_POWER >> 1;
  1003. #else
  1004. temp_chamber.soft_pwm_amount = temp_chamber.celsius < temp_chamber.target ? MAX_CHAMBER_POWER >> 1 : 0;
  1005. #endif
  1006. }
  1007. else {
  1008. temp_chamber.soft_pwm_amount = 0;
  1009. WRITE_HEATER_CHAMBER(LOW);
  1010. }
  1011. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  1012. thermal_runaway_protection(tr_state_machine_chamber, temp_chamber.celsius, temp_chamber.target, H_CHAMBER, THERMAL_PROTECTION_CHAMBER_PERIOD, THERMAL_PROTECTION_CHAMBER_HYSTERESIS);
  1013. #endif
  1014. }
  1015. // TODO: Implement true PID pwm
  1016. //temp_bed.soft_pwm_amount = WITHIN(temp_chamber.celsius, CHAMBER_MINTEMP, CHAMBER_MAXTEMP) ? (int)get_pid_output_chamber() >> 1 : 0;
  1017. #endif // HAS_HEATED_CHAMBER
  1018. UNUSED(ms);
  1019. }
  1020. #define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
  1021. #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
  1022. /**
  1023. * Bisect search for the range of the 'raw' value, then interpolate
  1024. * proportionally between the under and over values.
  1025. */
  1026. #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
  1027. uint8_t l = 0, r = LEN, m; \
  1028. for (;;) { \
  1029. m = (l + r) >> 1; \
  1030. if (!m) return short(pgm_read_word(&TBL[0][1])); \
  1031. if (m == l || m == r) return short(pgm_read_word(&TBL[LEN-1][1])); \
  1032. short v00 = pgm_read_word(&TBL[m-1][0]), \
  1033. v10 = pgm_read_word(&TBL[m-0][0]); \
  1034. if (raw < v00) r = m; \
  1035. else if (raw > v10) l = m; \
  1036. else { \
  1037. const short v01 = short(pgm_read_word(&TBL[m-1][1])), \
  1038. v11 = short(pgm_read_word(&TBL[m-0][1])); \
  1039. return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00); \
  1040. } \
  1041. } \
  1042. }while(0)
  1043. #if HAS_USER_THERMISTORS
  1044. user_thermistor_t Temperature::user_thermistor[USER_THERMISTORS]; // Initialized by settings.load()
  1045. void Temperature::reset_user_thermistors() {
  1046. user_thermistor_t user_thermistor[USER_THERMISTORS] = {
  1047. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1048. { true, 0, 0, HOTEND0_PULLUP_RESISTOR_OHMS, HOTEND0_RESISTANCE_25C_OHMS, 0, 0, HOTEND0_BETA, 0 },
  1049. #endif
  1050. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1051. { true, 0, 0, HOTEND1_PULLUP_RESISTOR_OHMS, HOTEND1_RESISTANCE_25C_OHMS, 0, 0, HOTEND1_BETA, 0 },
  1052. #endif
  1053. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1054. { true, 0, 0, HOTEND2_PULLUP_RESISTOR_OHMS, HOTEND2_RESISTANCE_25C_OHMS, 0, 0, HOTEND2_BETA, 0 },
  1055. #endif
  1056. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1057. { true, 0, 0, HOTEND3_PULLUP_RESISTOR_OHMS, HOTEND3_RESISTANCE_25C_OHMS, 0, 0, HOTEND3_BETA, 0 },
  1058. #endif
  1059. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1060. { true, 0, 0, HOTEND4_PULLUP_RESISTOR_OHMS, HOTEND4_RESISTANCE_25C_OHMS, 0, 0, HOTEND4_BETA, 0 },
  1061. #endif
  1062. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1063. { true, 0, 0, HOTEND5_PULLUP_RESISTOR_OHMS, HOTEND5_RESISTANCE_25C_OHMS, 0, 0, HOTEND5_BETA, 0 },
  1064. #endif
  1065. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1066. { true, 0, 0, BED_PULLUP_RESISTOR_OHMS, BED_RESISTANCE_25C_OHMS, 0, 0, BED_BETA, 0 },
  1067. #endif
  1068. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1069. { true, 0, 0, CHAMBER_PULLUP_RESISTOR_OHMS, CHAMBER_RESISTANCE_25C_OHMS, 0, 0, CHAMBER_BETA, 0 }
  1070. #endif
  1071. };
  1072. COPY(thermalManager.user_thermistor, user_thermistor);
  1073. }
  1074. void Temperature::log_user_thermistor(const uint8_t t_index, const bool eprom/*=false*/) {
  1075. if (eprom)
  1076. SERIAL_ECHOPGM(" M305 ");
  1077. else
  1078. SERIAL_ECHO_START();
  1079. SERIAL_CHAR('P');
  1080. SERIAL_CHAR('0' + t_index);
  1081. const user_thermistor_t &t = user_thermistor[t_index];
  1082. SERIAL_ECHOPAIR_F(" R", t.series_res, 1);
  1083. SERIAL_ECHOPAIR_F(" T", t.res_25, 1);
  1084. SERIAL_ECHOPAIR_F(" B", t.beta, 1);
  1085. SERIAL_ECHOPAIR_F(" C", t.sh_c_coeff, 9);
  1086. SERIAL_ECHOPGM(" ; ");
  1087. serialprintPGM(
  1088. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1089. t_index == CTI_HOTEND_0 ? PSTR("HOTEND 0") :
  1090. #endif
  1091. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1092. t_index == CTI_HOTEND_1 ? PSTR("HOTEND 1") :
  1093. #endif
  1094. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1095. t_index == CTI_HOTEND_2 ? PSTR("HOTEND 2") :
  1096. #endif
  1097. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1098. t_index == CTI_HOTEND_3 ? PSTR("HOTEND 3") :
  1099. #endif
  1100. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1101. t_index == CTI_HOTEND_4 ? PSTR("HOTEND 4") :
  1102. #endif
  1103. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1104. t_index == CTI_HOTEND_5 ? PSTR("HOTEND 5") :
  1105. #endif
  1106. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1107. t_index == CTI_BED ? PSTR("BED") :
  1108. #endif
  1109. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1110. t_index == CTI_CHAMBER ? PSTR("CHAMBER") :
  1111. #endif
  1112. nullptr
  1113. );
  1114. SERIAL_EOL();
  1115. }
  1116. float Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int raw) {
  1117. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1118. // static uint32_t clocks_total = 0;
  1119. // static uint32_t calls = 0;
  1120. // uint32_t tcnt5 = TCNT5;
  1121. //#endif
  1122. if (!WITHIN(t_index, 0, COUNT(user_thermistor) - 1)) return 25;
  1123. user_thermistor_t &t = user_thermistor[t_index];
  1124. if (t.pre_calc) { // pre-calculate some variables
  1125. t.pre_calc = false;
  1126. t.res_25_recip = 1.0f / t.res_25;
  1127. t.res_25_log = logf(t.res_25);
  1128. t.beta_recip = 1.0f / t.beta;
  1129. t.sh_alpha = RECIPROCAL(THERMISTOR_RESISTANCE_NOMINAL_C - (THERMISTOR_ABS_ZERO_C))
  1130. - (t.beta_recip * t.res_25_log) - (t.sh_c_coeff * cu(t.res_25_log));
  1131. }
  1132. // maximum adc value .. take into account the over sampling
  1133. const int adc_max = MAX_RAW_THERMISTOR_VALUE,
  1134. adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
  1135. const float adc_inverse = (adc_max - adc_raw) - 0.5f,
  1136. resistance = t.series_res * (adc_raw + 0.5f) / adc_inverse,
  1137. log_resistance = logf(resistance);
  1138. float value = t.sh_alpha;
  1139. value += log_resistance * t.beta_recip;
  1140. if (t.sh_c_coeff != 0)
  1141. value += t.sh_c_coeff * cu(log_resistance);
  1142. value = 1.0f / value;
  1143. //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB)
  1144. // int32_t clocks = TCNT5 - tcnt5;
  1145. // if (clocks >= 0) {
  1146. // clocks_total += clocks;
  1147. // calls++;
  1148. // }
  1149. //#endif
  1150. // Return degrees C (up to 999, as the LCD only displays 3 digits)
  1151. return _MIN(value + THERMISTOR_ABS_ZERO_C, 999);
  1152. }
  1153. #endif
  1154. #if HOTENDS
  1155. // Derived from RepRap FiveD extruder::getTemperature()
  1156. // For hot end temperature measurement.
  1157. float Temperature::analog_to_celsius_hotend(const int raw, const uint8_t e) {
  1158. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1159. if (e > HOTENDS)
  1160. #else
  1161. if (e >= HOTENDS)
  1162. #endif
  1163. {
  1164. SERIAL_ERROR_START();
  1165. SERIAL_ECHO((int)e);
  1166. SERIAL_ECHOLNPGM(MSG_INVALID_EXTRUDER_NUM);
  1167. kill();
  1168. return 0.0;
  1169. }
  1170. switch (e) {
  1171. case 0:
  1172. #if ENABLED(HEATER_0_USER_THERMISTOR)
  1173. return user_thermistor_to_deg_c(CTI_HOTEND_0, raw);
  1174. #elif ENABLED(HEATER_0_USES_MAX6675)
  1175. return (
  1176. #if ENABLED(MAX6675_IS_MAX31865)
  1177. max31865.temperature(100, 400) // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1178. #else
  1179. raw * 0.25
  1180. #endif
  1181. );
  1182. #elif ENABLED(HEATER_0_USES_AD595)
  1183. return TEMP_AD595(raw);
  1184. #elif ENABLED(HEATER_0_USES_AD8495)
  1185. return TEMP_AD8495(raw);
  1186. #else
  1187. break;
  1188. #endif
  1189. case 1:
  1190. #if ENABLED(HEATER_1_USER_THERMISTOR)
  1191. return user_thermistor_to_deg_c(CTI_HOTEND_1, raw);
  1192. #elif ENABLED(HEATER_1_USES_MAX6675)
  1193. return raw * 0.25;
  1194. #elif ENABLED(HEATER_1_USES_AD595)
  1195. return TEMP_AD595(raw);
  1196. #elif ENABLED(HEATER_1_USES_AD8495)
  1197. return TEMP_AD8495(raw);
  1198. #else
  1199. break;
  1200. #endif
  1201. case 2:
  1202. #if ENABLED(HEATER_2_USER_THERMISTOR)
  1203. return user_thermistor_to_deg_c(CTI_HOTEND_2, raw);
  1204. #elif ENABLED(HEATER_2_USES_AD595)
  1205. return TEMP_AD595(raw);
  1206. #elif ENABLED(HEATER_2_USES_AD8495)
  1207. return TEMP_AD8495(raw);
  1208. #else
  1209. break;
  1210. #endif
  1211. case 3:
  1212. #if ENABLED(HEATER_3_USER_THERMISTOR)
  1213. return user_thermistor_to_deg_c(CTI_HOTEND_3, raw);
  1214. #elif ENABLED(HEATER_3_USES_AD595)
  1215. return TEMP_AD595(raw);
  1216. #elif ENABLED(HEATER_3_USES_AD8495)
  1217. return TEMP_AD8495(raw);
  1218. #else
  1219. break;
  1220. #endif
  1221. case 4:
  1222. #if ENABLED(HEATER_4_USER_THERMISTOR)
  1223. return user_thermistor_to_deg_c(CTI_HOTEND_4, raw);
  1224. #elif ENABLED(HEATER_4_USES_AD595)
  1225. return TEMP_AD595(raw);
  1226. #elif ENABLED(HEATER_4_USES_AD8495)
  1227. return TEMP_AD8495(raw);
  1228. #else
  1229. break;
  1230. #endif
  1231. case 5:
  1232. #if ENABLED(HEATER_5_USER_THERMISTOR)
  1233. return user_thermistor_to_deg_c(CTI_HOTEND_5, raw);
  1234. #elif ENABLED(HEATER_5_USES_AD595)
  1235. return TEMP_AD595(raw);
  1236. #elif ENABLED(HEATER_5_USES_AD8495)
  1237. return TEMP_AD8495(raw);
  1238. #else
  1239. break;
  1240. #endif
  1241. default: break;
  1242. }
  1243. #if HOTEND_USES_THERMISTOR
  1244. // Thermistor with conversion table?
  1245. const short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  1246. SCAN_THERMISTOR_TABLE((*tt), heater_ttbllen_map[e]);
  1247. #endif
  1248. return 0;
  1249. }
  1250. #endif // HOTENDS
  1251. #if HAS_HEATED_BED
  1252. // Derived from RepRap FiveD extruder::getTemperature()
  1253. // For bed temperature measurement.
  1254. float Temperature::analog_to_celsius_bed(const int raw) {
  1255. #if ENABLED(HEATER_BED_USER_THERMISTOR)
  1256. return user_thermistor_to_deg_c(CTI_BED, raw);
  1257. #elif ENABLED(HEATER_BED_USES_THERMISTOR)
  1258. SCAN_THERMISTOR_TABLE(BED_TEMPTABLE, BED_TEMPTABLE_LEN);
  1259. #elif ENABLED(HEATER_BED_USES_AD595)
  1260. return TEMP_AD595(raw);
  1261. #elif ENABLED(HEATER_BED_USES_AD8495)
  1262. return TEMP_AD8495(raw);
  1263. #else
  1264. return 0;
  1265. #endif
  1266. }
  1267. #endif // HAS_HEATED_BED
  1268. #if HAS_TEMP_CHAMBER
  1269. // Derived from RepRap FiveD extruder::getTemperature()
  1270. // For chamber temperature measurement.
  1271. float Temperature::analog_to_celsius_chamber(const int raw) {
  1272. #if ENABLED(HEATER_CHAMBER_USER_THERMISTOR)
  1273. return user_thermistor_to_deg_c(CTI_CHAMBER, raw);
  1274. #elif ENABLED(HEATER_CHAMBER_USES_THERMISTOR)
  1275. SCAN_THERMISTOR_TABLE(CHAMBER_TEMPTABLE, CHAMBER_TEMPTABLE_LEN);
  1276. #elif ENABLED(HEATER_CHAMBER_USES_AD595)
  1277. return TEMP_AD595(raw);
  1278. #elif ENABLED(HEATER_CHAMBER_USES_AD8495)
  1279. return TEMP_AD8495(raw);
  1280. #else
  1281. return 0;
  1282. #endif
  1283. }
  1284. #endif // HAS_TEMP_CHAMBER
  1285. /**
  1286. * Get the raw values into the actual temperatures.
  1287. * The raw values are created in interrupt context,
  1288. * and this function is called from normal context
  1289. * as it would block the stepper routine.
  1290. */
  1291. void Temperature::updateTemperaturesFromRawValues() {
  1292. #if ENABLED(HEATER_0_USES_MAX6675)
  1293. temp_hotend[0].raw = READ_MAX6675(0);
  1294. #endif
  1295. #if ENABLED(HEATER_1_USES_MAX6675)
  1296. temp_hotend[1].raw = READ_MAX6675(1);
  1297. #endif
  1298. #if HOTENDS
  1299. HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e);
  1300. #endif
  1301. #if HAS_HEATED_BED
  1302. temp_bed.celsius = analog_to_celsius_bed(temp_bed.raw);
  1303. #endif
  1304. #if HAS_TEMP_CHAMBER
  1305. temp_chamber.celsius = analog_to_celsius_chamber(temp_chamber.raw);
  1306. #endif
  1307. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1308. redundant_temperature = analog_to_celsius_hotend(redundant_temperature_raw, 1);
  1309. #endif
  1310. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1311. filwidth.update_measured_mm();
  1312. #endif
  1313. // Reset the watchdog on good temperature measurement
  1314. watchdog_refresh();
  1315. temp_meas_ready = false;
  1316. }
  1317. #if MAX6675_SEPARATE_SPI
  1318. SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  1319. #endif
  1320. // Init fans according to whether they're native PWM or Software PWM
  1321. #ifdef ALFAWISE_UX0
  1322. #define _INIT_SOFT_FAN(P) OUT_WRITE_OD(P, FAN_INVERTING ? LOW : HIGH)
  1323. #else
  1324. #define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH)
  1325. #endif
  1326. #if ENABLED(FAN_SOFT_PWM)
  1327. #define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P)
  1328. #else
  1329. #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0)
  1330. #endif
  1331. #if ENABLED(FAST_PWM_FAN)
  1332. #define SET_FAST_PWM_FREQ(P) set_pwm_frequency(P, FAST_PWM_FAN_FREQUENCY)
  1333. #else
  1334. #define SET_FAST_PWM_FREQ(P) NOOP
  1335. #endif
  1336. #define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0)
  1337. #if EXTRUDER_AUTO_FAN_SPEED != 255
  1338. #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)
  1339. #else
  1340. #define INIT_E_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1341. #endif
  1342. #if CHAMBER_AUTO_FAN_SPEED != 255
  1343. #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)
  1344. #else
  1345. #define INIT_CHAMBER_AUTO_FAN_PIN(P) SET_OUTPUT(P)
  1346. #endif
  1347. /**
  1348. * Initialize the temperature manager
  1349. * The manager is implemented by periodic calls to manage_heater()
  1350. */
  1351. void Temperature::init() {
  1352. #if ENABLED(MAX6675_IS_MAX31865)
  1353. max31865.begin(MAX31865_2WIRE); // MAX31865_2WIRE, MAX31865_3WIRE, MAX31865_4WIRE
  1354. #endif
  1355. #if EARLY_WATCHDOG
  1356. // Flag that the thermalManager should be running
  1357. if (inited) return;
  1358. inited = true;
  1359. #endif
  1360. #if MB(RUMBA)
  1361. #define _AD(N) (ANY(HEATER_##N##_USES_AD595, HEATER_##N##_USES_AD8495))
  1362. #if _AD(0) || _AD(1) || _AD(2) || _AD(3) || _AD(4) || _AD(5) || _AD(BED) || _AD(CHAMBER)
  1363. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  1364. MCUCR = _BV(JTD);
  1365. MCUCR = _BV(JTD);
  1366. #endif
  1367. #endif
  1368. #if BOTH(PIDTEMP, PID_EXTRUSION_SCALING)
  1369. last_e_position = 0;
  1370. #endif
  1371. #if HAS_HEATER_0
  1372. #ifdef ALFAWISE_UX0
  1373. OUT_WRITE_OD(HEATER_0_PIN, HEATER_0_INVERTING);
  1374. #else
  1375. OUT_WRITE(HEATER_0_PIN, HEATER_0_INVERTING);
  1376. #endif
  1377. #endif
  1378. #if HAS_HEATER_1
  1379. OUT_WRITE(HEATER_1_PIN, HEATER_1_INVERTING);
  1380. #endif
  1381. #if HAS_HEATER_2
  1382. OUT_WRITE(HEATER_2_PIN, HEATER_2_INVERTING);
  1383. #endif
  1384. #if HAS_HEATER_3
  1385. OUT_WRITE(HEATER_3_PIN, HEATER_3_INVERTING);
  1386. #endif
  1387. #if HAS_HEATER_4
  1388. OUT_WRITE(HEATER_4_PIN, HEATER_4_INVERTING);
  1389. #endif
  1390. #if HAS_HEATER_5
  1391. OUT_WRITE(HEATER_5_PIN, HEATER_5_INVERTING);
  1392. #endif
  1393. #if HAS_HEATED_BED
  1394. #ifdef ALFAWISE_UX0
  1395. OUT_WRITE_OD(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1396. #else
  1397. OUT_WRITE(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1398. #endif
  1399. #endif
  1400. #if HAS_HEATED_CHAMBER
  1401. OUT_WRITE(HEATER_CHAMBER_PIN, HEATER_CHAMBER_INVERTING);
  1402. #endif
  1403. #if HAS_FAN0
  1404. INIT_FAN_PIN(FAN_PIN);
  1405. #endif
  1406. #if HAS_FAN1
  1407. INIT_FAN_PIN(FAN1_PIN);
  1408. #endif
  1409. #if HAS_FAN2
  1410. INIT_FAN_PIN(FAN2_PIN);
  1411. #endif
  1412. #if ENABLED(USE_CONTROLLER_FAN)
  1413. INIT_FAN_PIN(CONTROLLER_FAN_PIN);
  1414. #endif
  1415. #if MAX6675_SEPARATE_SPI
  1416. OUT_WRITE(SCK_PIN, LOW);
  1417. OUT_WRITE(MOSI_PIN, HIGH);
  1418. SET_INPUT_PULLUP(MISO_PIN);
  1419. max6675_spi.init();
  1420. OUT_WRITE(SS_PIN, HIGH);
  1421. OUT_WRITE(MAX6675_SS_PIN, HIGH);
  1422. #endif
  1423. #if ENABLED(HEATER_1_USES_MAX6675)
  1424. OUT_WRITE(MAX6675_SS2_PIN, HIGH);
  1425. #endif
  1426. HAL_adc_init();
  1427. #if HAS_TEMP_ADC_0
  1428. HAL_ANALOG_SELECT(TEMP_0_PIN);
  1429. #endif
  1430. #if HAS_TEMP_ADC_1
  1431. HAL_ANALOG_SELECT(TEMP_1_PIN);
  1432. #endif
  1433. #if HAS_TEMP_ADC_2
  1434. HAL_ANALOG_SELECT(TEMP_2_PIN);
  1435. #endif
  1436. #if HAS_TEMP_ADC_3
  1437. HAL_ANALOG_SELECT(TEMP_3_PIN);
  1438. #endif
  1439. #if HAS_TEMP_ADC_4
  1440. HAL_ANALOG_SELECT(TEMP_4_PIN);
  1441. #endif
  1442. #if HAS_TEMP_ADC_5
  1443. HAL_ANALOG_SELECT(TEMP_5_PIN);
  1444. #endif
  1445. #if HAS_JOY_ADC_X
  1446. HAL_ANALOG_SELECT(JOY_X_PIN);
  1447. #endif
  1448. #if HAS_JOY_ADC_Y
  1449. HAL_ANALOG_SELECT(JOY_Y_PIN);
  1450. #endif
  1451. #if HAS_JOY_ADC_Z
  1452. HAL_ANALOG_SELECT(JOY_Z_PIN);
  1453. #endif
  1454. #if HAS_JOY_ADC_EN
  1455. SET_INPUT_PULLUP(JOY_EN_PIN);
  1456. #endif
  1457. #if HAS_HEATED_BED
  1458. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  1459. #endif
  1460. #if HAS_TEMP_CHAMBER
  1461. HAL_ANALOG_SELECT(TEMP_CHAMBER_PIN);
  1462. #endif
  1463. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1464. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  1465. #endif
  1466. #if HAS_ADC_BUTTONS
  1467. HAL_ANALOG_SELECT(ADC_KEYPAD_PIN);
  1468. #endif
  1469. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  1470. ENABLE_TEMPERATURE_INTERRUPT();
  1471. #if HAS_AUTO_FAN_0
  1472. INIT_E_AUTO_FAN_PIN(E0_AUTO_FAN_PIN);
  1473. #endif
  1474. #if HAS_AUTO_FAN_1 && !_EFANOVERLAP(1,0)
  1475. INIT_E_AUTO_FAN_PIN(E1_AUTO_FAN_PIN);
  1476. #endif
  1477. #if HAS_AUTO_FAN_2 && !(_EFANOVERLAP(2,0) || _EFANOVERLAP(2,1))
  1478. INIT_E_AUTO_FAN_PIN(E2_AUTO_FAN_PIN);
  1479. #endif
  1480. #if HAS_AUTO_FAN_3 && !(_EFANOVERLAP(3,0) || _EFANOVERLAP(3,1) || _EFANOVERLAP(3,2))
  1481. INIT_E_AUTO_FAN_PIN(E3_AUTO_FAN_PIN);
  1482. #endif
  1483. #if HAS_AUTO_FAN_4 && !(_EFANOVERLAP(4,0) || _EFANOVERLAP(4,1) || _EFANOVERLAP(4,2) || _EFANOVERLAP(4,3))
  1484. INIT_E_AUTO_FAN_PIN(E4_AUTO_FAN_PIN);
  1485. #endif
  1486. #if HAS_AUTO_FAN_5 && !(_EFANOVERLAP(5,0) || _EFANOVERLAP(5,1) || _EFANOVERLAP(5,2) || _EFANOVERLAP(5,3) || _EFANOVERLAP(5,4))
  1487. INIT_E_AUTO_FAN_PIN(E5_AUTO_FAN_PIN);
  1488. #endif
  1489. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_E
  1490. INIT_CHAMBER_AUTO_FAN_PIN(CHAMBER_AUTO_FAN_PIN);
  1491. #endif
  1492. // Wait for temperature measurement to settle
  1493. delay(250);
  1494. #if HOTENDS
  1495. #define _TEMP_MIN_E(NR) do{ \
  1496. temp_range[NR].mintemp = HEATER_ ##NR## _MINTEMP; \
  1497. while (analog_to_celsius_hotend(temp_range[NR].raw_min, NR) < HEATER_ ##NR## _MINTEMP) \
  1498. temp_range[NR].raw_min += TEMPDIR(NR) * (OVERSAMPLENR); \
  1499. }while(0)
  1500. #define _TEMP_MAX_E(NR) do{ \
  1501. temp_range[NR].maxtemp = HEATER_ ##NR## _MAXTEMP; \
  1502. while (analog_to_celsius_hotend(temp_range[NR].raw_max, NR) > HEATER_ ##NR## _MAXTEMP) \
  1503. temp_range[NR].raw_max -= TEMPDIR(NR) * (OVERSAMPLENR); \
  1504. }while(0)
  1505. #ifdef HEATER_0_MINTEMP
  1506. _TEMP_MIN_E(0);
  1507. #endif
  1508. #ifdef HEATER_0_MAXTEMP
  1509. _TEMP_MAX_E(0);
  1510. #endif
  1511. #if HOTENDS > 1
  1512. #ifdef HEATER_1_MINTEMP
  1513. _TEMP_MIN_E(1);
  1514. #endif
  1515. #ifdef HEATER_1_MAXTEMP
  1516. _TEMP_MAX_E(1);
  1517. #endif
  1518. #if HOTENDS > 2
  1519. #ifdef HEATER_2_MINTEMP
  1520. _TEMP_MIN_E(2);
  1521. #endif
  1522. #ifdef HEATER_2_MAXTEMP
  1523. _TEMP_MAX_E(2);
  1524. #endif
  1525. #if HOTENDS > 3
  1526. #ifdef HEATER_3_MINTEMP
  1527. _TEMP_MIN_E(3);
  1528. #endif
  1529. #ifdef HEATER_3_MAXTEMP
  1530. _TEMP_MAX_E(3);
  1531. #endif
  1532. #if HOTENDS > 4
  1533. #ifdef HEATER_4_MINTEMP
  1534. _TEMP_MIN_E(4);
  1535. #endif
  1536. #ifdef HEATER_4_MAXTEMP
  1537. _TEMP_MAX_E(4);
  1538. #endif
  1539. #if HOTENDS > 5
  1540. #ifdef HEATER_5_MINTEMP
  1541. _TEMP_MIN_E(5);
  1542. #endif
  1543. #ifdef HEATER_5_MAXTEMP
  1544. _TEMP_MAX_E(5);
  1545. #endif
  1546. #endif // HOTENDS > 5
  1547. #endif // HOTENDS > 4
  1548. #endif // HOTENDS > 3
  1549. #endif // HOTENDS > 2
  1550. #endif // HOTENDS > 1
  1551. #endif // HOTENDS
  1552. #if HAS_HEATED_BED
  1553. #ifdef BED_MINTEMP
  1554. while (analog_to_celsius_bed(mintemp_raw_BED) < BED_MINTEMP) mintemp_raw_BED += TEMPDIR(BED) * (OVERSAMPLENR);
  1555. #endif
  1556. #ifdef BED_MAXTEMP
  1557. while (analog_to_celsius_bed(maxtemp_raw_BED) > BED_MAXTEMP) maxtemp_raw_BED -= TEMPDIR(BED) * (OVERSAMPLENR);
  1558. #endif
  1559. #endif // HAS_HEATED_BED
  1560. #if HAS_HEATED_CHAMBER
  1561. #ifdef CHAMBER_MINTEMP
  1562. while (analog_to_celsius_chamber(mintemp_raw_CHAMBER) < CHAMBER_MINTEMP) mintemp_raw_CHAMBER += TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1563. #endif
  1564. #ifdef CHAMBER_MAXTEMP
  1565. while (analog_to_celsius_chamber(maxtemp_raw_CHAMBER) > CHAMBER_MAXTEMP) maxtemp_raw_CHAMBER -= TEMPDIR(CHAMBER) * (OVERSAMPLENR);
  1566. #endif
  1567. #endif
  1568. #if ENABLED(PROBING_HEATERS_OFF)
  1569. paused = false;
  1570. #endif
  1571. }
  1572. #if WATCH_HOTENDS
  1573. /**
  1574. * Start Heating Sanity Check for hotends that are below
  1575. * their target temperature by a configurable margin.
  1576. * This is called when the temperature is set. (M104, M109)
  1577. */
  1578. void Temperature::start_watching_hotend(const uint8_t E_NAME) {
  1579. const uint8_t ee = HOTEND_INDEX;
  1580. if (degTargetHotend(ee) && degHotend(ee) < degTargetHotend(ee) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1581. watch_hotend[ee].target = degHotend(ee) + WATCH_TEMP_INCREASE;
  1582. watch_hotend[ee].next_ms = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1583. }
  1584. else
  1585. watch_hotend[ee].next_ms = 0;
  1586. }
  1587. #endif
  1588. #if WATCH_BED
  1589. /**
  1590. * Start Heating Sanity Check for hotends that are below
  1591. * their target temperature by a configurable margin.
  1592. * This is called when the temperature is set. (M140, M190)
  1593. */
  1594. void Temperature::start_watching_bed() {
  1595. if (degTargetBed() && degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1596. watch_bed.target = degBed() + WATCH_BED_TEMP_INCREASE;
  1597. watch_bed.next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1598. }
  1599. else
  1600. watch_bed.next_ms = 0;
  1601. }
  1602. #endif
  1603. #if WATCH_CHAMBER
  1604. /**
  1605. * Start Heating Sanity Check for chamber that is below
  1606. * its target temperature by a configurable margin.
  1607. * This is called when the temperature is set. (M141, M191)
  1608. */
  1609. void Temperature::start_watching_chamber() {
  1610. if (degChamber() < degTargetChamber() - (WATCH_CHAMBER_TEMP_INCREASE + TEMP_CHAMBER_HYSTERESIS + 1)) {
  1611. watch_chamber.target = degChamber() + WATCH_CHAMBER_TEMP_INCREASE;
  1612. watch_chamber.next_ms = millis() + (WATCH_CHAMBER_TEMP_PERIOD) * 1000UL;
  1613. }
  1614. else
  1615. watch_chamber.next_ms = 0;
  1616. }
  1617. #endif
  1618. #if HAS_THERMAL_PROTECTION
  1619. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1620. Temperature::tr_state_machine_t Temperature::tr_state_machine[HOTENDS]; // = { { TRInactive, 0 } };
  1621. #endif
  1622. #if HAS_THERMALLY_PROTECTED_BED
  1623. Temperature::tr_state_machine_t Temperature::tr_state_machine_bed; // = { TRInactive, 0 };
  1624. #endif
  1625. #if ENABLED(THERMAL_PROTECTION_CHAMBER)
  1626. Temperature::tr_state_machine_t Temperature::tr_state_machine_chamber; // = { TRInactive, 0 };
  1627. #endif
  1628. 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) {
  1629. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1630. /**
  1631. SERIAL_ECHO_START();
  1632. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1633. if (heater_id == H_CHAMBER) SERIAL_ECHOPGM("chamber");
  1634. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1635. SERIAL_ECHOPAIR(" ; State:", sm.state, " ; Timer:", sm.timer, " ; Temperature:", current, " ; Target Temp:", target);
  1636. if (heater_id >= 0)
  1637. SERIAL_ECHOPAIR(" ; Idle Timeout:", hotend_idle[heater_id].timed_out);
  1638. else
  1639. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle.timed_out);
  1640. SERIAL_EOL();
  1641. //*/
  1642. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1643. #if HEATER_IDLE_HANDLER
  1644. // If the heater idle timeout expires, restart
  1645. if ((heater_id >= 0 && hotend_idle[heater_id].timed_out)
  1646. #if HAS_HEATED_BED
  1647. || (heater_id < 0 && bed_idle.timed_out)
  1648. #endif
  1649. ) {
  1650. sm.state = TRInactive;
  1651. tr_target_temperature[heater_index] = 0;
  1652. }
  1653. else
  1654. #endif
  1655. {
  1656. // If the target temperature changes, restart
  1657. if (tr_target_temperature[heater_index] != target) {
  1658. tr_target_temperature[heater_index] = target;
  1659. sm.state = target > 0 ? TRFirstHeating : TRInactive;
  1660. }
  1661. }
  1662. switch (sm.state) {
  1663. // Inactive state waits for a target temperature to be set
  1664. case TRInactive: break;
  1665. // When first heating, wait for the temperature to be reached then go to Stable state
  1666. case TRFirstHeating:
  1667. if (current < tr_target_temperature[heater_index]) break;
  1668. sm.state = TRStable;
  1669. // While the temperature is stable watch for a bad temperature
  1670. case TRStable:
  1671. #if ENABLED(ADAPTIVE_FAN_SLOWING)
  1672. if (adaptive_fan_slowing && heater_id >= 0) {
  1673. const int fan_index = _MIN(heater_id, FAN_COUNT - 1);
  1674. if (fan_speed[fan_index] == 0 || current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.25f))
  1675. fan_speed_scaler[fan_index] = 128;
  1676. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.3335f))
  1677. fan_speed_scaler[fan_index] = 96;
  1678. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.5f))
  1679. fan_speed_scaler[fan_index] = 64;
  1680. else if (current >= tr_target_temperature[heater_id] - (hysteresis_degc * 0.8f))
  1681. fan_speed_scaler[fan_index] = 32;
  1682. else
  1683. fan_speed_scaler[fan_index] = 0;
  1684. }
  1685. #endif
  1686. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1687. sm.timer = millis() + period_seconds * 1000UL;
  1688. break;
  1689. }
  1690. else if (PENDING(millis(), sm.timer)) break;
  1691. sm.state = TRRunaway;
  1692. case TRRunaway:
  1693. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), GET_TEXT(MSG_THERMAL_RUNAWAY));
  1694. }
  1695. }
  1696. #endif // HAS_THERMAL_PROTECTION
  1697. void Temperature::disable_all_heaters() {
  1698. #if ENABLED(AUTOTEMP)
  1699. planner.autotemp_enabled = false;
  1700. #endif
  1701. #if HOTENDS
  1702. HOTEND_LOOP() setTargetHotend(0, e);
  1703. #endif
  1704. #if HAS_HEATED_BED
  1705. setTargetBed(0);
  1706. #endif
  1707. #if HAS_HEATED_CHAMBER
  1708. setTargetChamber(0);
  1709. #endif
  1710. // Unpause and reset everything
  1711. #if ENABLED(PROBING_HEATERS_OFF)
  1712. pause(false);
  1713. #endif
  1714. #define DISABLE_HEATER(NR) { \
  1715. setTargetHotend(0, NR); \
  1716. temp_hotend[NR].soft_pwm_amount = 0; \
  1717. WRITE_HEATER_ ##NR (LOW); \
  1718. }
  1719. #if HAS_TEMP_HOTEND
  1720. REPEAT(HOTENDS, DISABLE_HEATER);
  1721. #endif
  1722. #if HAS_HEATED_BED
  1723. temp_bed.target = 0;
  1724. temp_bed.soft_pwm_amount = 0;
  1725. WRITE_HEATER_BED(LOW);
  1726. #endif
  1727. #if HAS_HEATED_CHAMBER
  1728. temp_chamber.target = 0;
  1729. temp_chamber.soft_pwm_amount = 0;
  1730. WRITE_HEATER_CHAMBER(LOW);
  1731. #endif
  1732. }
  1733. #if ENABLED(PROBING_HEATERS_OFF)
  1734. void Temperature::pause(const bool p) {
  1735. if (p != paused) {
  1736. paused = p;
  1737. if (p) {
  1738. HOTEND_LOOP() hotend_idle[e].expire(); // timeout immediately
  1739. #if HAS_HEATED_BED
  1740. bed_idle.expire(); // timeout immediately
  1741. #endif
  1742. }
  1743. else {
  1744. HOTEND_LOOP() reset_heater_idle_timer(e);
  1745. #if HAS_HEATED_BED
  1746. reset_bed_idle_timer();
  1747. #endif
  1748. }
  1749. }
  1750. }
  1751. #endif // PROBING_HEATERS_OFF
  1752. #if HAS_MAX6675
  1753. int Temperature::read_max6675(
  1754. #if COUNT_6675 > 1
  1755. const uint8_t hindex
  1756. #endif
  1757. ) {
  1758. #if COUNT_6675 == 1
  1759. constexpr uint8_t hindex = 0;
  1760. #else
  1761. // Needed to return the correct temp when this is called too soon
  1762. static uint16_t max6675_temp_previous[COUNT_6675] = { 0 };
  1763. #endif
  1764. #define MAX6675_HEAT_INTERVAL 250UL
  1765. #if ENABLED(MAX6675_IS_MAX31855)
  1766. static uint32_t max6675_temp = 2000;
  1767. #define MAX6675_ERROR_MASK 7
  1768. #define MAX6675_DISCARD_BITS 18
  1769. #define MAX6675_SPEED_BITS 3 // (_BV(SPR1)) // clock ÷ 64
  1770. #else
  1771. static uint16_t max6675_temp = 2000;
  1772. #define MAX6675_ERROR_MASK 4
  1773. #define MAX6675_DISCARD_BITS 3
  1774. #define MAX6675_SPEED_BITS 2 // (_BV(SPR0)) // clock ÷ 16
  1775. #endif
  1776. // Return last-read value between readings
  1777. static millis_t next_max6675_ms[COUNT_6675] = { 0 };
  1778. millis_t ms = millis();
  1779. if (PENDING(ms, next_max6675_ms[hindex]))
  1780. return int(
  1781. #if COUNT_6675 == 1
  1782. max6675_temp
  1783. #else
  1784. max6675_temp_previous[hindex] // Need to return the correct previous value
  1785. #endif
  1786. );
  1787. next_max6675_ms[hindex] = ms + MAX6675_HEAT_INTERVAL;
  1788. #if ENABLED(MAX6675_IS_MAX31865)
  1789. max6675_temp = int(max31865.temperature(100, 400)); // 100 ohms = PT100 resistance. 400 ohms = calibration resistor
  1790. #endif
  1791. //
  1792. // TODO: spiBegin, spiRec and spiInit doesn't work when soft spi is used.
  1793. //
  1794. #if !MAX6675_SEPARATE_SPI
  1795. spiBegin();
  1796. spiInit(MAX6675_SPEED_BITS);
  1797. #endif
  1798. #if COUNT_6675 > 1
  1799. #define WRITE_MAX6675(V) do{ switch (hindex) { case 1: WRITE(MAX6675_SS2_PIN, V); break; default: WRITE(MAX6675_SS_PIN, V); } }while(0)
  1800. #define SET_OUTPUT_MAX6675() do{ switch (hindex) { case 1: SET_OUTPUT(MAX6675_SS2_PIN); break; default: SET_OUTPUT(MAX6675_SS_PIN); } }while(0)
  1801. #elif ENABLED(HEATER_1_USES_MAX6675)
  1802. #define WRITE_MAX6675(V) WRITE(MAX6675_SS2_PIN, V)
  1803. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS2_PIN)
  1804. #else
  1805. #define WRITE_MAX6675(V) WRITE(MAX6675_SS_PIN, V)
  1806. #define SET_OUTPUT_MAX6675() SET_OUTPUT(MAX6675_SS_PIN)
  1807. #endif
  1808. SET_OUTPUT_MAX6675();
  1809. WRITE_MAX6675(LOW); // enable TT_MAX6675
  1810. DELAY_NS(100); // Ensure 100ns delay
  1811. // Read a big-endian temperature value
  1812. max6675_temp = 0;
  1813. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1814. max6675_temp |= (
  1815. #if MAX6675_SEPARATE_SPI
  1816. max6675_spi.receive()
  1817. #else
  1818. spiRec()
  1819. #endif
  1820. );
  1821. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1822. }
  1823. WRITE_MAX6675(HIGH); // disable TT_MAX6675
  1824. if (max6675_temp & MAX6675_ERROR_MASK) {
  1825. SERIAL_ERROR_START();
  1826. SERIAL_ECHOPGM("Temp measurement error! ");
  1827. #if MAX6675_ERROR_MASK == 7
  1828. SERIAL_ECHOPGM("MAX31855 ");
  1829. if (max6675_temp & 1)
  1830. SERIAL_ECHOLNPGM("Open Circuit");
  1831. else if (max6675_temp & 2)
  1832. SERIAL_ECHOLNPGM("Short to GND");
  1833. else if (max6675_temp & 4)
  1834. SERIAL_ECHOLNPGM("Short to VCC");
  1835. #else
  1836. SERIAL_ECHOLNPGM("MAX6675");
  1837. #endif
  1838. // Thermocouple open
  1839. max6675_temp = 4 * (
  1840. #if COUNT_6675 > 1
  1841. hindex ? HEATER_1_MAX6675_TMAX : HEATER_0_MAX6675_TMAX
  1842. #elif ENABLED(HEATER_1_USES_MAX6675)
  1843. HEATER_1_MAX6675_TMAX
  1844. #else
  1845. HEATER_0_MAX6675_TMAX
  1846. #endif
  1847. );
  1848. }
  1849. else
  1850. max6675_temp >>= MAX6675_DISCARD_BITS;
  1851. #if ENABLED(MAX6675_IS_MAX31855)
  1852. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000; // Support negative temperature
  1853. #endif
  1854. #if COUNT_6675 > 1
  1855. max6675_temp_previous[hindex] = max6675_temp;
  1856. #endif
  1857. return int(max6675_temp);
  1858. }
  1859. #endif // HAS_MAX6675
  1860. /**
  1861. * Get raw temperatures
  1862. */
  1863. void Temperature::set_current_temp_raw() {
  1864. #if HAS_TEMP_ADC_0 && DISABLED(HEATER_0_USES_MAX6675)
  1865. temp_hotend[0].update();
  1866. #endif
  1867. #if HAS_TEMP_ADC_1
  1868. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1869. redundant_temperature_raw = temp_hotend[1].acc;
  1870. #elif DISABLED(HEATER_1_USES_MAX6675)
  1871. temp_hotend[1].update();
  1872. #endif
  1873. #if HAS_TEMP_ADC_2
  1874. temp_hotend[2].update();
  1875. #if HAS_TEMP_ADC_3
  1876. temp_hotend[3].update();
  1877. #if HAS_TEMP_ADC_4
  1878. temp_hotend[4].update();
  1879. #if HAS_TEMP_ADC_5
  1880. temp_hotend[5].update();
  1881. #endif // HAS_TEMP_ADC_5
  1882. #endif // HAS_TEMP_ADC_4
  1883. #endif // HAS_TEMP_ADC_3
  1884. #endif // HAS_TEMP_ADC_2
  1885. #endif // HAS_TEMP_ADC_1
  1886. #if HAS_HEATED_BED
  1887. temp_bed.update();
  1888. #endif
  1889. #if HAS_TEMP_CHAMBER
  1890. temp_chamber.update();
  1891. #endif
  1892. #if HAS_JOY_ADC_X
  1893. joystick.x.update();
  1894. #endif
  1895. #if HAS_JOY_ADC_Y
  1896. joystick.y.update();
  1897. #endif
  1898. #if HAS_JOY_ADC_Z
  1899. joystick.z.update();
  1900. #endif
  1901. temp_meas_ready = true;
  1902. }
  1903. void Temperature::readings_ready() {
  1904. // Update the raw values if they've been read. Else we could be updating them during reading.
  1905. if (!temp_meas_ready) set_current_temp_raw();
  1906. // Filament Sensor - can be read any time since IIR filtering is used
  1907. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1908. filwidth.reading_ready();
  1909. #endif
  1910. #if HOTENDS
  1911. HOTEND_LOOP() temp_hotend[e].reset();
  1912. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1913. temp_hotend[1].reset();
  1914. #endif
  1915. #endif
  1916. #if HAS_HEATED_BED
  1917. temp_bed.reset();
  1918. #endif
  1919. #if HAS_TEMP_CHAMBER
  1920. temp_chamber.reset();
  1921. #endif
  1922. #if HAS_JOY_ADC_X
  1923. joystick.x.reset();
  1924. #endif
  1925. #if HAS_JOY_ADC_Y
  1926. joystick.y.reset();
  1927. #endif
  1928. #if HAS_JOY_ADC_Z
  1929. joystick.z.reset();
  1930. #endif
  1931. #if HOTENDS
  1932. static constexpr int8_t temp_dir[] = {
  1933. #if ENABLED(HEATER_0_USES_MAX6675)
  1934. 0
  1935. #else
  1936. TEMPDIR(0)
  1937. #endif
  1938. #if HOTENDS > 1
  1939. #define _TEMPDIR(N) , TEMPDIR(N)
  1940. #if ENABLED(HEATER_1_USES_MAX6675)
  1941. , 0
  1942. #else
  1943. _TEMPDIR(1)
  1944. #endif
  1945. #if HOTENDS > 2
  1946. REPEAT_S(2, HOTENDS, _TEMPDIR)
  1947. #endif // HOTENDS > 2
  1948. #endif // HOTENDS > 1
  1949. };
  1950. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1951. const int8_t tdir = temp_dir[e];
  1952. if (tdir) {
  1953. const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
  1954. const bool heater_on = (temp_hotend[e].target > 0
  1955. #if ENABLED(PIDTEMP)
  1956. || temp_hotend[e].soft_pwm_amount > 0
  1957. #endif
  1958. );
  1959. if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_ind_t)e);
  1960. if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
  1961. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1962. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1963. #endif
  1964. min_temp_error((heater_ind_t)e);
  1965. }
  1966. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1967. else
  1968. consecutive_low_temperature_error[e] = 0;
  1969. #endif
  1970. }
  1971. }
  1972. #endif // HOTENDS
  1973. #if HAS_HEATED_BED
  1974. #if TEMPDIR(BED) < 0
  1975. #define BEDCMP(A,B) ((A)<=(B))
  1976. #else
  1977. #define BEDCMP(A,B) ((A)>=(B))
  1978. #endif
  1979. const bool bed_on = (temp_bed.target > 0)
  1980. #if ENABLED(PIDTEMPBED)
  1981. || (temp_bed.soft_pwm_amount > 0)
  1982. #endif
  1983. ;
  1984. if (BEDCMP(temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
  1985. if (bed_on && BEDCMP(mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
  1986. #endif
  1987. #if HAS_HEATED_CHAMBER
  1988. #if TEMPDIR(CHAMBER) < 0
  1989. #define CHAMBERCMP(A,B) ((A)<=(B))
  1990. #else
  1991. #define CHAMBERCMP(A,B) ((A)>=(B))
  1992. #endif
  1993. const bool chamber_on = (temp_chamber.target > 0);
  1994. if (CHAMBERCMP(temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
  1995. if (chamber_on && CHAMBERCMP(mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
  1996. #endif
  1997. }
  1998. /**
  1999. * Timer 0 is shared with millies so don't change the prescaler.
  2000. *
  2001. * On AVR this ISR uses the compare method so it runs at the base
  2002. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  2003. * in OCR0B above (128 or halfway between OVFs).
  2004. *
  2005. * - Manage PWM to all the heaters and fan
  2006. * - Prepare or Measure one of the raw ADC sensor values
  2007. * - Check new temperature values for MIN/MAX errors (kill on error)
  2008. * - Step the babysteps value for each axis towards 0
  2009. * - For PINS_DEBUGGING, monitor and report endstop pins
  2010. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  2011. * - Call planner.tick to count down its "ignore" time
  2012. */
  2013. HAL_TEMP_TIMER_ISR() {
  2014. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  2015. Temperature::tick();
  2016. HAL_timer_isr_epilogue(TEMP_TIMER_NUM);
  2017. }
  2018. #if ENABLED(SLOW_PWM_HEATERS) && !defined(MIN_STATE_TIME)
  2019. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  2020. #endif
  2021. class SoftPWM {
  2022. public:
  2023. uint8_t count;
  2024. inline bool add(const uint8_t mask, const uint8_t amount) {
  2025. count = (count & mask) + amount; return (count > mask);
  2026. }
  2027. #if ENABLED(SLOW_PWM_HEATERS)
  2028. bool state_heater;
  2029. uint8_t state_timer_heater;
  2030. inline void dec() { if (state_timer_heater > 0) state_timer_heater--; }
  2031. inline bool ready(const bool v) {
  2032. const bool rdy = !state_timer_heater;
  2033. if (rdy && state_heater != v) {
  2034. state_heater = v;
  2035. state_timer_heater = MIN_STATE_TIME;
  2036. }
  2037. return rdy;
  2038. }
  2039. #endif
  2040. };
  2041. /**
  2042. * Handle various ~1KHz tasks associated with temperature
  2043. * - Heater PWM (~1KHz with scaler)
  2044. * - LCD Button polling (~500Hz)
  2045. * - Start / Read one ADC sensor
  2046. * - Advance Babysteps
  2047. * - Endstop polling
  2048. * - Planner clean buffer
  2049. */
  2050. void Temperature::tick() {
  2051. static int8_t temp_count = -1;
  2052. static ADCSensorState adc_sensor_state = StartupDelay;
  2053. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  2054. // avoid multiple loads of pwm_count
  2055. uint8_t pwm_count_tmp = pwm_count;
  2056. #if HAS_ADC_BUTTONS
  2057. static unsigned int raw_ADCKey_value = 0;
  2058. static bool ADCKey_pressed = false;
  2059. #endif
  2060. #if HOTENDS
  2061. static SoftPWM soft_pwm_hotend[HOTENDS];
  2062. #endif
  2063. #if HAS_HEATED_BED
  2064. static SoftPWM soft_pwm_bed;
  2065. #endif
  2066. #if HAS_HEATED_CHAMBER
  2067. static SoftPWM soft_pwm_chamber;
  2068. #endif
  2069. #if DISABLED(SLOW_PWM_HEATERS)
  2070. #if HOTENDS || HAS_HEATED_BED || HAS_HEATED_CHAMBER
  2071. constexpr uint8_t pwm_mask =
  2072. #if ENABLED(SOFT_PWM_DITHER)
  2073. _BV(SOFT_PWM_SCALE) - 1
  2074. #else
  2075. 0
  2076. #endif
  2077. ;
  2078. #define _PWM_MOD(N,S,T) do{ \
  2079. const bool on = S.add(pwm_mask, T.soft_pwm_amount); \
  2080. WRITE_HEATER_##N(on); \
  2081. }while(0)
  2082. #endif
  2083. /**
  2084. * Standard heater PWM modulation
  2085. */
  2086. if (pwm_count_tmp >= 127) {
  2087. pwm_count_tmp -= 127;
  2088. #if HOTENDS
  2089. #define _PWM_MOD_E(N) _PWM_MOD(N,soft_pwm_hotend[N],temp_hotend[N]);
  2090. REPEAT(HOTENDS, _PWM_MOD_E);
  2091. #endif
  2092. #if HAS_HEATED_BED
  2093. _PWM_MOD(BED,soft_pwm_bed,temp_bed);
  2094. #endif
  2095. #if HAS_HEATED_CHAMBER
  2096. _PWM_MOD(CHAMBER,soft_pwm_chamber,temp_chamber);
  2097. #endif
  2098. #if ENABLED(FAN_SOFT_PWM)
  2099. #define _FAN_PWM(N) do{ \
  2100. uint8_t &spcf = soft_pwm_count_fan[N]; \
  2101. spcf = (spcf & pwm_mask) + (soft_pwm_amount_fan[N] >> 1); \
  2102. WRITE_FAN(N, spcf > pwm_mask ? HIGH : LOW); \
  2103. }while(0)
  2104. #if HAS_FAN0
  2105. _FAN_PWM(0);
  2106. #endif
  2107. #if HAS_FAN1
  2108. _FAN_PWM(1);
  2109. #endif
  2110. #if HAS_FAN2
  2111. _FAN_PWM(2);
  2112. #endif
  2113. #endif
  2114. }
  2115. else {
  2116. #define _PWM_LOW(N,S) do{ if (S.count <= pwm_count_tmp) WRITE_HEATER_##N(LOW); }while(0)
  2117. #if HOTENDS
  2118. #define _PWM_LOW_E(N) _PWM_LOW(N, soft_pwm_hotend[N]);
  2119. REPEAT(HOTENDS, _PWM_LOW_E);
  2120. #endif
  2121. #if HAS_HEATED_BED
  2122. _PWM_LOW(BED, soft_pwm_bed);
  2123. #endif
  2124. #if HAS_HEATED_CHAMBER
  2125. _PWM_LOW(CHAMBER, soft_pwm_chamber);
  2126. #endif
  2127. #if ENABLED(FAN_SOFT_PWM)
  2128. #if HAS_FAN0
  2129. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2130. #endif
  2131. #if HAS_FAN1
  2132. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2133. #endif
  2134. #if HAS_FAN2
  2135. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2136. #endif
  2137. #endif
  2138. }
  2139. // SOFT_PWM_SCALE to frequency:
  2140. //
  2141. // 0: 16000000/64/256/128 = 7.6294 Hz
  2142. // 1: / 64 = 15.2588 Hz
  2143. // 2: / 32 = 30.5176 Hz
  2144. // 3: / 16 = 61.0352 Hz
  2145. // 4: / 8 = 122.0703 Hz
  2146. // 5: / 4 = 244.1406 Hz
  2147. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2148. #else // SLOW_PWM_HEATERS
  2149. /**
  2150. * SLOW PWM HEATERS
  2151. *
  2152. * For relay-driven heaters
  2153. */
  2154. #define _SLOW_SET(NR,PWM,V) do{ if (PWM.ready(V)) WRITE_HEATER_##NR(V); }while(0)
  2155. #define _SLOW_PWM(NR,PWM,SRC) do{ PWM.count = SRC.soft_pwm_amount; _SLOW_SET(NR,PWM,(PWM.count > 0)); }while(0)
  2156. #define _PWM_OFF(NR,PWM) do{ if (PWM.count < slow_pwm_count) _SLOW_SET(NR,PWM,0); }while(0)
  2157. static uint8_t slow_pwm_count = 0;
  2158. if (slow_pwm_count == 0) {
  2159. #if HOTENDS
  2160. #define _SLOW_PWM_E(N) _SLOW_PWM(N, soft_pwm_hotend[N], temp_hotend[N]);
  2161. REPEAT(HOTENDS, _SLOW_PWM_E);
  2162. #endif
  2163. #if HAS_HEATED_BED
  2164. _SLOW_PWM(BED, soft_pwm_bed, temp_bed);
  2165. #endif
  2166. } // slow_pwm_count == 0
  2167. #if HOTENDS
  2168. #define _PWM_OFF_E(N) _PWM_OFF(N, soft_pwm_hotend[N]);
  2169. REPEAT(HOTENDS, _PWM_OFF_E);
  2170. #endif
  2171. #if HAS_HEATED_BED
  2172. _PWM_OFF(BED, soft_pwm_bed);
  2173. #endif
  2174. #if ENABLED(FAN_SOFT_PWM)
  2175. if (pwm_count_tmp >= 127) {
  2176. pwm_count_tmp = 0;
  2177. #define _PWM_FAN(N) do{ \
  2178. soft_pwm_count_fan[N] = soft_pwm_amount_fan[N] >> 1; \
  2179. WRITE_FAN(N, soft_pwm_count_fan[N] > 0 ? HIGH : LOW); \
  2180. }while(0)
  2181. #if HAS_FAN0
  2182. _PWM_FAN(0);
  2183. #endif
  2184. #if HAS_FAN1
  2185. _PWM_FAN(1);
  2186. #endif
  2187. #if HAS_FAN2
  2188. _PWM_FAN(2);
  2189. #endif
  2190. }
  2191. #if HAS_FAN0
  2192. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0, LOW);
  2193. #endif
  2194. #if HAS_FAN1
  2195. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN(1, LOW);
  2196. #endif
  2197. #if HAS_FAN2
  2198. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN(2, LOW);
  2199. #endif
  2200. #endif // FAN_SOFT_PWM
  2201. // SOFT_PWM_SCALE to frequency:
  2202. //
  2203. // 0: 16000000/64/256/128 = 7.6294 Hz
  2204. // 1: / 64 = 15.2588 Hz
  2205. // 2: / 32 = 30.5176 Hz
  2206. // 3: / 16 = 61.0352 Hz
  2207. // 4: / 8 = 122.0703 Hz
  2208. // 5: / 4 = 244.1406 Hz
  2209. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  2210. // increment slow_pwm_count only every 64th pwm_count,
  2211. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  2212. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  2213. slow_pwm_count++;
  2214. slow_pwm_count &= 0x7F;
  2215. #if HOTENDS
  2216. HOTEND_LOOP() soft_pwm_hotend[e].dec();
  2217. #endif
  2218. #if HAS_HEATED_BED
  2219. soft_pwm_bed.dec();
  2220. #endif
  2221. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  2222. #endif // SLOW_PWM_HEATERS
  2223. //
  2224. // Update lcd buttons 488 times per second
  2225. //
  2226. static bool do_buttons;
  2227. if ((do_buttons ^= true)) ui.update_buttons();
  2228. /**
  2229. * One sensor is sampled on every other call of the ISR.
  2230. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  2231. *
  2232. * On each Prepare pass, ADC is started for a sensor pin.
  2233. * On the next pass, the ADC value is read and accumulated.
  2234. *
  2235. * This gives each ADC 0.9765ms to charge up.
  2236. */
  2237. #define ACCUMULATE_ADC(obj) do{ \
  2238. if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; \
  2239. else obj.sample(HAL_READ_ADC()); \
  2240. }while(0)
  2241. ADCSensorState next_sensor_state = adc_sensor_state < SensorsReady ? (ADCSensorState)(int(adc_sensor_state) + 1) : StartSampling;
  2242. switch (adc_sensor_state) {
  2243. case SensorsReady: {
  2244. // All sensors have been read. Stay in this state for a few
  2245. // ISRs to save on calls to temp update/checking code below.
  2246. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  2247. static uint8_t delay_count = 0;
  2248. if (extra_loops > 0) {
  2249. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  2250. if (--delay_count) // While delaying...
  2251. next_sensor_state = SensorsReady; // retain this state (else, next state will be 0)
  2252. break;
  2253. }
  2254. else {
  2255. adc_sensor_state = StartSampling; // Fall-through to start sampling
  2256. next_sensor_state = (ADCSensorState)(int(StartSampling) + 1);
  2257. }
  2258. }
  2259. case StartSampling: // Start of sampling loops. Do updates/checks.
  2260. if (++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  2261. temp_count = 0;
  2262. readings_ready();
  2263. }
  2264. break;
  2265. #if HAS_TEMP_ADC_0
  2266. case PrepareTemp_0: HAL_START_ADC(TEMP_0_PIN); break;
  2267. case MeasureTemp_0: ACCUMULATE_ADC(temp_hotend[0]); break;
  2268. #endif
  2269. #if HAS_HEATED_BED
  2270. case PrepareTemp_BED: HAL_START_ADC(TEMP_BED_PIN); break;
  2271. case MeasureTemp_BED: ACCUMULATE_ADC(temp_bed); break;
  2272. #endif
  2273. #if HAS_TEMP_CHAMBER
  2274. case PrepareTemp_CHAMBER: HAL_START_ADC(TEMP_CHAMBER_PIN); break;
  2275. case MeasureTemp_CHAMBER: ACCUMULATE_ADC(temp_chamber); break;
  2276. #endif
  2277. #if HAS_TEMP_ADC_1
  2278. case PrepareTemp_1: HAL_START_ADC(TEMP_1_PIN); break;
  2279. case MeasureTemp_1: ACCUMULATE_ADC(temp_hotend[1]); break;
  2280. #endif
  2281. #if HAS_TEMP_ADC_2
  2282. case PrepareTemp_2: HAL_START_ADC(TEMP_2_PIN); break;
  2283. case MeasureTemp_2: ACCUMULATE_ADC(temp_hotend[2]); break;
  2284. #endif
  2285. #if HAS_TEMP_ADC_3
  2286. case PrepareTemp_3: HAL_START_ADC(TEMP_3_PIN); break;
  2287. case MeasureTemp_3: ACCUMULATE_ADC(temp_hotend[3]); break;
  2288. #endif
  2289. #if HAS_TEMP_ADC_4
  2290. case PrepareTemp_4: HAL_START_ADC(TEMP_4_PIN); break;
  2291. case MeasureTemp_4: ACCUMULATE_ADC(temp_hotend[4]); break;
  2292. #endif
  2293. #if HAS_TEMP_ADC_5
  2294. case PrepareTemp_5: HAL_START_ADC(TEMP_5_PIN); break;
  2295. case MeasureTemp_5: ACCUMULATE_ADC(temp_hotend[5]); break;
  2296. #endif
  2297. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  2298. case Prepare_FILWIDTH: HAL_START_ADC(FILWIDTH_PIN); break;
  2299. case Measure_FILWIDTH:
  2300. if (!HAL_ADC_READY())
  2301. next_sensor_state = adc_sensor_state; // redo this state
  2302. else
  2303. filwidth.accumulate(HAL_READ_ADC());
  2304. break;
  2305. #endif
  2306. #if HAS_JOY_ADC_X
  2307. case PrepareJoy_X: HAL_START_ADC(JOY_X_PIN); break;
  2308. case MeasureJoy_X: ACCUMULATE_ADC(joystick.x); break;
  2309. #endif
  2310. #if HAS_JOY_ADC_Y
  2311. case PrepareJoy_Y: HAL_START_ADC(JOY_Y_PIN); break;
  2312. case MeasureJoy_Y: ACCUMULATE_ADC(joystick.y); break;
  2313. #endif
  2314. #if HAS_JOY_ADC_Z
  2315. case PrepareJoy_Z: HAL_START_ADC(JOY_Z_PIN); break;
  2316. case MeasureJoy_Z: ACCUMULATE_ADC(joystick.z); break;
  2317. #endif
  2318. #if HAS_ADC_BUTTONS
  2319. case Prepare_ADC_KEY: HAL_START_ADC(ADC_KEYPAD_PIN); break;
  2320. case Measure_ADC_KEY:
  2321. if (!HAL_ADC_READY())
  2322. next_sensor_state = adc_sensor_state; // redo this state
  2323. else if (ADCKey_count < 16) {
  2324. raw_ADCKey_value = HAL_READ_ADC();
  2325. if (raw_ADCKey_value <= 900UL * HAL_ADC_RANGE / 1024UL) {
  2326. NOMORE(current_ADCKey_raw, raw_ADCKey_value);
  2327. ADCKey_count++;
  2328. }
  2329. else { //ADC Key release
  2330. if (ADCKey_count > 0) ADCKey_count++; else ADCKey_pressed = false;
  2331. if (ADCKey_pressed) {
  2332. ADCKey_count = 0;
  2333. current_ADCKey_raw = HAL_ADC_RANGE;
  2334. }
  2335. }
  2336. }
  2337. if (ADCKey_count == 16) ADCKey_pressed = true;
  2338. break;
  2339. #endif // ADC_KEYPAD
  2340. case StartupDelay: break;
  2341. } // switch(adc_sensor_state)
  2342. // Go to the next state
  2343. adc_sensor_state = next_sensor_state;
  2344. //
  2345. // Additional ~1KHz Tasks
  2346. //
  2347. #if ENABLED(BABYSTEPPING)
  2348. babystep.task();
  2349. #endif
  2350. // Poll endstops state, if required
  2351. endstops.poll();
  2352. // Periodically call the planner timer
  2353. planner.tick();
  2354. }
  2355. #if HAS_TEMP_SENSOR
  2356. #include "../gcode/gcode.h"
  2357. static void print_heater_state(const float &c, const float &t
  2358. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2359. , const float r
  2360. #endif
  2361. , const heater_ind_t e=INDEX_NONE
  2362. ) {
  2363. char k;
  2364. switch (e) {
  2365. #if HAS_TEMP_CHAMBER
  2366. case H_CHAMBER: k = 'C'; break;
  2367. #endif
  2368. #if HAS_TEMP_HOTEND
  2369. default: k = 'T'; break;
  2370. #if HAS_HEATED_BED
  2371. case H_BED: k = 'B'; break;
  2372. #endif
  2373. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2374. case H_REDUNDANT: k = 'R'; break;
  2375. #endif
  2376. #elif HAS_HEATED_BED
  2377. default: k = 'B'; break;
  2378. #endif
  2379. }
  2380. SERIAL_CHAR(' ');
  2381. SERIAL_CHAR(k);
  2382. #if HOTENDS > 1
  2383. if (e >= 0) SERIAL_CHAR('0' + e);
  2384. #endif
  2385. SERIAL_CHAR(':');
  2386. SERIAL_ECHO(c);
  2387. SERIAL_ECHOPAIR(" /" , t);
  2388. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2389. SERIAL_ECHOPAIR(" (", r * RECIPROCAL(OVERSAMPLENR));
  2390. SERIAL_CHAR(')');
  2391. #endif
  2392. delay(2);
  2393. }
  2394. void Temperature::print_heater_states(const uint8_t target_extruder
  2395. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2396. , const bool include_r/*=false*/
  2397. #endif
  2398. ) {
  2399. #if HAS_TEMP_HOTEND
  2400. print_heater_state(degHotend(target_extruder), degTargetHotend(target_extruder)
  2401. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2402. , rawHotendTemp(target_extruder)
  2403. #endif
  2404. );
  2405. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  2406. if (include_r) print_heater_state(redundant_temperature, degTargetHotend(target_extruder)
  2407. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2408. , redundant_temperature_raw
  2409. #endif
  2410. , H_REDUNDANT
  2411. );
  2412. #endif
  2413. #endif
  2414. #if HAS_HEATED_BED
  2415. print_heater_state(degBed(), degTargetBed()
  2416. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2417. , rawBedTemp()
  2418. #endif
  2419. , H_BED
  2420. );
  2421. #endif
  2422. #if HAS_TEMP_CHAMBER
  2423. print_heater_state(degChamber()
  2424. #if HAS_HEATED_CHAMBER
  2425. , degTargetChamber()
  2426. #else
  2427. , 0
  2428. #endif
  2429. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2430. , rawChamberTemp()
  2431. #endif
  2432. , H_CHAMBER
  2433. );
  2434. #endif // HAS_TEMP_CHAMBER
  2435. #if HOTENDS > 1
  2436. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e)
  2437. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2438. , rawHotendTemp(e)
  2439. #endif
  2440. , (heater_ind_t)e
  2441. );
  2442. #endif
  2443. SERIAL_ECHOPAIR(" @:", getHeaterPower((heater_ind_t)target_extruder));
  2444. #if HAS_HEATED_BED
  2445. SERIAL_ECHOPAIR(" B@:", getHeaterPower(H_BED));
  2446. #endif
  2447. #if HAS_HEATED_CHAMBER
  2448. SERIAL_ECHOPAIR(" C@:", getHeaterPower(H_CHAMBER));
  2449. #endif
  2450. #if HOTENDS > 1
  2451. HOTEND_LOOP() {
  2452. SERIAL_ECHOPAIR(" @", e);
  2453. SERIAL_CHAR(':');
  2454. SERIAL_ECHO(getHeaterPower((heater_ind_t)e));
  2455. }
  2456. #endif
  2457. }
  2458. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  2459. uint8_t Temperature::auto_report_temp_interval;
  2460. millis_t Temperature::next_temp_report_ms;
  2461. void Temperature::auto_report_temperatures() {
  2462. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  2463. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  2464. PORT_REDIRECT(SERIAL_BOTH);
  2465. print_heater_states(active_extruder);
  2466. SERIAL_EOL();
  2467. }
  2468. }
  2469. #endif // AUTO_REPORT_TEMPERATURES
  2470. #if HOTENDS && HAS_DISPLAY
  2471. void Temperature::set_heating_message(const uint8_t e) {
  2472. const bool heating = isHeatingHotend(e);
  2473. ui.status_printf_P(0,
  2474. #if HOTENDS > 1
  2475. PSTR("E%c " S_FMT), '1' + e
  2476. #else
  2477. PSTR("E " S_FMT)
  2478. #endif
  2479. , heating ? GET_TEXT(MSG_HEATING) : GET_TEXT(MSG_COOLING)
  2480. );
  2481. }
  2482. #endif
  2483. #if HAS_TEMP_HOTEND
  2484. #ifndef MIN_COOLING_SLOPE_DEG
  2485. #define MIN_COOLING_SLOPE_DEG 1.50
  2486. #endif
  2487. #ifndef MIN_COOLING_SLOPE_TIME
  2488. #define MIN_COOLING_SLOPE_TIME 60
  2489. #endif
  2490. bool Temperature::wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling/*=true*/
  2491. #if G26_CLICK_CAN_CANCEL
  2492. , const bool click_to_cancel/*=false*/
  2493. #endif
  2494. ) {
  2495. #if TEMP_RESIDENCY_TIME > 0
  2496. millis_t residency_start_ms = 0;
  2497. // Loop until the temperature has stabilized
  2498. #define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_RESIDENCY_TIME) * 1000UL))
  2499. #else
  2500. // Loop until the temperature is very close target
  2501. #define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
  2502. #endif
  2503. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2504. KEEPALIVE_STATE(NOT_BUSY);
  2505. #endif
  2506. #if ENABLED(PRINTER_EVENT_LEDS)
  2507. const float start_temp = degHotend(target_extruder);
  2508. printerEventLEDs.onHotendHeatingStart();
  2509. #endif
  2510. float target_temp = -1.0, old_temp = 9999.0;
  2511. bool wants_to_cool = false, first_loop = true;
  2512. wait_for_heatup = true;
  2513. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2514. do {
  2515. // Target temperature might be changed during the loop
  2516. if (target_temp != degTargetHotend(target_extruder)) {
  2517. wants_to_cool = isCoolingHotend(target_extruder);
  2518. target_temp = degTargetHotend(target_extruder);
  2519. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2520. if (no_wait_for_cooling && wants_to_cool) break;
  2521. }
  2522. now = millis();
  2523. if (ELAPSED(now, next_temp_ms)) { // Print temp & remaining time every 1s while waiting
  2524. next_temp_ms = now + 1000UL;
  2525. print_heater_states(target_extruder);
  2526. #if TEMP_RESIDENCY_TIME > 0
  2527. SERIAL_ECHOPGM(" W:");
  2528. if (residency_start_ms)
  2529. SERIAL_ECHO(long((((TEMP_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2530. else
  2531. SERIAL_CHAR('?');
  2532. #endif
  2533. SERIAL_EOL();
  2534. }
  2535. idle();
  2536. gcode.reset_stepper_timeout(); // Keep steppers powered
  2537. const float temp = degHotend(target_extruder);
  2538. #if ENABLED(PRINTER_EVENT_LEDS)
  2539. // Gradually change LED strip from violet to red as nozzle heats up
  2540. if (!wants_to_cool) printerEventLEDs.onHotendHeating(start_temp, temp, target_temp);
  2541. #endif
  2542. #if TEMP_RESIDENCY_TIME > 0
  2543. const float temp_diff = ABS(target_temp - temp);
  2544. if (!residency_start_ms) {
  2545. // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
  2546. if (temp_diff < TEMP_WINDOW) {
  2547. residency_start_ms = now;
  2548. if (first_loop) residency_start_ms += (TEMP_RESIDENCY_TIME) * 1000UL;
  2549. }
  2550. }
  2551. else if (temp_diff > TEMP_HYSTERESIS) {
  2552. // Restart the timer whenever the temperature falls outside the hysteresis.
  2553. residency_start_ms = now;
  2554. }
  2555. #endif
  2556. // Prevent a wait-forever situation if R is misused i.e. M109 R0
  2557. if (wants_to_cool) {
  2558. // break after MIN_COOLING_SLOPE_TIME seconds
  2559. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG
  2560. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2561. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG)) break;
  2562. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME;
  2563. old_temp = temp;
  2564. }
  2565. }
  2566. #if G26_CLICK_CAN_CANCEL
  2567. if (click_to_cancel && ui.use_click()) {
  2568. wait_for_heatup = false;
  2569. ui.quick_feedback();
  2570. }
  2571. #endif
  2572. first_loop = false;
  2573. } while (wait_for_heatup && TEMP_CONDITIONS);
  2574. if (wait_for_heatup) {
  2575. ui.reset_status();
  2576. #if ENABLED(PRINTER_EVENT_LEDS)
  2577. printerEventLEDs.onHeatingDone();
  2578. #endif
  2579. }
  2580. return wait_for_heatup;
  2581. }
  2582. #endif // HAS_TEMP_HOTEND
  2583. #if HAS_HEATED_BED
  2584. #ifndef MIN_COOLING_SLOPE_DEG_BED
  2585. #define MIN_COOLING_SLOPE_DEG_BED 1.50
  2586. #endif
  2587. #ifndef MIN_COOLING_SLOPE_TIME_BED
  2588. #define MIN_COOLING_SLOPE_TIME_BED 60
  2589. #endif
  2590. bool Temperature::wait_for_bed(const bool no_wait_for_cooling/*=true*/
  2591. #if G26_CLICK_CAN_CANCEL
  2592. , const bool click_to_cancel/*=false*/
  2593. #endif
  2594. ) {
  2595. #if TEMP_BED_RESIDENCY_TIME > 0
  2596. millis_t residency_start_ms = 0;
  2597. bool first_loop = true;
  2598. // Loop until the temperature has stabilized
  2599. #define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_BED_RESIDENCY_TIME) * 1000UL))
  2600. #else
  2601. // Loop until the temperature is very close target
  2602. #define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
  2603. #endif
  2604. float target_temp = -1, old_temp = 9999;
  2605. bool wants_to_cool = false;
  2606. wait_for_heatup = true;
  2607. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2608. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2609. KEEPALIVE_STATE(NOT_BUSY);
  2610. #endif
  2611. #if ENABLED(PRINTER_EVENT_LEDS)
  2612. const float start_temp = degBed();
  2613. printerEventLEDs.onBedHeatingStart();
  2614. #endif
  2615. do {
  2616. // Target temperature might be changed during the loop
  2617. if (target_temp != degTargetBed()) {
  2618. wants_to_cool = isCoolingBed();
  2619. target_temp = degTargetBed();
  2620. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2621. if (no_wait_for_cooling && wants_to_cool) break;
  2622. }
  2623. now = millis();
  2624. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2625. next_temp_ms = now + 1000UL;
  2626. print_heater_states(active_extruder);
  2627. #if TEMP_BED_RESIDENCY_TIME > 0
  2628. SERIAL_ECHOPGM(" W:");
  2629. if (residency_start_ms)
  2630. SERIAL_ECHO(long((((TEMP_BED_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2631. else
  2632. SERIAL_CHAR('?');
  2633. #endif
  2634. SERIAL_EOL();
  2635. }
  2636. idle();
  2637. gcode.reset_stepper_timeout(); // Keep steppers powered
  2638. const float temp = degBed();
  2639. #if ENABLED(PRINTER_EVENT_LEDS)
  2640. // Gradually change LED strip from blue to violet as bed heats up
  2641. if (!wants_to_cool) printerEventLEDs.onBedHeating(start_temp, temp, target_temp);
  2642. #endif
  2643. #if TEMP_BED_RESIDENCY_TIME > 0
  2644. const float temp_diff = ABS(target_temp - temp);
  2645. if (!residency_start_ms) {
  2646. // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time.
  2647. if (temp_diff < TEMP_BED_WINDOW) {
  2648. residency_start_ms = now;
  2649. if (first_loop) residency_start_ms += (TEMP_BED_RESIDENCY_TIME) * 1000UL;
  2650. }
  2651. }
  2652. else if (temp_diff > TEMP_BED_HYSTERESIS) {
  2653. // Restart the timer whenever the temperature falls outside the hysteresis.
  2654. residency_start_ms = now;
  2655. }
  2656. #endif // TEMP_BED_RESIDENCY_TIME > 0
  2657. // Prevent a wait-forever situation if R is misused i.e. M190 R0
  2658. if (wants_to_cool) {
  2659. // Break after MIN_COOLING_SLOPE_TIME_BED seconds
  2660. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_BED
  2661. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2662. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_BED)) break;
  2663. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_BED;
  2664. old_temp = temp;
  2665. }
  2666. }
  2667. #if G26_CLICK_CAN_CANCEL
  2668. if (click_to_cancel && ui.use_click()) {
  2669. wait_for_heatup = false;
  2670. ui.quick_feedback();
  2671. }
  2672. #endif
  2673. #if TEMP_BED_RESIDENCY_TIME > 0
  2674. first_loop = false;
  2675. #endif
  2676. } while (wait_for_heatup && TEMP_BED_CONDITIONS);
  2677. if (wait_for_heatup) ui.reset_status();
  2678. return wait_for_heatup;
  2679. }
  2680. #endif // HAS_HEATED_BED
  2681. #if HAS_HEATED_CHAMBER
  2682. #ifndef MIN_COOLING_SLOPE_DEG_CHAMBER
  2683. #define MIN_COOLING_SLOPE_DEG_CHAMBER 1.50
  2684. #endif
  2685. #ifndef MIN_COOLING_SLOPE_TIME_CHAMBER
  2686. #define MIN_COOLING_SLOPE_TIME_CHAMBER 60
  2687. #endif
  2688. bool Temperature::wait_for_chamber(const bool no_wait_for_cooling/*=true*/) {
  2689. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2690. millis_t residency_start_ms = 0;
  2691. // Loop until the temperature has stabilized
  2692. #define TEMP_CHAMBER_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL))
  2693. #else
  2694. // Loop until the temperature is very close target
  2695. #define TEMP_CHAMBER_CONDITIONS (wants_to_cool ? isCoolingChamber() : isHeatingChamber())
  2696. #endif
  2697. float target_temp = -1, old_temp = 9999;
  2698. bool wants_to_cool = false, first_loop = true;
  2699. wait_for_heatup = true;
  2700. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2701. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2702. KEEPALIVE_STATE(NOT_BUSY);
  2703. #endif
  2704. do {
  2705. // Target temperature might be changed during the loop
  2706. if (target_temp != degTargetChamber()) {
  2707. wants_to_cool = isCoolingChamber();
  2708. target_temp = degTargetChamber();
  2709. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2710. if (no_wait_for_cooling && wants_to_cool) break;
  2711. }
  2712. now = millis();
  2713. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2714. next_temp_ms = now + 1000UL;
  2715. print_heater_states(active_extruder);
  2716. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2717. SERIAL_ECHOPGM(" W:");
  2718. if (residency_start_ms)
  2719. SERIAL_ECHO(long((((TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2720. else
  2721. SERIAL_CHAR('?');
  2722. #endif
  2723. SERIAL_EOL();
  2724. }
  2725. idle();
  2726. gcode.reset_stepper_timeout(); // Keep steppers powered
  2727. const float temp = degChamber();
  2728. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2729. const float temp_diff = ABS(target_temp - temp);
  2730. if (!residency_start_ms) {
  2731. // Start the TEMP_CHAMBER_RESIDENCY_TIME timer when we reach target temp for the first time.
  2732. if (temp_diff < TEMP_CHAMBER_WINDOW) {
  2733. residency_start_ms = now;
  2734. if (first_loop) residency_start_ms += (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL;
  2735. }
  2736. }
  2737. else if (temp_diff > TEMP_CHAMBER_HYSTERESIS) {
  2738. // Restart the timer whenever the temperature falls outside the hysteresis.
  2739. residency_start_ms = now;
  2740. }
  2741. #endif // TEMP_CHAMBER_RESIDENCY_TIME > 0
  2742. // Prevent a wait-forever situation if R is misused i.e. M191 R0
  2743. if (wants_to_cool) {
  2744. // Break after MIN_COOLING_SLOPE_TIME_CHAMBER seconds
  2745. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_CHAMBER
  2746. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2747. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_CHAMBER)) break;
  2748. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_CHAMBER;
  2749. old_temp = temp;
  2750. }
  2751. }
  2752. first_loop = false;
  2753. } while (wait_for_heatup && TEMP_CHAMBER_CONDITIONS);
  2754. if (wait_for_heatup) ui.reset_status();
  2755. return wait_for_heatup;
  2756. }
  2757. #endif // HAS_HEATED_CHAMBER
  2758. #endif // HAS_TEMP_SENSOR