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

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