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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283
  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 20
  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. float pid_output;
  699. #if ENABLED(PIDTEMP)
  700. #if DISABLED(PID_OPENLOOP)
  701. static hotend_pid_t work_pid[HOTENDS];
  702. static float temp_iState[HOTENDS] = { 0 },
  703. temp_dState[HOTENDS] = { 0 };
  704. static bool pid_reset[HOTENDS] = { false };
  705. const float pid_error = temp_hotend[ee].target - temp_hotend[ee].celsius;
  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. 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. #if HOTENDS > 1
  2546. ui.status_printf_P(0, heating ? PSTR("E%c " MSG_HEATING) : PSTR("E%c " MSG_COOLING), '1' + e);
  2547. #else
  2548. ui.set_status_P(heating ? PSTR("E " MSG_HEATING) : PSTR("E " MSG_COOLING));
  2549. #endif
  2550. }
  2551. #endif
  2552. #if HAS_TEMP_HOTEND
  2553. #ifndef MIN_COOLING_SLOPE_DEG
  2554. #define MIN_COOLING_SLOPE_DEG 1.50
  2555. #endif
  2556. #ifndef MIN_COOLING_SLOPE_TIME
  2557. #define MIN_COOLING_SLOPE_TIME 60
  2558. #endif
  2559. bool Temperature::wait_for_hotend(const uint8_t target_extruder, const bool no_wait_for_cooling/*=true*/
  2560. #if G26_CLICK_CAN_CANCEL
  2561. , const bool click_to_cancel/*=false*/
  2562. #endif
  2563. ) {
  2564. #if TEMP_RESIDENCY_TIME > 0
  2565. millis_t residency_start_ms = 0;
  2566. // Loop until the temperature has stabilized
  2567. #define TEMP_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_RESIDENCY_TIME) * 1000UL))
  2568. #else
  2569. // Loop until the temperature is very close target
  2570. #define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
  2571. #endif
  2572. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2573. KEEPALIVE_STATE(NOT_BUSY);
  2574. #endif
  2575. #if ENABLED(PRINTER_EVENT_LEDS)
  2576. const float start_temp = degHotend(target_extruder);
  2577. printerEventLEDs.onHotendHeatingStart();
  2578. #endif
  2579. float target_temp = -1.0, old_temp = 9999.0;
  2580. bool wants_to_cool = false, first_loop = true;
  2581. wait_for_heatup = true;
  2582. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2583. do {
  2584. // Target temperature might be changed during the loop
  2585. if (target_temp != degTargetHotend(target_extruder)) {
  2586. wants_to_cool = isCoolingHotend(target_extruder);
  2587. target_temp = degTargetHotend(target_extruder);
  2588. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2589. if (no_wait_for_cooling && wants_to_cool) break;
  2590. }
  2591. now = millis();
  2592. if (ELAPSED(now, next_temp_ms)) { // Print temp & remaining time every 1s while waiting
  2593. next_temp_ms = now + 1000UL;
  2594. print_heater_states(target_extruder);
  2595. #if TEMP_RESIDENCY_TIME > 0
  2596. SERIAL_ECHOPGM(" W:");
  2597. if (residency_start_ms)
  2598. SERIAL_ECHO(long((((TEMP_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2599. else
  2600. SERIAL_CHAR('?');
  2601. #endif
  2602. SERIAL_EOL();
  2603. }
  2604. idle();
  2605. gcode.reset_stepper_timeout(); // Keep steppers powered
  2606. const float temp = degHotend(target_extruder);
  2607. #if ENABLED(PRINTER_EVENT_LEDS)
  2608. // Gradually change LED strip from violet to red as nozzle heats up
  2609. if (!wants_to_cool) printerEventLEDs.onHotendHeating(start_temp, temp, target_temp);
  2610. #endif
  2611. #if TEMP_RESIDENCY_TIME > 0
  2612. const float temp_diff = ABS(target_temp - temp);
  2613. if (!residency_start_ms) {
  2614. // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
  2615. if (temp_diff < TEMP_WINDOW) {
  2616. residency_start_ms = now;
  2617. if (first_loop) residency_start_ms += (TEMP_RESIDENCY_TIME) * 1000UL;
  2618. }
  2619. }
  2620. else if (temp_diff > TEMP_HYSTERESIS) {
  2621. // Restart the timer whenever the temperature falls outside the hysteresis.
  2622. residency_start_ms = now;
  2623. }
  2624. #endif
  2625. // Prevent a wait-forever situation if R is misused i.e. M109 R0
  2626. if (wants_to_cool) {
  2627. // break after MIN_COOLING_SLOPE_TIME seconds
  2628. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG
  2629. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2630. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG)) break;
  2631. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME;
  2632. old_temp = temp;
  2633. }
  2634. }
  2635. #if G26_CLICK_CAN_CANCEL
  2636. if (click_to_cancel && ui.use_click()) {
  2637. wait_for_heatup = false;
  2638. ui.quick_feedback();
  2639. }
  2640. #endif
  2641. first_loop = false;
  2642. } while (wait_for_heatup && TEMP_CONDITIONS);
  2643. if (wait_for_heatup) {
  2644. ui.reset_status();
  2645. #if ENABLED(PRINTER_EVENT_LEDS)
  2646. printerEventLEDs.onHeatingDone();
  2647. #endif
  2648. }
  2649. return wait_for_heatup;
  2650. }
  2651. #endif // HAS_TEMP_HOTEND
  2652. #if HAS_HEATED_BED
  2653. #ifndef MIN_COOLING_SLOPE_DEG_BED
  2654. #define MIN_COOLING_SLOPE_DEG_BED 1.50
  2655. #endif
  2656. #ifndef MIN_COOLING_SLOPE_TIME_BED
  2657. #define MIN_COOLING_SLOPE_TIME_BED 60
  2658. #endif
  2659. bool Temperature::wait_for_bed(const bool no_wait_for_cooling/*=true*/
  2660. #if G26_CLICK_CAN_CANCEL
  2661. , const bool click_to_cancel/*=false*/
  2662. #endif
  2663. ) {
  2664. #if TEMP_BED_RESIDENCY_TIME > 0
  2665. millis_t residency_start_ms = 0;
  2666. bool first_loop = true;
  2667. // Loop until the temperature has stabilized
  2668. #define TEMP_BED_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_BED_RESIDENCY_TIME) * 1000UL))
  2669. #else
  2670. // Loop until the temperature is very close target
  2671. #define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
  2672. #endif
  2673. float target_temp = -1, old_temp = 9999;
  2674. bool wants_to_cool = false;
  2675. wait_for_heatup = true;
  2676. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2677. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2678. KEEPALIVE_STATE(NOT_BUSY);
  2679. #endif
  2680. #if ENABLED(PRINTER_EVENT_LEDS)
  2681. const float start_temp = degBed();
  2682. printerEventLEDs.onBedHeatingStart();
  2683. #endif
  2684. do {
  2685. // Target temperature might be changed during the loop
  2686. if (target_temp != degTargetBed()) {
  2687. wants_to_cool = isCoolingBed();
  2688. target_temp = degTargetBed();
  2689. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2690. if (no_wait_for_cooling && wants_to_cool) break;
  2691. }
  2692. now = millis();
  2693. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2694. next_temp_ms = now + 1000UL;
  2695. print_heater_states(active_extruder);
  2696. #if TEMP_BED_RESIDENCY_TIME > 0
  2697. SERIAL_ECHOPGM(" W:");
  2698. if (residency_start_ms)
  2699. SERIAL_ECHO(long((((TEMP_BED_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2700. else
  2701. SERIAL_CHAR('?');
  2702. #endif
  2703. SERIAL_EOL();
  2704. }
  2705. idle();
  2706. gcode.reset_stepper_timeout(); // Keep steppers powered
  2707. const float temp = degBed();
  2708. #if ENABLED(PRINTER_EVENT_LEDS)
  2709. // Gradually change LED strip from blue to violet as bed heats up
  2710. if (!wants_to_cool) printerEventLEDs.onBedHeating(start_temp, temp, target_temp);
  2711. #endif
  2712. #if TEMP_BED_RESIDENCY_TIME > 0
  2713. const float temp_diff = ABS(target_temp - temp);
  2714. if (!residency_start_ms) {
  2715. // Start the TEMP_BED_RESIDENCY_TIME timer when we reach target temp for the first time.
  2716. if (temp_diff < TEMP_BED_WINDOW) {
  2717. residency_start_ms = now;
  2718. if (first_loop) residency_start_ms += (TEMP_BED_RESIDENCY_TIME) * 1000UL;
  2719. }
  2720. }
  2721. else if (temp_diff > TEMP_BED_HYSTERESIS) {
  2722. // Restart the timer whenever the temperature falls outside the hysteresis.
  2723. residency_start_ms = now;
  2724. }
  2725. #endif // TEMP_BED_RESIDENCY_TIME > 0
  2726. // Prevent a wait-forever situation if R is misused i.e. M190 R0
  2727. if (wants_to_cool) {
  2728. // Break after MIN_COOLING_SLOPE_TIME_BED seconds
  2729. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_BED
  2730. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2731. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_BED)) break;
  2732. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_BED;
  2733. old_temp = temp;
  2734. }
  2735. }
  2736. #if G26_CLICK_CAN_CANCEL
  2737. if (click_to_cancel && ui.use_click()) {
  2738. wait_for_heatup = false;
  2739. ui.quick_feedback();
  2740. }
  2741. #endif
  2742. #if TEMP_BED_RESIDENCY_TIME > 0
  2743. first_loop = false;
  2744. #endif
  2745. } while (wait_for_heatup && TEMP_BED_CONDITIONS);
  2746. if (wait_for_heatup) ui.reset_status();
  2747. return wait_for_heatup;
  2748. }
  2749. #endif // HAS_HEATED_BED
  2750. #if 0 && HAS_HEATED_CHAMBER
  2751. #ifndef MIN_COOLING_SLOPE_DEG_CHAMBER
  2752. #define MIN_COOLING_SLOPE_DEG_CHAMBER 1.50
  2753. #endif
  2754. #ifndef MIN_COOLING_SLOPE_TIME_CHAMBER
  2755. #define MIN_COOLING_SLOPE_TIME_CHAMBER 60
  2756. #endif
  2757. bool Temperature::wait_for_chamber(const bool no_wait_for_cooling/*=true*/) {
  2758. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2759. millis_t residency_start_ms = 0;
  2760. // Loop until the temperature has stabilized
  2761. #define TEMP_CHAMBER_CONDITIONS (!residency_start_ms || PENDING(now, residency_start_ms + (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL))
  2762. #else
  2763. // Loop until the temperature is very close target
  2764. #define TEMP_CHAMBER_CONDITIONS (wants_to_cool ? isCoolingChamber() : isHeatingChamber())
  2765. #endif
  2766. float target_temp = -1, old_temp = 9999;
  2767. bool wants_to_cool = false, first_loop = true;
  2768. wait_for_heatup = true;
  2769. millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;
  2770. #if DISABLED(BUSY_WHILE_HEATING) && ENABLED(HOST_KEEPALIVE_FEATURE)
  2771. KEEPALIVE_STATE(NOT_BUSY);
  2772. #endif
  2773. do {
  2774. // Target temperature might be changed during the loop
  2775. if (target_temp != degTargetChamber()) {
  2776. wants_to_cool = isCoolingChamber();
  2777. target_temp = degTargetChamber();
  2778. // Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
  2779. if (no_wait_for_cooling && wants_to_cool) break;
  2780. }
  2781. now = millis();
  2782. if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
  2783. next_temp_ms = now + 1000UL;
  2784. print_heater_states(active_extruder);
  2785. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2786. SERIAL_ECHOPGM(" W:");
  2787. if (residency_start_ms)
  2788. SERIAL_ECHO(long((((TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL) - (now - residency_start_ms)) / 1000UL));
  2789. else
  2790. SERIAL_CHAR('?');
  2791. #endif
  2792. SERIAL_EOL();
  2793. }
  2794. idle();
  2795. gcode.reset_stepper_timeout(); // Keep steppers powered
  2796. const float temp = degChamber();
  2797. #if TEMP_CHAMBER_RESIDENCY_TIME > 0
  2798. const float temp_diff = ABS(target_temp - temp);
  2799. if (!residency_start_ms) {
  2800. // Start the TEMP_CHAMBER_RESIDENCY_TIME timer when we reach target temp for the first time.
  2801. if (temp_diff < TEMP_CHAMBER_WINDOW) {
  2802. residency_start_ms = now;
  2803. if (first_loop) residency_start_ms += (TEMP_CHAMBER_RESIDENCY_TIME) * 1000UL;
  2804. }
  2805. }
  2806. else if (temp_diff > TEMP_CHAMBER_HYSTERESIS) {
  2807. // Restart the timer whenever the temperature falls outside the hysteresis.
  2808. residency_start_ms = now;
  2809. }
  2810. #endif // TEMP_CHAMBER_RESIDENCY_TIME > 0
  2811. // Prevent a wait-forever situation if R is misused i.e. M191 R0
  2812. if (wants_to_cool) {
  2813. // Break after MIN_COOLING_SLOPE_TIME_CHAMBER seconds
  2814. // if the temperature did not drop at least MIN_COOLING_SLOPE_DEG_CHAMBER
  2815. if (!next_cool_check_ms || ELAPSED(now, next_cool_check_ms)) {
  2816. if (old_temp - temp < float(MIN_COOLING_SLOPE_DEG_CHAMBER)) break;
  2817. next_cool_check_ms = now + 1000UL * MIN_COOLING_SLOPE_TIME_CHAMBER;
  2818. old_temp = temp;
  2819. }
  2820. }
  2821. first_loop = false;
  2822. } while (wait_for_heatup && TEMP_CHAMBER_CONDITIONS);
  2823. if (wait_for_heatup) ui.reset_status();
  2824. return wait_for_heatup;
  2825. }
  2826. #endif // HAS_HEATED_CHAMBER
  2827. #endif // HAS_TEMP_SENSOR