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

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