My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

temperature.cpp 74KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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/Delay.h"
  32. #if ENABLED(HEATER_0_USES_MAX6675)
  33. #include "../libs/private_spi.h"
  34. #endif
  35. #if ENABLED(BABYSTEPPING) || ENABLED(PID_EXTRUSION_SCALING)
  36. #include "stepper.h"
  37. #endif
  38. #include "printcounter.h"
  39. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  40. #include "../feature/filwidth.h"
  41. #endif
  42. #if ENABLED(EMERGENCY_PARSER)
  43. #include "../feature/emergency_parser.h"
  44. #endif
  45. #if HOTEND_USES_THERMISTOR
  46. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  47. static void* heater_ttbl_map[2] = { (void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  48. static constexpr uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  49. #else
  50. 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);
  51. 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);
  52. #endif
  53. #endif
  54. Temperature thermalManager;
  55. /**
  56. * Macros to include the heater id in temp errors. The compiler's dead-code
  57. * elimination should (hopefully) optimize out the unused strings.
  58. */
  59. #if HAS_HEATED_BED
  60. #define TEMP_ERR_PSTR(MSG, E) \
  61. (E) == -1 ? PSTR(MSG ## _BED) : \
  62. (HOTENDS > 1 && (E) == 1) ? PSTR(MSG_E2 " " MSG) : \
  63. (HOTENDS > 2 && (E) == 2) ? PSTR(MSG_E3 " " MSG) : \
  64. (HOTENDS > 3 && (E) == 3) ? PSTR(MSG_E4 " " MSG) : \
  65. (HOTENDS > 4 && (E) == 4) ? PSTR(MSG_E5 " " MSG) : \
  66. PSTR(MSG_E1 " " MSG)
  67. #else
  68. #define TEMP_ERR_PSTR(MSG, E) \
  69. (HOTENDS > 1 && (E) == 1) ? PSTR(MSG_E2 " " MSG) : \
  70. (HOTENDS > 2 && (E) == 2) ? PSTR(MSG_E3 " " MSG) : \
  71. (HOTENDS > 3 && (E) == 3) ? PSTR(MSG_E4 " " MSG) : \
  72. (HOTENDS > 4 && (E) == 4) ? PSTR(MSG_E5 " " MSG) : \
  73. PSTR(MSG_E1 " " MSG)
  74. #endif
  75. // public:
  76. float Temperature::current_temperature[HOTENDS] = { 0.0 };
  77. int16_t Temperature::current_temperature_raw[HOTENDS] = { 0 },
  78. Temperature::target_temperature[HOTENDS] = { 0 };
  79. #if ENABLED(AUTO_POWER_E_FANS)
  80. int16_t Temperature::autofan_speed[HOTENDS] = { 0 };
  81. #endif
  82. #if HAS_HEATED_BED
  83. float Temperature::current_temperature_bed = 0.0;
  84. int16_t Temperature::current_temperature_bed_raw = 0,
  85. Temperature::target_temperature_bed = 0;
  86. uint8_t Temperature::soft_pwm_amount_bed;
  87. #ifdef BED_MINTEMP
  88. int16_t Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  89. #endif
  90. #ifdef BED_MAXTEMP
  91. int16_t Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  92. #endif
  93. #if WATCH_THE_BED
  94. uint16_t Temperature::watch_target_bed_temp = 0;
  95. millis_t Temperature::watch_bed_next_ms = 0;
  96. #endif
  97. #if ENABLED(PIDTEMPBED)
  98. float Temperature::bedKp, Temperature::bedKi, Temperature::bedKd, // Initialized by settings.load()
  99. Temperature::temp_iState_bed = { 0 },
  100. Temperature::temp_dState_bed = { 0 },
  101. Temperature::pTerm_bed,
  102. Temperature::iTerm_bed,
  103. Temperature::dTerm_bed,
  104. Temperature::pid_error_bed;
  105. #else
  106. millis_t Temperature::next_bed_check_ms;
  107. #endif
  108. uint16_t Temperature::raw_temp_bed_value = 0;
  109. #if HEATER_IDLE_HANDLER
  110. millis_t Temperature::bed_idle_timeout_ms = 0;
  111. bool Temperature::bed_idle_timeout_exceeded = false;
  112. #endif
  113. #endif // HAS_HEATED_BED
  114. #if HAS_TEMP_CHAMBER
  115. float Temperature::current_temperature_chamber = 0.0;
  116. int16_t Temperature::current_temperature_chamber_raw = 0;
  117. uint16_t Temperature::raw_temp_chamber_value = 0;
  118. #endif
  119. // Initialized by settings.load()
  120. #if ENABLED(PIDTEMP)
  121. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  122. float Temperature::Kp[HOTENDS], Temperature::Ki[HOTENDS], Temperature::Kd[HOTENDS];
  123. #if ENABLED(PID_EXTRUSION_SCALING)
  124. float Temperature::Kc[HOTENDS];
  125. #endif
  126. #else
  127. float Temperature::Kp, Temperature::Ki, Temperature::Kd;
  128. #if ENABLED(PID_EXTRUSION_SCALING)
  129. float Temperature::Kc;
  130. #endif
  131. #endif
  132. #endif
  133. #if ENABLED(BABYSTEPPING)
  134. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  135. #endif
  136. #if WATCH_HOTENDS
  137. uint16_t Temperature::watch_target_temp[HOTENDS] = { 0 };
  138. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  139. #endif
  140. #if ENABLED(PREVENT_COLD_EXTRUSION)
  141. bool Temperature::allow_cold_extrude = false;
  142. int16_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  143. #endif
  144. // private:
  145. #if EARLY_WATCHDOG
  146. bool Temperature::inited = false;
  147. #endif
  148. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  149. uint16_t Temperature::redundant_temperature_raw = 0;
  150. float Temperature::redundant_temperature = 0.0;
  151. #endif
  152. volatile bool Temperature::temp_meas_ready = false;
  153. #if ENABLED(PIDTEMP)
  154. float Temperature::temp_iState[HOTENDS] = { 0 },
  155. Temperature::temp_dState[HOTENDS] = { 0 },
  156. Temperature::pTerm[HOTENDS],
  157. Temperature::iTerm[HOTENDS],
  158. Temperature::dTerm[HOTENDS];
  159. #if ENABLED(PID_EXTRUSION_SCALING)
  160. float Temperature::cTerm[HOTENDS];
  161. long Temperature::last_e_position;
  162. long Temperature::lpq[LPQ_MAX_LEN];
  163. int Temperature::lpq_ptr = 0;
  164. #endif
  165. float Temperature::pid_error[HOTENDS];
  166. bool Temperature::pid_reset[HOTENDS];
  167. #endif
  168. uint16_t Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 };
  169. // Init min and max temp with extreme values to prevent false errors during startup
  170. int16_t Temperature::minttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP, HEATER_4_RAW_LO_TEMP),
  171. Temperature::maxttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP, HEATER_4_RAW_HI_TEMP),
  172. Temperature::minttemp[HOTENDS] = { 0 },
  173. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  174. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  175. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  176. #endif
  177. #ifdef MILLISECONDS_PREHEAT_TIME
  178. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  179. #endif
  180. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  181. int8_t Temperature::meas_shift_index; // Index of a delayed sample in buffer
  182. #endif
  183. #if HAS_AUTO_FAN
  184. millis_t Temperature::next_auto_fan_check_ms = 0;
  185. #endif
  186. uint8_t Temperature::soft_pwm_amount[HOTENDS];
  187. #if ENABLED(FAN_SOFT_PWM)
  188. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  189. Temperature::soft_pwm_count_fan[FAN_COUNT];
  190. #endif
  191. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  192. uint16_t Temperature::current_raw_filwidth = 0; // Measured filament diameter - one extruder only
  193. #endif
  194. #if ENABLED(PROBING_HEATERS_OFF)
  195. bool Temperature::paused;
  196. #endif
  197. #if HEATER_IDLE_HANDLER
  198. millis_t Temperature::heater_idle_timeout_ms[HOTENDS] = { 0 };
  199. bool Temperature::heater_idle_timeout_exceeded[HOTENDS] = { false };
  200. #endif
  201. #if ENABLED(ADC_KEYPAD)
  202. uint32_t Temperature::current_ADCKey_raw = 0;
  203. uint8_t Temperature::ADCKey_count = 0;
  204. #endif
  205. #if ENABLED(PID_EXTRUSION_SCALING)
  206. int16_t Temperature::lpq_len; // Initialized in configuration_store
  207. #endif
  208. #if HAS_PID_HEATING
  209. /**
  210. * PID Autotuning (M303)
  211. *
  212. * Alternately heat and cool the nozzle, observing its behavior to
  213. * determine the best PID values to achieve a stable temperature.
  214. */
  215. void Temperature::PID_autotune(const float &target, const int8_t hotend, const int8_t ncycles, const bool set_result/*=false*/) {
  216. float current = 0.0;
  217. int cycles = 0;
  218. bool heating = true;
  219. millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
  220. long t_high = 0, t_low = 0;
  221. long bias, d;
  222. float Ku, Tu,
  223. workKp = 0, workKi = 0, workKd = 0,
  224. max = 0, min = 10000;
  225. #define HAS_TP_BED (ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED))
  226. #if HAS_TP_BED && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  227. #define TV(B,H) (hotend < 0 ? (B) : (H))
  228. #elif HAS_TP_BED
  229. #define TV(B,H) (B)
  230. #else
  231. #define TV(B,H) (H)
  232. #endif
  233. #if WATCH_THE_BED || WATCH_HOTENDS
  234. const uint16_t watch_temp_period = TV(WATCH_BED_TEMP_PERIOD, WATCH_TEMP_PERIOD);
  235. const uint8_t watch_temp_increase = TV(WATCH_BED_TEMP_INCREASE, WATCH_TEMP_INCREASE);
  236. const float watch_temp_target = target - float(watch_temp_increase + TV(TEMP_BED_HYSTERESIS, TEMP_HYSTERESIS) + 1);
  237. millis_t temp_change_ms = next_temp_ms + watch_temp_period * 1000UL;
  238. float next_watch_temp = 0.0;
  239. bool heated = false;
  240. #endif
  241. #if HAS_AUTO_FAN
  242. next_auto_fan_check_ms = next_temp_ms + 2500UL;
  243. #endif
  244. #if ENABLED(PIDTEMP)
  245. #define _TOP_HOTEND HOTENDS - 1
  246. #else
  247. #define _TOP_HOTEND -1
  248. #endif
  249. #if ENABLED(PIDTEMPBED)
  250. #define _BOT_HOTEND -1
  251. #else
  252. #define _BOT_HOTEND 0
  253. #endif
  254. if (!WITHIN(hotend, _BOT_HOTEND, _TOP_HOTEND)) {
  255. SERIAL_ECHOLNPGM(MSG_PID_BAD_EXTRUDER_NUM);
  256. return;
  257. }
  258. SERIAL_ECHOLNPGM(MSG_PID_AUTOTUNE_START);
  259. disable_all_heaters(); // switch off all heaters.
  260. #if HAS_PID_FOR_BOTH
  261. if (hotend < 0)
  262. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  263. else
  264. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  265. #elif ENABLED(PIDTEMP)
  266. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  267. #else
  268. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  269. #endif
  270. wait_for_heatup = true; // Can be interrupted with M108
  271. // PID Tuning loop
  272. while (wait_for_heatup) {
  273. const millis_t ms = millis();
  274. if (temp_meas_ready) { // temp sample ready
  275. updateTemperaturesFromRawValues();
  276. // Get the current temperature and constrain it
  277. current =
  278. #if HAS_PID_FOR_BOTH
  279. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  280. #elif ENABLED(PIDTEMP)
  281. current_temperature[hotend]
  282. #else
  283. current_temperature_bed
  284. #endif
  285. ;
  286. NOLESS(max, current);
  287. NOMORE(min, current);
  288. #if HAS_AUTO_FAN
  289. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  290. checkExtruderAutoFans();
  291. next_auto_fan_check_ms = ms + 2500UL;
  292. }
  293. #endif
  294. if (heating && current > target) {
  295. if (ELAPSED(ms, t2 + 5000UL)) {
  296. heating = false;
  297. #if HAS_PID_FOR_BOTH
  298. if (hotend < 0)
  299. soft_pwm_amount_bed = (bias - d) >> 1;
  300. else
  301. soft_pwm_amount[hotend] = (bias - d) >> 1;
  302. #elif ENABLED(PIDTEMP)
  303. soft_pwm_amount[hotend] = (bias - d) >> 1;
  304. #elif ENABLED(PIDTEMPBED)
  305. soft_pwm_amount_bed = (bias - d) >> 1;
  306. #endif
  307. t1 = ms;
  308. t_high = t1 - t2;
  309. max = target;
  310. }
  311. }
  312. if (!heating && current < target) {
  313. if (ELAPSED(ms, t1 + 5000UL)) {
  314. heating = true;
  315. t2 = ms;
  316. t_low = t2 - t1;
  317. if (cycles > 0) {
  318. long max_pow =
  319. #if HAS_PID_FOR_BOTH
  320. hotend < 0 ? MAX_BED_POWER : PID_MAX
  321. #elif ENABLED(PIDTEMP)
  322. PID_MAX
  323. #else
  324. MAX_BED_POWER
  325. #endif
  326. ;
  327. bias += (d * (t_high - t_low)) / (t_low + t_high);
  328. bias = constrain(bias, 20, max_pow - 20);
  329. d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias;
  330. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  331. SERIAL_PROTOCOLPAIR(MSG_D, d);
  332. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  333. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  334. if (cycles > 2) {
  335. Ku = (4.0 * d) / (M_PI * (max - min) * 0.5);
  336. Tu = ((float)(t_low + t_high) * 0.001);
  337. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  338. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  339. workKp = 0.6 * Ku;
  340. workKi = 2 * workKp / Tu;
  341. workKd = workKp * Tu * 0.125;
  342. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  343. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  344. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  345. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  346. /**
  347. workKp = 0.33*Ku;
  348. workKi = workKp/Tu;
  349. workKd = workKp*Tu/3;
  350. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  351. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  352. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  353. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  354. workKp = 0.2*Ku;
  355. workKi = 2*workKp/Tu;
  356. workKd = workKp*Tu/3;
  357. SERIAL_PROTOCOLLNPGM(" No overshoot");
  358. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  359. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  360. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  361. */
  362. }
  363. }
  364. #if HAS_PID_FOR_BOTH
  365. if (hotend < 0)
  366. soft_pwm_amount_bed = (bias + d) >> 1;
  367. else
  368. soft_pwm_amount[hotend] = (bias + d) >> 1;
  369. #elif ENABLED(PIDTEMP)
  370. soft_pwm_amount[hotend] = (bias + d) >> 1;
  371. #else
  372. soft_pwm_amount_bed = (bias + d) >> 1;
  373. #endif
  374. cycles++;
  375. min = target;
  376. }
  377. }
  378. }
  379. // Did the temperature overshoot very far?
  380. #ifndef MAX_OVERSHOOT_PID_AUTOTUNE
  381. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  382. #endif
  383. if (current > target + MAX_OVERSHOOT_PID_AUTOTUNE) {
  384. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  385. break;
  386. }
  387. // Report heater states every 2 seconds
  388. if (ELAPSED(ms, next_temp_ms)) {
  389. #if HAS_TEMP_SENSOR
  390. print_heaterstates();
  391. SERIAL_EOL();
  392. #endif
  393. next_temp_ms = ms + 2000UL;
  394. // Make sure heating is actually working
  395. #if WATCH_THE_BED || WATCH_HOTENDS
  396. if (
  397. #if WATCH_THE_BED && WATCH_HOTENDS
  398. true
  399. #elif WATCH_THE_BED
  400. hotend < 0
  401. #else
  402. hotend >= 0
  403. #endif
  404. ) {
  405. if (!heated) { // If not yet reached target...
  406. if (current > next_watch_temp) { // Over the watch temp?
  407. next_watch_temp = current + watch_temp_increase; // - set the next temp to watch for
  408. temp_change_ms = ms + watch_temp_period * 1000UL; // - move the expiration timer up
  409. if (current > watch_temp_target) heated = true; // - Flag if target temperature reached
  410. }
  411. else if (ELAPSED(ms, temp_change_ms)) // Watch timer expired
  412. _temp_error(hotend, PSTR(MSG_T_HEATING_FAILED), TEMP_ERR_PSTR(MSG_HEATING_FAILED_LCD, hotend));
  413. }
  414. else if (current < target - (MAX_OVERSHOOT_PID_AUTOTUNE)) // Heated, then temperature fell too far?
  415. _temp_error(hotend, PSTR(MSG_T_THERMAL_RUNAWAY), TEMP_ERR_PSTR(MSG_THERMAL_RUNAWAY, hotend));
  416. }
  417. #endif
  418. } // every 2 seconds
  419. // Timeout after MAX_CYCLE_TIME_PID_AUTOTUNE minutes since the last undershoot/overshoot cycle
  420. #ifndef MAX_CYCLE_TIME_PID_AUTOTUNE
  421. #define MAX_CYCLE_TIME_PID_AUTOTUNE 20L
  422. #endif
  423. if (((ms - t1) + (ms - t2)) > (MAX_CYCLE_TIME_PID_AUTOTUNE * 60L * 1000L)) {
  424. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  425. break;
  426. }
  427. if (cycles > ncycles) {
  428. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  429. #if HAS_PID_FOR_BOTH
  430. const char* estring = hotend < 0 ? "bed" : "";
  431. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL();
  432. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL();
  433. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL();
  434. #elif ENABLED(PIDTEMP)
  435. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL();
  436. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL();
  437. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL();
  438. #else
  439. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL();
  440. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL();
  441. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL();
  442. #endif
  443. #define _SET_BED_PID() do { \
  444. bedKp = workKp; \
  445. bedKi = scalePID_i(workKi); \
  446. bedKd = scalePID_d(workKd); \
  447. }while(0)
  448. #define _SET_EXTRUDER_PID() do { \
  449. PID_PARAM(Kp, hotend) = workKp; \
  450. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  451. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  452. updatePID(); }while(0)
  453. // Use the result? (As with "M303 U1")
  454. if (set_result) {
  455. #if HAS_PID_FOR_BOTH
  456. if (hotend < 0)
  457. _SET_BED_PID();
  458. else
  459. _SET_EXTRUDER_PID();
  460. #elif ENABLED(PIDTEMP)
  461. _SET_EXTRUDER_PID();
  462. #else
  463. _SET_BED_PID();
  464. #endif
  465. }
  466. return;
  467. }
  468. lcd_update();
  469. }
  470. disable_all_heaters();
  471. }
  472. #endif // HAS_PID_HEATING
  473. /**
  474. * Class and Instance Methods
  475. */
  476. Temperature::Temperature() { }
  477. int Temperature::getHeaterPower(const int heater) {
  478. return (
  479. #if HAS_HEATED_BED
  480. heater < 0 ? soft_pwm_amount_bed :
  481. #endif
  482. soft_pwm_amount[heater]
  483. );
  484. }
  485. #if HAS_AUTO_FAN
  486. void Temperature::checkExtruderAutoFans() {
  487. static const pin_t fanPin[] PROGMEM = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN, CHAMBER_AUTO_FAN_PIN };
  488. static const uint8_t fanBit[] PROGMEM = {
  489. 0,
  490. AUTO_1_IS_0 ? 0 : 1,
  491. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  492. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3,
  493. AUTO_4_IS_0 ? 0 : AUTO_4_IS_1 ? 1 : AUTO_4_IS_2 ? 2 : AUTO_4_IS_3 ? 3 : 4,
  494. 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 : 5
  495. };
  496. uint8_t fanState = 0;
  497. HOTEND_LOOP()
  498. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  499. SBI(fanState, pgm_read_byte(&fanBit[e]));
  500. #if HAS_TEMP_CHAMBER
  501. if (current_temperature_chamber > EXTRUDER_AUTO_FAN_TEMPERATURE)
  502. SBI(fanState, pgm_read_byte(&fanBit[5]));
  503. #endif
  504. uint8_t fanDone = 0;
  505. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  506. #ifdef ARDUINO
  507. pin_t pin = pgm_read_byte(&fanPin[f]);
  508. #else
  509. pin_t pin = fanPin[f];
  510. #endif
  511. const uint8_t bit = pgm_read_byte(&fanBit[f]);
  512. if (pin >= 0 && !TEST(fanDone, bit)) {
  513. uint8_t newFanSpeed = TEST(fanState, bit) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  514. #if ENABLED(AUTO_POWER_E_FANS)
  515. autofan_speed[f] = newFanSpeed;
  516. #endif
  517. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  518. digitalWrite(pin, newFanSpeed);
  519. analogWrite(pin, newFanSpeed);
  520. SBI(fanDone, bit);
  521. }
  522. }
  523. }
  524. #endif // HAS_AUTO_FAN
  525. //
  526. // Temperature Error Handlers
  527. //
  528. void Temperature::_temp_error(const int8_t e, const char * const serial_msg, const char * const lcd_msg) {
  529. static bool killed = false;
  530. if (IsRunning()) {
  531. SERIAL_ERROR_START();
  532. serialprintPGM(serial_msg);
  533. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  534. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  535. }
  536. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  537. if (!killed) {
  538. Running = false;
  539. killed = true;
  540. kill(lcd_msg);
  541. }
  542. else
  543. disable_all_heaters(); // paranoia
  544. #endif
  545. }
  546. void Temperature::max_temp_error(const int8_t e) {
  547. _temp_error(e, PSTR(MSG_T_MAXTEMP), TEMP_ERR_PSTR(MSG_ERR_MAXTEMP, e));
  548. }
  549. void Temperature::min_temp_error(const int8_t e) {
  550. _temp_error(e, PSTR(MSG_T_MINTEMP), TEMP_ERR_PSTR(MSG_ERR_MINTEMP, e));
  551. }
  552. float Temperature::get_pid_output(const int8_t e) {
  553. #if HOTENDS == 1
  554. UNUSED(e);
  555. #define _HOTEND_TEST true
  556. #else
  557. #define _HOTEND_TEST e == active_extruder
  558. #endif
  559. float pid_output;
  560. #if ENABLED(PIDTEMP)
  561. #if DISABLED(PID_OPENLOOP)
  562. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  563. dTerm[HOTEND_INDEX] = PID_K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + PID_K1 * dTerm[HOTEND_INDEX];
  564. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  565. #if HEATER_IDLE_HANDLER
  566. if (heater_idle_timeout_exceeded[HOTEND_INDEX]) {
  567. pid_output = 0;
  568. pid_reset[HOTEND_INDEX] = true;
  569. }
  570. else
  571. #endif
  572. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  573. pid_output = BANG_MAX;
  574. pid_reset[HOTEND_INDEX] = true;
  575. }
  576. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0
  577. #if HEATER_IDLE_HANDLER
  578. || heater_idle_timeout_exceeded[HOTEND_INDEX]
  579. #endif
  580. ) {
  581. pid_output = 0;
  582. pid_reset[HOTEND_INDEX] = true;
  583. }
  584. else {
  585. if (pid_reset[HOTEND_INDEX]) {
  586. temp_iState[HOTEND_INDEX] = 0.0;
  587. pid_reset[HOTEND_INDEX] = false;
  588. }
  589. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  590. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  591. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  592. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  593. #if ENABLED(PID_EXTRUSION_SCALING)
  594. cTerm[HOTEND_INDEX] = 0;
  595. if (_HOTEND_TEST) {
  596. const long e_position = stepper.position(E_AXIS);
  597. if (e_position > last_e_position) {
  598. lpq[lpq_ptr] = e_position - last_e_position;
  599. last_e_position = e_position;
  600. }
  601. else
  602. lpq[lpq_ptr] = 0;
  603. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  604. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  605. pid_output += cTerm[HOTEND_INDEX];
  606. }
  607. #endif // PID_EXTRUSION_SCALING
  608. if (pid_output > PID_MAX) {
  609. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  610. pid_output = PID_MAX;
  611. }
  612. else if (pid_output < 0) {
  613. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  614. pid_output = 0;
  615. }
  616. }
  617. #else
  618. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  619. #endif // PID_OPENLOOP
  620. #if ENABLED(PID_DEBUG)
  621. SERIAL_ECHO_START();
  622. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  623. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  624. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  625. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  626. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  627. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  628. #if ENABLED(PID_EXTRUSION_SCALING)
  629. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  630. #endif
  631. SERIAL_EOL();
  632. #endif // PID_DEBUG
  633. #else /* PID off */
  634. #if HEATER_IDLE_HANDLER
  635. if (heater_idle_timeout_exceeded[HOTEND_INDEX])
  636. pid_output = 0;
  637. else
  638. #endif
  639. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  640. #endif
  641. return pid_output;
  642. }
  643. #if ENABLED(PIDTEMPBED)
  644. float Temperature::get_pid_output_bed() {
  645. float pid_output;
  646. #if DISABLED(PID_OPENLOOP)
  647. pid_error_bed = target_temperature_bed - current_temperature_bed;
  648. pTerm_bed = bedKp * pid_error_bed;
  649. temp_iState_bed += pid_error_bed;
  650. iTerm_bed = bedKi * temp_iState_bed;
  651. dTerm_bed = PID_K2 * bedKd * (current_temperature_bed - temp_dState_bed) + PID_K1 * dTerm_bed;
  652. temp_dState_bed = current_temperature_bed;
  653. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  654. if (pid_output > MAX_BED_POWER) {
  655. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  656. pid_output = MAX_BED_POWER;
  657. }
  658. else if (pid_output < 0) {
  659. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  660. pid_output = 0;
  661. }
  662. #else
  663. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  664. #endif // PID_OPENLOOP
  665. #if ENABLED(PID_BED_DEBUG)
  666. SERIAL_ECHO_START();
  667. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  668. SERIAL_ECHOPGM(": Input ");
  669. SERIAL_ECHO(current_temperature_bed);
  670. SERIAL_ECHOPGM(" Output ");
  671. SERIAL_ECHO(pid_output);
  672. SERIAL_ECHOPGM(" pTerm ");
  673. SERIAL_ECHO(pTerm_bed);
  674. SERIAL_ECHOPGM(" iTerm ");
  675. SERIAL_ECHO(iTerm_bed);
  676. SERIAL_ECHOPGM(" dTerm ");
  677. SERIAL_ECHOLN(dTerm_bed);
  678. #endif // PID_BED_DEBUG
  679. return pid_output;
  680. }
  681. #endif // PIDTEMPBED
  682. /**
  683. * Manage heating activities for extruder hot-ends and a heated bed
  684. * - Acquire updated temperature readings
  685. * - Also resets the watchdog timer
  686. * - Invoke thermal runaway protection
  687. * - Manage extruder auto-fan
  688. * - Apply filament width to the extrusion rate (may move)
  689. * - Update the heated bed PID output value
  690. */
  691. void Temperature::manage_heater() {
  692. #if EARLY_WATCHDOG
  693. // If thermal manager is still not running, make sure to at least reset the watchdog!
  694. if (!inited) {
  695. watchdog_reset();
  696. return;
  697. }
  698. #endif
  699. #if ENABLED(PROBING_HEATERS_OFF) && ENABLED(BED_LIMIT_SWITCHING)
  700. static bool last_pause_state;
  701. #endif
  702. #if ENABLED(EMERGENCY_PARSER)
  703. if (emergency_parser.killed_by_M112) kill(PSTR(MSG_KILLED));
  704. #endif
  705. if (!temp_meas_ready) return;
  706. updateTemperaturesFromRawValues(); // also resets the watchdog
  707. #if ENABLED(HEATER_0_USES_MAX6675)
  708. if (current_temperature[0] > MIN(HEATER_0_MAXTEMP, MAX6675_TMAX - 1.0)) max_temp_error(0);
  709. if (current_temperature[0] < MAX(HEATER_0_MINTEMP, MAX6675_TMIN + .01)) min_temp_error(0);
  710. #endif
  711. #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN || HEATER_IDLE_HANDLER
  712. millis_t ms = millis();
  713. #endif
  714. HOTEND_LOOP() {
  715. #if HEATER_IDLE_HANDLER
  716. if (!heater_idle_timeout_exceeded[e] && heater_idle_timeout_ms[e] && ELAPSED(ms, heater_idle_timeout_ms[e]))
  717. heater_idle_timeout_exceeded[e] = true;
  718. #endif
  719. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  720. // Check for thermal runaway
  721. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  722. #endif
  723. soft_pwm_amount[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)get_pid_output(e) >> 1 : 0;
  724. #if WATCH_HOTENDS
  725. // Make sure temperature is increasing
  726. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) { // Time to check this extruder?
  727. if (degHotend(e) < watch_target_temp[e]) // Failed to increase enough?
  728. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), TEMP_ERR_PSTR(MSG_HEATING_FAILED_LCD, e));
  729. else // Start again if the target is still far off
  730. start_watching_heater(e);
  731. }
  732. #endif
  733. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  734. // Make sure measured temperatures are close together
  735. if (ABS(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  736. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  737. #endif
  738. } // HOTEND_LOOP
  739. #if HAS_AUTO_FAN
  740. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  741. checkExtruderAutoFans();
  742. next_auto_fan_check_ms = ms + 2500UL;
  743. }
  744. #endif
  745. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  746. /**
  747. * Filament Width Sensor dynamically sets the volumetric multiplier
  748. * based on a delayed measurement of the filament diameter.
  749. */
  750. if (filament_sensor) {
  751. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  752. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  753. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  754. planner.calculate_volumetric_for_width_sensor(measurement_delay[meas_shift_index]);
  755. }
  756. #endif // FILAMENT_WIDTH_SENSOR
  757. #if HAS_HEATED_BED
  758. #if WATCH_THE_BED
  759. // Make sure temperature is increasing
  760. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) { // Time to check the bed?
  761. if (degBed() < watch_target_bed_temp) // Failed to increase enough?
  762. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), TEMP_ERR_PSTR(MSG_HEATING_FAILED_LCD, -1));
  763. else // Start again if the target is still far off
  764. start_watching_bed();
  765. }
  766. #endif // WATCH_THE_BED
  767. #if DISABLED(PIDTEMPBED)
  768. if (PENDING(ms, next_bed_check_ms)
  769. #if ENABLED(PROBING_HEATERS_OFF) && ENABLED(BED_LIMIT_SWITCHING)
  770. && paused == last_pause_state
  771. #endif
  772. ) return;
  773. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  774. #if ENABLED(PROBING_HEATERS_OFF) && ENABLED(BED_LIMIT_SWITCHING)
  775. last_pause_state = paused;
  776. #endif
  777. #endif
  778. #if HEATER_IDLE_HANDLER
  779. if (!bed_idle_timeout_exceeded && bed_idle_timeout_ms && ELAPSED(ms, bed_idle_timeout_ms))
  780. bed_idle_timeout_exceeded = true;
  781. #endif
  782. #if HAS_THERMALLY_PROTECTED_BED
  783. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, -1, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS);
  784. #endif
  785. #if HEATER_IDLE_HANDLER
  786. if (bed_idle_timeout_exceeded) {
  787. soft_pwm_amount_bed = 0;
  788. #if DISABLED(PIDTEMPBED)
  789. WRITE_HEATER_BED(LOW);
  790. #endif
  791. }
  792. else
  793. #endif
  794. {
  795. #if ENABLED(PIDTEMPBED)
  796. soft_pwm_amount_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  797. #else
  798. // Check if temperature is within the correct band
  799. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  800. #if ENABLED(BED_LIMIT_SWITCHING)
  801. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  802. soft_pwm_amount_bed = 0;
  803. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  804. soft_pwm_amount_bed = MAX_BED_POWER >> 1;
  805. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  806. soft_pwm_amount_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  807. #endif
  808. }
  809. else {
  810. soft_pwm_amount_bed = 0;
  811. WRITE_HEATER_BED(LOW);
  812. }
  813. #endif
  814. }
  815. #endif // HAS_HEATED_BED
  816. }
  817. #define TEMP_AD595(RAW) ((RAW) * 5.0 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET)
  818. #define TEMP_AD8495(RAW) ((RAW) * 6.6 * 100.0 / 1024.0 / (OVERSAMPLENR) * (TEMP_SENSOR_AD8495_GAIN) + TEMP_SENSOR_AD8495_OFFSET)
  819. /**
  820. * Bisect search for the range of the 'raw' value, then interpolate
  821. * proportionally between the under and over values.
  822. */
  823. #define SCAN_THERMISTOR_TABLE(TBL,LEN) do{ \
  824. uint8_t l = 0, r = LEN, m; \
  825. for (;;) { \
  826. m = (l + r) >> 1; \
  827. if (m == l || m == r) return (short)pgm_read_word(&TBL[LEN-1][1]); \
  828. short v00 = pgm_read_word(&TBL[m-1][0]), \
  829. v10 = pgm_read_word(&TBL[m-0][0]); \
  830. if (raw < v00) r = m; \
  831. else if (raw > v10) l = m; \
  832. else { \
  833. const short v01 = (short)pgm_read_word(&TBL[m-1][1]), \
  834. v11 = (short)pgm_read_word(&TBL[m-0][1]); \
  835. return v01 + (raw - v00) * float(v11 - v01) / float(v10 - v00); \
  836. } \
  837. } \
  838. }while(0)
  839. // Derived from RepRap FiveD extruder::getTemperature()
  840. // For hot end temperature measurement.
  841. float Temperature::analog2temp(const int raw, const uint8_t e) {
  842. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  843. if (e > HOTENDS)
  844. #else
  845. if (e >= HOTENDS)
  846. #endif
  847. {
  848. SERIAL_ERROR_START();
  849. SERIAL_ERROR((int)e);
  850. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  851. kill(PSTR(MSG_KILLED));
  852. return 0.0;
  853. }
  854. switch (e) {
  855. case 0:
  856. #if ENABLED(HEATER_0_USES_MAX6675)
  857. return raw * 0.25;
  858. #elif ENABLED(HEATER_0_USES_AD595)
  859. return TEMP_AD595(raw);
  860. #elif ENABLED(HEATER_0_USES_AD8495)
  861. return TEMP_AD8495(raw);
  862. #else
  863. break;
  864. #endif
  865. case 1:
  866. #if ENABLED(HEATER_1_USES_AD595)
  867. return TEMP_AD595(raw);
  868. #elif ENABLED(HEATER_1_USES_AD8495)
  869. return TEMP_AD8495(raw);
  870. #else
  871. break;
  872. #endif
  873. case 2:
  874. #if ENABLED(HEATER_2_USES_AD595)
  875. return TEMP_AD595(raw);
  876. #elif ENABLED(HEATER_2_USES_AD8495)
  877. return TEMP_AD8495(raw);
  878. #else
  879. break;
  880. #endif
  881. case 3:
  882. #if ENABLED(HEATER_3_USES_AD595)
  883. return TEMP_AD595(raw);
  884. #elif ENABLED(HEATER_3_USES_AD8495)
  885. return TEMP_AD8495(raw);
  886. #else
  887. break;
  888. #endif
  889. case 4:
  890. #if ENABLED(HEATER_4_USES_AD595)
  891. return TEMP_AD595(raw);
  892. #elif ENABLED(HEATER_4_USES_AD8495)
  893. return TEMP_AD8495(raw);
  894. #else
  895. break;
  896. #endif
  897. default: break;
  898. }
  899. #if HOTEND_USES_THERMISTOR
  900. // Thermistor with conversion table?
  901. const short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  902. SCAN_THERMISTOR_TABLE((*tt), heater_ttbllen_map[e]);
  903. #endif
  904. return 0;
  905. }
  906. #if HAS_HEATED_BED
  907. // Derived from RepRap FiveD extruder::getTemperature()
  908. // For bed temperature measurement.
  909. float Temperature::analog2tempBed(const int raw) {
  910. #if ENABLED(HEATER_BED_USES_THERMISTOR)
  911. SCAN_THERMISTOR_TABLE(BEDTEMPTABLE, BEDTEMPTABLE_LEN);
  912. #elif ENABLED(HEATER_BED_USES_AD595)
  913. return TEMP_AD595(raw);
  914. #elif ENABLED(HEATER_BED_USES_AD8495)
  915. return TEMP_AD8495(raw);
  916. #else
  917. return 0;
  918. #endif
  919. }
  920. #endif // HAS_HEATED_BED
  921. #if HAS_TEMP_CHAMBER
  922. // Derived from RepRap FiveD extruder::getTemperature()
  923. // For chamber temperature measurement.
  924. float Temperature::analog2tempChamber(const int raw) {
  925. #if ENABLED(HEATER_CHAMBER_USES_THERMISTOR)
  926. SCAN_THERMISTOR_TABLE(CHAMBERTEMPTABLE, CHAMBERTEMPTABLE_LEN);
  927. #elif ENABLED(HEATER_CHAMBER_USES_AD595)
  928. return TEMP_AD595(raw);
  929. #elif ENABLED(HEATER_CHAMBER_USES_AD8495)
  930. return TEMP_AD8495(raw);
  931. #else
  932. return 0;
  933. #endif
  934. }
  935. #endif // HAS_TEMP_CHAMBER
  936. /**
  937. * Get the raw values into the actual temperatures.
  938. * The raw values are created in interrupt context,
  939. * and this function is called from normal context
  940. * as it would block the stepper routine.
  941. */
  942. void Temperature::updateTemperaturesFromRawValues() {
  943. #if ENABLED(HEATER_0_USES_MAX6675)
  944. current_temperature_raw[0] = read_max6675();
  945. #endif
  946. HOTEND_LOOP() current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  947. #if HAS_HEATED_BED
  948. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  949. #endif
  950. #if HAS_TEMP_CHAMBER
  951. current_temperature_chamber = Temperature::analog2tempChamber(current_temperature_chamber_raw);
  952. #endif
  953. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  954. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  955. #endif
  956. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  957. filament_width_meas = analog2widthFil();
  958. #endif
  959. #if ENABLED(USE_WATCHDOG)
  960. // Reset the watchdog after we know we have a temperature measurement.
  961. watchdog_reset();
  962. #endif
  963. temp_meas_ready = false;
  964. }
  965. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  966. // Convert raw Filament Width to millimeters
  967. float Temperature::analog2widthFil() {
  968. return current_raw_filwidth * 5.0 * (1.0 / 16383.0);
  969. }
  970. /**
  971. * Convert Filament Width (mm) to a simple ratio
  972. * and reduce to an 8 bit value.
  973. *
  974. * A nominal width of 1.75 and measured width of 1.73
  975. * gives (100 * 1.75 / 1.73) for a ratio of 101 and
  976. * a return value of 1.
  977. */
  978. int8_t Temperature::widthFil_to_size_ratio() {
  979. if (ABS(filament_width_nominal - filament_width_meas) <= FILWIDTH_ERROR_MARGIN)
  980. return int(100.0 * filament_width_nominal / filament_width_meas) - 100;
  981. return 0;
  982. }
  983. #endif
  984. #if ENABLED(HEATER_0_USES_MAX6675)
  985. #ifndef MAX6675_SCK_PIN
  986. #define MAX6675_SCK_PIN SCK_PIN
  987. #endif
  988. #ifndef MAX6675_DO_PIN
  989. #define MAX6675_DO_PIN MISO_PIN
  990. #endif
  991. SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  992. #endif
  993. /**
  994. * Initialize the temperature manager
  995. * The manager is implemented by periodic calls to manage_heater()
  996. */
  997. void Temperature::init() {
  998. #if EARLY_WATCHDOG
  999. // Flag that the thermalManager should be running
  1000. if (inited) return;
  1001. inited = true;
  1002. #endif
  1003. #if MB(RUMBA) && ( \
  1004. ENABLED(HEATER_0_USES_AD595) || ENABLED(HEATER_1_USES_AD595) || ENABLED(HEATER_2_USES_AD595) || ENABLED(HEATER_3_USES_AD595) || ENABLED(HEATER_4_USES_AD595) || ENABLED(HEATER_BED_USES_AD595) || ENABLED(HEATER_CHAMBER_USES_AD595) \
  1005. || ENABLED(HEATER_0_USES_AD8495) || ENABLED(HEATER_1_USES_AD8495) || ENABLED(HEATER_2_USES_AD8495) || ENABLED(HEATER_3_USES_AD8495) || ENABLED(HEATER_4_USES_AD8495) || ENABLED(HEATER_BED_USES_AD8495) || ENABLED(HEATER_CHAMBER_USES_AD8495))
  1006. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  1007. MCUCR = _BV(JTD);
  1008. MCUCR = _BV(JTD);
  1009. #endif
  1010. // Finish init of mult hotend arrays
  1011. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  1012. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  1013. last_e_position = 0;
  1014. #endif
  1015. #if HAS_HEATER_0
  1016. OUT_WRITE(HEATER_0_PIN, HEATER_0_INVERTING);
  1017. #endif
  1018. #if HAS_HEATER_1
  1019. OUT_WRITE(HEATER_1_PIN, HEATER_1_INVERTING);
  1020. #endif
  1021. #if HAS_HEATER_2
  1022. OUT_WRITE(HEATER_2_PIN, HEATER_2_INVERTING);
  1023. #endif
  1024. #if HAS_HEATER_3
  1025. OUT_WRITE(HEATER_3_PIN, HEATER_3_INVERTING);
  1026. #endif
  1027. #if HAS_HEATER_4
  1028. OUT_WRITE(HEATER_3_PIN, HEATER_4_INVERTING);
  1029. #endif
  1030. #if HAS_HEATED_BED
  1031. OUT_WRITE(HEATER_BED_PIN, HEATER_BED_INVERTING);
  1032. #endif
  1033. #if HAS_FAN0
  1034. SET_OUTPUT(FAN_PIN);
  1035. #if ENABLED(FAST_PWM_FAN)
  1036. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1037. #endif
  1038. #endif
  1039. #if HAS_FAN1
  1040. SET_OUTPUT(FAN1_PIN);
  1041. #if ENABLED(FAST_PWM_FAN)
  1042. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1043. #endif
  1044. #endif
  1045. #if HAS_FAN2
  1046. SET_OUTPUT(FAN2_PIN);
  1047. #if ENABLED(FAST_PWM_FAN)
  1048. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1049. #endif
  1050. #endif
  1051. #if ENABLED(HEATER_0_USES_MAX6675)
  1052. OUT_WRITE(SCK_PIN, LOW);
  1053. OUT_WRITE(MOSI_PIN, HIGH);
  1054. SET_INPUT_PULLUP(MISO_PIN);
  1055. max6675_spi.init();
  1056. OUT_WRITE(SS_PIN, HIGH);
  1057. OUT_WRITE(MAX6675_SS, HIGH);
  1058. #endif // HEATER_0_USES_MAX6675
  1059. HAL_adc_init();
  1060. #if HAS_TEMP_ADC_0
  1061. HAL_ANALOG_SELECT(TEMP_0_PIN);
  1062. #endif
  1063. #if HAS_TEMP_ADC_1
  1064. HAL_ANALOG_SELECT(TEMP_1_PIN);
  1065. #endif
  1066. #if HAS_TEMP_ADC_2
  1067. HAL_ANALOG_SELECT(TEMP_2_PIN);
  1068. #endif
  1069. #if HAS_TEMP_ADC_3
  1070. HAL_ANALOG_SELECT(TEMP_3_PIN);
  1071. #endif
  1072. #if HAS_TEMP_ADC_4
  1073. HAL_ANALOG_SELECT(TEMP_4_PIN);
  1074. #endif
  1075. #if HAS_HEATED_BED
  1076. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  1077. #endif
  1078. #if HAS_TEMP_CHAMBER
  1079. HAL_ANALOG_SELECT(TEMP_CHAMBER_PIN);
  1080. #endif
  1081. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1082. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  1083. #endif
  1084. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  1085. ENABLE_TEMPERATURE_INTERRUPT();
  1086. #if HAS_AUTO_FAN_0
  1087. #if E0_AUTO_FAN_PIN == FAN1_PIN
  1088. SET_OUTPUT(E0_AUTO_FAN_PIN);
  1089. #if ENABLED(FAST_PWM_FAN)
  1090. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1091. #endif
  1092. #else
  1093. SET_OUTPUT(E0_AUTO_FAN_PIN);
  1094. #endif
  1095. #endif
  1096. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  1097. #if E1_AUTO_FAN_PIN == FAN1_PIN
  1098. SET_OUTPUT(E1_AUTO_FAN_PIN);
  1099. #if ENABLED(FAST_PWM_FAN)
  1100. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1101. #endif
  1102. #else
  1103. SET_OUTPUT(E1_AUTO_FAN_PIN);
  1104. #endif
  1105. #endif
  1106. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  1107. #if E2_AUTO_FAN_PIN == FAN1_PIN
  1108. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1109. #if ENABLED(FAST_PWM_FAN)
  1110. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1111. #endif
  1112. #else
  1113. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1114. #endif
  1115. #endif
  1116. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  1117. #if E3_AUTO_FAN_PIN == FAN1_PIN
  1118. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1119. #if ENABLED(FAST_PWM_FAN)
  1120. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1121. #endif
  1122. #else
  1123. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1124. #endif
  1125. #endif
  1126. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  1127. #if E4_AUTO_FAN_PIN == FAN1_PIN
  1128. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1129. #if ENABLED(FAST_PWM_FAN)
  1130. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1131. #endif
  1132. #else
  1133. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1134. #endif
  1135. #endif
  1136. #if HAS_AUTO_CHAMBER_FAN && !AUTO_CHAMBER_IS_0 && !AUTO_CHAMBER_IS_1 && !AUTO_CHAMBER_IS_2 && !AUTO_CHAMBER_IS_3 && ! AUTO_CHAMBER_IS_4
  1137. #if CHAMBER_AUTO_FAN_PIN == FAN1_PIN
  1138. SET_OUTPUT(CHAMBER_AUTO_FAN_PIN);
  1139. #if ENABLED(FAST_PWM_FAN)
  1140. setPwmFrequency(CHAMBER_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1141. #endif
  1142. #else
  1143. SET_OUTPUT(CHAMBER_AUTO_FAN_PIN);
  1144. #endif
  1145. #endif
  1146. // Wait for temperature measurement to settle
  1147. delay(250);
  1148. #define TEMP_MIN_ROUTINE(NR) \
  1149. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  1150. while (analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  1151. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1152. minttemp_raw[NR] += OVERSAMPLENR; \
  1153. else \
  1154. minttemp_raw[NR] -= OVERSAMPLENR; \
  1155. }
  1156. #define TEMP_MAX_ROUTINE(NR) \
  1157. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  1158. while (analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  1159. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1160. maxttemp_raw[NR] -= OVERSAMPLENR; \
  1161. else \
  1162. maxttemp_raw[NR] += OVERSAMPLENR; \
  1163. }
  1164. #ifdef HEATER_0_MINTEMP
  1165. TEMP_MIN_ROUTINE(0);
  1166. #endif
  1167. #ifdef HEATER_0_MAXTEMP
  1168. TEMP_MAX_ROUTINE(0);
  1169. #endif
  1170. #if HOTENDS > 1
  1171. #ifdef HEATER_1_MINTEMP
  1172. TEMP_MIN_ROUTINE(1);
  1173. #endif
  1174. #ifdef HEATER_1_MAXTEMP
  1175. TEMP_MAX_ROUTINE(1);
  1176. #endif
  1177. #if HOTENDS > 2
  1178. #ifdef HEATER_2_MINTEMP
  1179. TEMP_MIN_ROUTINE(2);
  1180. #endif
  1181. #ifdef HEATER_2_MAXTEMP
  1182. TEMP_MAX_ROUTINE(2);
  1183. #endif
  1184. #if HOTENDS > 3
  1185. #ifdef HEATER_3_MINTEMP
  1186. TEMP_MIN_ROUTINE(3);
  1187. #endif
  1188. #ifdef HEATER_3_MAXTEMP
  1189. TEMP_MAX_ROUTINE(3);
  1190. #endif
  1191. #if HOTENDS > 4
  1192. #ifdef HEATER_4_MINTEMP
  1193. TEMP_MIN_ROUTINE(4);
  1194. #endif
  1195. #ifdef HEATER_4_MAXTEMP
  1196. TEMP_MAX_ROUTINE(4);
  1197. #endif
  1198. #endif // HOTENDS > 4
  1199. #endif // HOTENDS > 3
  1200. #endif // HOTENDS > 2
  1201. #endif // HOTENDS > 1
  1202. #if HAS_HEATED_BED
  1203. #ifdef BED_MINTEMP
  1204. while (analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1205. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1206. bed_minttemp_raw += OVERSAMPLENR;
  1207. #else
  1208. bed_minttemp_raw -= OVERSAMPLENR;
  1209. #endif
  1210. }
  1211. #endif // BED_MINTEMP
  1212. #ifdef BED_MAXTEMP
  1213. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1214. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1215. bed_maxttemp_raw -= OVERSAMPLENR;
  1216. #else
  1217. bed_maxttemp_raw += OVERSAMPLENR;
  1218. #endif
  1219. }
  1220. #endif // BED_MAXTEMP
  1221. #endif // HAS_HEATED_BED
  1222. #if ENABLED(PROBING_HEATERS_OFF)
  1223. paused = false;
  1224. #endif
  1225. }
  1226. #if ENABLED(FAST_PWM_FAN)
  1227. void Temperature::setPwmFrequency(const pin_t pin, int val) {
  1228. #if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM)
  1229. val &= 0x07;
  1230. switch (digitalPinToTimer(pin)) {
  1231. #ifdef TCCR0A
  1232. #if !AVR_AT90USB1286_FAMILY
  1233. case TIMER0A:
  1234. #endif
  1235. case TIMER0B: //_SET_CS(0, val);
  1236. break;
  1237. #endif
  1238. #ifdef TCCR1A
  1239. case TIMER1A: case TIMER1B: //_SET_CS(1, val);
  1240. break;
  1241. #endif
  1242. #if defined(TCCR2) || defined(TCCR2A)
  1243. #ifdef TCCR2
  1244. case TIMER2:
  1245. #endif
  1246. #ifdef TCCR2A
  1247. case TIMER2A: case TIMER2B:
  1248. #endif
  1249. _SET_CS(2, val); break;
  1250. #endif
  1251. #ifdef TCCR3A
  1252. case TIMER3A: case TIMER3B: case TIMER3C: _SET_CS(3, val); break;
  1253. #endif
  1254. #ifdef TCCR4A
  1255. case TIMER4A: case TIMER4B: case TIMER4C: _SET_CS(4, val); break;
  1256. #endif
  1257. #ifdef TCCR5A
  1258. case TIMER5A: case TIMER5B: case TIMER5C: _SET_CS(5, val); break;
  1259. #endif
  1260. }
  1261. #endif
  1262. }
  1263. #endif // FAST_PWM_FAN
  1264. #if WATCH_HOTENDS
  1265. /**
  1266. * Start Heating Sanity Check for hotends that are below
  1267. * their target temperature by a configurable margin.
  1268. * This is called when the temperature is set. (M104, M109)
  1269. */
  1270. void Temperature::start_watching_heater(const uint8_t e) {
  1271. #if HOTENDS == 1
  1272. UNUSED(e);
  1273. #endif
  1274. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1275. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1276. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1277. }
  1278. else
  1279. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1280. }
  1281. #endif
  1282. #if WATCH_THE_BED
  1283. /**
  1284. * Start Heating Sanity Check for hotends that are below
  1285. * their target temperature by a configurable margin.
  1286. * This is called when the temperature is set. (M140, M190)
  1287. */
  1288. void Temperature::start_watching_bed() {
  1289. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1290. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1291. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1292. }
  1293. else
  1294. watch_bed_next_ms = 0;
  1295. }
  1296. #endif
  1297. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1298. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1299. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1300. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1301. #endif
  1302. #if HAS_THERMALLY_PROTECTED_BED
  1303. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1304. millis_t Temperature::thermal_runaway_bed_timer;
  1305. #endif
  1306. void Temperature::thermal_runaway_protection(Temperature::TRState * const state, millis_t * const timer, const float &current, const float &target, const int8_t heater_id, const uint16_t period_seconds, const uint16_t hysteresis_degc) {
  1307. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1308. /**
  1309. SERIAL_ECHO_START();
  1310. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1311. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1312. SERIAL_ECHOPAIR(" ; State:", *state);
  1313. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1314. SERIAL_ECHOPAIR(" ; Temperature:", current);
  1315. SERIAL_ECHOPAIR(" ; Target Temp:", target);
  1316. if (heater_id >= 0)
  1317. SERIAL_ECHOPAIR(" ; Idle Timeout:", heater_idle_timeout_exceeded[heater_id]);
  1318. else
  1319. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle_timeout_exceeded);
  1320. SERIAL_EOL();
  1321. */
  1322. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1323. #if HEATER_IDLE_HANDLER
  1324. // If the heater idle timeout expires, restart
  1325. if ((heater_id >= 0 && heater_idle_timeout_exceeded[heater_id])
  1326. #if HAS_HEATED_BED
  1327. || (heater_id < 0 && bed_idle_timeout_exceeded)
  1328. #endif
  1329. ) {
  1330. *state = TRInactive;
  1331. tr_target_temperature[heater_index] = 0;
  1332. }
  1333. else
  1334. #endif
  1335. {
  1336. // If the target temperature changes, restart
  1337. if (tr_target_temperature[heater_index] != target) {
  1338. tr_target_temperature[heater_index] = target;
  1339. *state = target > 0 ? TRFirstHeating : TRInactive;
  1340. }
  1341. }
  1342. switch (*state) {
  1343. // Inactive state waits for a target temperature to be set
  1344. case TRInactive: break;
  1345. // When first heating, wait for the temperature to be reached then go to Stable state
  1346. case TRFirstHeating:
  1347. if (current < tr_target_temperature[heater_index]) break;
  1348. *state = TRStable;
  1349. // While the temperature is stable watch for a bad temperature
  1350. case TRStable:
  1351. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1352. *timer = millis() + period_seconds * 1000UL;
  1353. break;
  1354. }
  1355. else if (PENDING(millis(), *timer)) break;
  1356. *state = TRRunaway;
  1357. case TRRunaway:
  1358. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), TEMP_ERR_PSTR(MSG_THERMAL_RUNAWAY, heater_id));
  1359. }
  1360. }
  1361. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1362. void Temperature::disable_all_heaters() {
  1363. #if ENABLED(AUTOTEMP)
  1364. planner.autotemp_enabled = false;
  1365. #endif
  1366. HOTEND_LOOP() setTargetHotend(0, e);
  1367. #if HAS_HEATED_BED
  1368. setTargetBed(0);
  1369. #endif
  1370. // Unpause and reset everything
  1371. #if ENABLED(PROBING_HEATERS_OFF)
  1372. pause(false);
  1373. #endif
  1374. // If all heaters go down then for sure our print job has stopped
  1375. print_job_timer.stop();
  1376. #define DISABLE_HEATER(NR) { \
  1377. setTargetHotend(0, NR); \
  1378. soft_pwm_amount[NR] = 0; \
  1379. WRITE_HEATER_ ##NR (LOW); \
  1380. }
  1381. #if HAS_TEMP_HOTEND
  1382. DISABLE_HEATER(0);
  1383. #if HOTENDS > 1
  1384. DISABLE_HEATER(1);
  1385. #if HOTENDS > 2
  1386. DISABLE_HEATER(2);
  1387. #if HOTENDS > 3
  1388. DISABLE_HEATER(3);
  1389. #if HOTENDS > 4
  1390. DISABLE_HEATER(4);
  1391. #endif // HOTENDS > 4
  1392. #endif // HOTENDS > 3
  1393. #endif // HOTENDS > 2
  1394. #endif // HOTENDS > 1
  1395. #endif
  1396. #if HAS_HEATED_BED
  1397. target_temperature_bed = 0;
  1398. soft_pwm_amount_bed = 0;
  1399. #if HAS_HEATED_BED
  1400. WRITE_HEATER_BED(LOW);
  1401. #endif
  1402. #endif
  1403. }
  1404. #if ENABLED(PROBING_HEATERS_OFF)
  1405. void Temperature::pause(const bool p) {
  1406. if (p != paused) {
  1407. paused = p;
  1408. if (p) {
  1409. HOTEND_LOOP() start_heater_idle_timer(e, 0); // timeout immediately
  1410. #if HAS_HEATED_BED
  1411. start_bed_idle_timer(0); // timeout immediately
  1412. #endif
  1413. }
  1414. else {
  1415. HOTEND_LOOP() reset_heater_idle_timer(e);
  1416. #if HAS_HEATED_BED
  1417. reset_bed_idle_timer();
  1418. #endif
  1419. }
  1420. }
  1421. }
  1422. #endif // PROBING_HEATERS_OFF
  1423. #if ENABLED(HEATER_0_USES_MAX6675)
  1424. #define MAX6675_HEAT_INTERVAL 250u
  1425. #if ENABLED(MAX6675_IS_MAX31855)
  1426. uint32_t max6675_temp = 2000;
  1427. #define MAX6675_ERROR_MASK 7
  1428. #define MAX6675_DISCARD_BITS 18
  1429. #define MAX6675_SPEED_BITS 3 // (_BV(SPR1)) // clock ÷ 64
  1430. #else
  1431. uint16_t max6675_temp = 2000;
  1432. #define MAX6675_ERROR_MASK 4
  1433. #define MAX6675_DISCARD_BITS 3
  1434. #define MAX6675_SPEED_BITS 2 // (_BV(SPR0)) // clock ÷ 16
  1435. #endif
  1436. int Temperature::read_max6675() {
  1437. static millis_t next_max6675_ms = 0;
  1438. millis_t ms = millis();
  1439. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1440. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1441. spiBegin();
  1442. spiInit(MAX6675_SPEED_BITS);
  1443. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1444. DELAY_NS(100); // Ensure 100ns delay
  1445. // Read a big-endian temperature value
  1446. max6675_temp = 0;
  1447. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1448. max6675_temp |= spiRec();
  1449. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1450. }
  1451. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1452. if (max6675_temp & MAX6675_ERROR_MASK) {
  1453. SERIAL_ERROR_START();
  1454. SERIAL_ERRORPGM("Temp measurement error! ");
  1455. #if MAX6675_ERROR_MASK == 7
  1456. SERIAL_ERRORPGM("MAX31855 ");
  1457. if (max6675_temp & 1)
  1458. SERIAL_ERRORLNPGM("Open Circuit");
  1459. else if (max6675_temp & 2)
  1460. SERIAL_ERRORLNPGM("Short to GND");
  1461. else if (max6675_temp & 4)
  1462. SERIAL_ERRORLNPGM("Short to VCC");
  1463. #else
  1464. SERIAL_ERRORLNPGM("MAX6675");
  1465. #endif
  1466. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1467. }
  1468. else
  1469. max6675_temp >>= MAX6675_DISCARD_BITS;
  1470. #if ENABLED(MAX6675_IS_MAX31855)
  1471. // Support negative temperature
  1472. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000;
  1473. #endif
  1474. return (int)max6675_temp;
  1475. }
  1476. #endif // HEATER_0_USES_MAX6675
  1477. /**
  1478. * Get raw temperatures
  1479. */
  1480. void Temperature::set_current_temp_raw() {
  1481. #if HAS_TEMP_ADC_0 && DISABLED(HEATER_0_USES_MAX6675)
  1482. current_temperature_raw[0] = raw_temp_value[0];
  1483. #endif
  1484. #if HAS_TEMP_ADC_1
  1485. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1486. redundant_temperature_raw = raw_temp_value[1];
  1487. #else
  1488. current_temperature_raw[1] = raw_temp_value[1];
  1489. #endif
  1490. #if HAS_TEMP_ADC_2
  1491. current_temperature_raw[2] = raw_temp_value[2];
  1492. #if HAS_TEMP_ADC_3
  1493. current_temperature_raw[3] = raw_temp_value[3];
  1494. #if HAS_TEMP_ADC_4
  1495. current_temperature_raw[4] = raw_temp_value[4];
  1496. #endif
  1497. #endif
  1498. #endif
  1499. #endif
  1500. #if HAS_HEATED_BED
  1501. current_temperature_bed_raw = raw_temp_bed_value;
  1502. #endif
  1503. #if HAS_TEMP_CHAMBER
  1504. current_temperature_chamber_raw = raw_temp_chamber_value;
  1505. #endif
  1506. temp_meas_ready = true;
  1507. }
  1508. /**
  1509. * Timer 0 is shared with millies so don't change the prescaler.
  1510. *
  1511. * On AVR this ISR uses the compare method so it runs at the base
  1512. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1513. * in OCR0B above (128 or halfway between OVFs).
  1514. *
  1515. * - Manage PWM to all the heaters and fan
  1516. * - Prepare or Measure one of the raw ADC sensor values
  1517. * - Check new temperature values for MIN/MAX errors (kill on error)
  1518. * - Step the babysteps value for each axis towards 0
  1519. * - For PINS_DEBUGGING, monitor and report endstop pins
  1520. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1521. * - Call planner.tick to count down its "ignore" time
  1522. */
  1523. HAL_TEMP_TIMER_ISR {
  1524. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  1525. Temperature::isr();
  1526. HAL_timer_isr_epilogue(TEMP_TIMER_NUM);
  1527. }
  1528. void Temperature::isr() {
  1529. static int8_t temp_count = -1;
  1530. static ADCSensorState adc_sensor_state = StartupDelay;
  1531. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1532. // avoid multiple loads of pwm_count
  1533. uint8_t pwm_count_tmp = pwm_count;
  1534. #if ENABLED(ADC_KEYPAD)
  1535. static unsigned int raw_ADCKey_value = 0;
  1536. #endif
  1537. // Static members for each heater
  1538. #if ENABLED(SLOW_PWM_HEATERS)
  1539. static uint8_t slow_pwm_count = 0;
  1540. #define ISR_STATICS(n) \
  1541. static uint8_t soft_pwm_count_ ## n, \
  1542. state_heater_ ## n = 0, \
  1543. state_timer_heater_ ## n = 0
  1544. #else
  1545. #define ISR_STATICS(n) static uint8_t soft_pwm_count_ ## n = 0
  1546. #endif
  1547. // Statics per heater
  1548. ISR_STATICS(0);
  1549. #if HOTENDS > 1
  1550. ISR_STATICS(1);
  1551. #if HOTENDS > 2
  1552. ISR_STATICS(2);
  1553. #if HOTENDS > 3
  1554. ISR_STATICS(3);
  1555. #if HOTENDS > 4
  1556. ISR_STATICS(4);
  1557. #endif // HOTENDS > 4
  1558. #endif // HOTENDS > 3
  1559. #endif // HOTENDS > 2
  1560. #endif // HOTENDS > 1
  1561. #if HAS_HEATED_BED
  1562. ISR_STATICS(BED);
  1563. #endif
  1564. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1565. static unsigned long raw_filwidth_value = 0;
  1566. #endif
  1567. #if DISABLED(SLOW_PWM_HEATERS)
  1568. constexpr uint8_t pwm_mask =
  1569. #if ENABLED(SOFT_PWM_DITHER)
  1570. _BV(SOFT_PWM_SCALE) - 1
  1571. #else
  1572. 0
  1573. #endif
  1574. ;
  1575. /**
  1576. * Standard PWM modulation
  1577. */
  1578. if (pwm_count_tmp >= 127) {
  1579. pwm_count_tmp -= 127;
  1580. soft_pwm_count_0 = (soft_pwm_count_0 & pwm_mask) + soft_pwm_amount[0];
  1581. WRITE_HEATER_0(soft_pwm_count_0 > pwm_mask ? HIGH : LOW);
  1582. #if HOTENDS > 1
  1583. soft_pwm_count_1 = (soft_pwm_count_1 & pwm_mask) + soft_pwm_amount[1];
  1584. WRITE_HEATER_1(soft_pwm_count_1 > pwm_mask ? HIGH : LOW);
  1585. #if HOTENDS > 2
  1586. soft_pwm_count_2 = (soft_pwm_count_2 & pwm_mask) + soft_pwm_amount[2];
  1587. WRITE_HEATER_2(soft_pwm_count_2 > pwm_mask ? HIGH : LOW);
  1588. #if HOTENDS > 3
  1589. soft_pwm_count_3 = (soft_pwm_count_3 & pwm_mask) + soft_pwm_amount[3];
  1590. WRITE_HEATER_3(soft_pwm_count_3 > pwm_mask ? HIGH : LOW);
  1591. #if HOTENDS > 4
  1592. soft_pwm_count_4 = (soft_pwm_count_4 & pwm_mask) + soft_pwm_amount[4];
  1593. WRITE_HEATER_4(soft_pwm_count_4 > pwm_mask ? HIGH : LOW);
  1594. #endif // HOTENDS > 4
  1595. #endif // HOTENDS > 3
  1596. #endif // HOTENDS > 2
  1597. #endif // HOTENDS > 1
  1598. #if HAS_HEATED_BED
  1599. soft_pwm_count_BED = (soft_pwm_count_BED & pwm_mask) + soft_pwm_amount_bed;
  1600. WRITE_HEATER_BED(soft_pwm_count_BED > pwm_mask ? HIGH : LOW);
  1601. #endif
  1602. #if ENABLED(FAN_SOFT_PWM)
  1603. #if HAS_FAN0
  1604. soft_pwm_count_fan[0] = (soft_pwm_count_fan[0] & pwm_mask) + (soft_pwm_amount_fan[0] >> 1);
  1605. WRITE_FAN(soft_pwm_count_fan[0] > pwm_mask ? HIGH : LOW);
  1606. #endif
  1607. #if HAS_FAN1
  1608. soft_pwm_count_fan[1] = (soft_pwm_count_fan[1] & pwm_mask) + (soft_pwm_amount_fan[1] >> 1);
  1609. WRITE_FAN1(soft_pwm_count_fan[1] > pwm_mask ? HIGH : LOW);
  1610. #endif
  1611. #if HAS_FAN2
  1612. soft_pwm_count_fan[2] = (soft_pwm_count_fan[2] & pwm_mask) + (soft_pwm_amount_fan[2] >> 1);
  1613. WRITE_FAN2(soft_pwm_count_fan[2] > pwm_mask ? HIGH : LOW);
  1614. #endif
  1615. #endif
  1616. }
  1617. else {
  1618. if (soft_pwm_count_0 <= pwm_count_tmp) WRITE_HEATER_0(LOW);
  1619. #if HOTENDS > 1
  1620. if (soft_pwm_count_1 <= pwm_count_tmp) WRITE_HEATER_1(LOW);
  1621. #if HOTENDS > 2
  1622. if (soft_pwm_count_2 <= pwm_count_tmp) WRITE_HEATER_2(LOW);
  1623. #if HOTENDS > 3
  1624. if (soft_pwm_count_3 <= pwm_count_tmp) WRITE_HEATER_3(LOW);
  1625. #if HOTENDS > 4
  1626. if (soft_pwm_count_4 <= pwm_count_tmp) WRITE_HEATER_4(LOW);
  1627. #endif // HOTENDS > 4
  1628. #endif // HOTENDS > 3
  1629. #endif // HOTENDS > 2
  1630. #endif // HOTENDS > 1
  1631. #if HAS_HEATED_BED
  1632. if (soft_pwm_count_BED <= pwm_count_tmp) WRITE_HEATER_BED(LOW);
  1633. #endif
  1634. #if ENABLED(FAN_SOFT_PWM)
  1635. #if HAS_FAN0
  1636. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1637. #endif
  1638. #if HAS_FAN1
  1639. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1640. #endif
  1641. #if HAS_FAN2
  1642. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1643. #endif
  1644. #endif
  1645. }
  1646. // SOFT_PWM_SCALE to frequency:
  1647. //
  1648. // 0: 16000000/64/256/128 = 7.6294 Hz
  1649. // 1: / 64 = 15.2588 Hz
  1650. // 2: / 32 = 30.5176 Hz
  1651. // 3: / 16 = 61.0352 Hz
  1652. // 4: / 8 = 122.0703 Hz
  1653. // 5: / 4 = 244.1406 Hz
  1654. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1655. #else // SLOW_PWM_HEATERS
  1656. /**
  1657. * SLOW PWM HEATERS
  1658. *
  1659. * For relay-driven heaters
  1660. */
  1661. #ifndef MIN_STATE_TIME
  1662. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1663. #endif
  1664. // Macros for Slow PWM timer logic
  1665. #define _SLOW_PWM_ROUTINE(NR, src) \
  1666. soft_pwm_count_ ##NR = src; \
  1667. if (soft_pwm_count_ ##NR > 0) { \
  1668. if (state_timer_heater_ ##NR == 0) { \
  1669. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1670. state_heater_ ##NR = 1; \
  1671. WRITE_HEATER_ ##NR(1); \
  1672. } \
  1673. } \
  1674. else { \
  1675. if (state_timer_heater_ ##NR == 0) { \
  1676. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1677. state_heater_ ##NR = 0; \
  1678. WRITE_HEATER_ ##NR(0); \
  1679. } \
  1680. }
  1681. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm_amount[n])
  1682. #define PWM_OFF_ROUTINE(NR) \
  1683. if (soft_pwm_count_ ##NR < slow_pwm_count) { \
  1684. if (state_timer_heater_ ##NR == 0) { \
  1685. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1686. state_heater_ ##NR = 0; \
  1687. WRITE_HEATER_ ##NR (0); \
  1688. } \
  1689. }
  1690. if (slow_pwm_count == 0) {
  1691. SLOW_PWM_ROUTINE(0);
  1692. #if HOTENDS > 1
  1693. SLOW_PWM_ROUTINE(1);
  1694. #if HOTENDS > 2
  1695. SLOW_PWM_ROUTINE(2);
  1696. #if HOTENDS > 3
  1697. SLOW_PWM_ROUTINE(3);
  1698. #if HOTENDS > 4
  1699. SLOW_PWM_ROUTINE(4);
  1700. #endif // HOTENDS > 4
  1701. #endif // HOTENDS > 3
  1702. #endif // HOTENDS > 2
  1703. #endif // HOTENDS > 1
  1704. #if HAS_HEATED_BED
  1705. _SLOW_PWM_ROUTINE(BED, soft_pwm_amount_bed); // BED
  1706. #endif
  1707. } // slow_pwm_count == 0
  1708. PWM_OFF_ROUTINE(0);
  1709. #if HOTENDS > 1
  1710. PWM_OFF_ROUTINE(1);
  1711. #if HOTENDS > 2
  1712. PWM_OFF_ROUTINE(2);
  1713. #if HOTENDS > 3
  1714. PWM_OFF_ROUTINE(3);
  1715. #if HOTENDS > 4
  1716. PWM_OFF_ROUTINE(4);
  1717. #endif // HOTENDS > 4
  1718. #endif // HOTENDS > 3
  1719. #endif // HOTENDS > 2
  1720. #endif // HOTENDS > 1
  1721. #if HAS_HEATED_BED
  1722. PWM_OFF_ROUTINE(BED); // BED
  1723. #endif
  1724. #if ENABLED(FAN_SOFT_PWM)
  1725. if (pwm_count_tmp >= 127) {
  1726. pwm_count_tmp = 0;
  1727. #if HAS_FAN0
  1728. soft_pwm_count_fan[0] = soft_pwm_amount_fan[0] >> 1;
  1729. WRITE_FAN(soft_pwm_count_fan[0] > 0 ? HIGH : LOW);
  1730. #endif
  1731. #if HAS_FAN1
  1732. soft_pwm_count_fan[1] = soft_pwm_amount_fan[1] >> 1;
  1733. WRITE_FAN1(soft_pwm_count_fan[1] > 0 ? HIGH : LOW);
  1734. #endif
  1735. #if HAS_FAN2
  1736. soft_pwm_count_fan[2] = soft_pwm_amount_fan[2] >> 1;
  1737. WRITE_FAN2(soft_pwm_count_fan[2] > 0 ? HIGH : LOW);
  1738. #endif
  1739. }
  1740. #if HAS_FAN0
  1741. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1742. #endif
  1743. #if HAS_FAN1
  1744. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1745. #endif
  1746. #if HAS_FAN2
  1747. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1748. #endif
  1749. #endif // FAN_SOFT_PWM
  1750. // SOFT_PWM_SCALE to frequency:
  1751. //
  1752. // 0: 16000000/64/256/128 = 7.6294 Hz
  1753. // 1: / 64 = 15.2588 Hz
  1754. // 2: / 32 = 30.5176 Hz
  1755. // 3: / 16 = 61.0352 Hz
  1756. // 4: / 8 = 122.0703 Hz
  1757. // 5: / 4 = 244.1406 Hz
  1758. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1759. // increment slow_pwm_count only every 64th pwm_count,
  1760. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1761. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1762. slow_pwm_count++;
  1763. slow_pwm_count &= 0x7F;
  1764. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1765. #if HOTENDS > 1
  1766. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1767. #if HOTENDS > 2
  1768. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1769. #if HOTENDS > 3
  1770. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1771. #if HOTENDS > 4
  1772. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1773. #endif // HOTENDS > 4
  1774. #endif // HOTENDS > 3
  1775. #endif // HOTENDS > 2
  1776. #endif // HOTENDS > 1
  1777. #if HAS_HEATED_BED
  1778. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1779. #endif
  1780. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1781. #endif // SLOW_PWM_HEATERS
  1782. //
  1783. // Update lcd buttons 488 times per second
  1784. //
  1785. static bool do_buttons;
  1786. if ((do_buttons ^= true)) lcd_buttons_update();
  1787. /**
  1788. * One sensor is sampled on every other call of the ISR.
  1789. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1790. *
  1791. * On each Prepare pass, ADC is started for a sensor pin.
  1792. * On the next pass, the ADC value is read and accumulated.
  1793. *
  1794. * This gives each ADC 0.9765ms to charge up.
  1795. */
  1796. switch (adc_sensor_state) {
  1797. case SensorsReady: {
  1798. // All sensors have been read. Stay in this state for a few
  1799. // ISRs to save on calls to temp update/checking code below.
  1800. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  1801. static uint8_t delay_count = 0;
  1802. if (extra_loops > 0) {
  1803. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1804. if (--delay_count) // While delaying...
  1805. adc_sensor_state = (ADCSensorState)(int(SensorsReady) - 1); // retain this state (else, next state will be 0)
  1806. break;
  1807. }
  1808. else
  1809. adc_sensor_state = (ADCSensorState)0; // Fall-through to start first sensor now
  1810. }
  1811. #if HAS_TEMP_ADC_0
  1812. case PrepareTemp_0:
  1813. HAL_START_ADC(TEMP_0_PIN);
  1814. break;
  1815. case MeasureTemp_0:
  1816. raw_temp_value[0] += HAL_READ_ADC;
  1817. break;
  1818. #endif
  1819. #if HAS_HEATED_BED
  1820. case PrepareTemp_BED:
  1821. HAL_START_ADC(TEMP_BED_PIN);
  1822. break;
  1823. case MeasureTemp_BED:
  1824. raw_temp_bed_value += HAL_READ_ADC;
  1825. break;
  1826. #endif
  1827. #if HAS_TEMP_CHAMBER
  1828. case PrepareTemp_CHAMBER:
  1829. HAL_START_ADC(TEMP_CHAMBER_PIN);
  1830. break;
  1831. case MeasureTemp_CHAMBER:
  1832. raw_temp_chamber_value += ADC;
  1833. break;
  1834. #endif
  1835. #if HAS_TEMP_ADC_1
  1836. case PrepareTemp_1:
  1837. HAL_START_ADC(TEMP_1_PIN);
  1838. break;
  1839. case MeasureTemp_1:
  1840. raw_temp_value[1] += HAL_READ_ADC;
  1841. break;
  1842. #endif
  1843. #if HAS_TEMP_ADC_2
  1844. case PrepareTemp_2:
  1845. HAL_START_ADC(TEMP_2_PIN);
  1846. break;
  1847. case MeasureTemp_2:
  1848. raw_temp_value[2] += HAL_READ_ADC;
  1849. break;
  1850. #endif
  1851. #if HAS_TEMP_ADC_3
  1852. case PrepareTemp_3:
  1853. HAL_START_ADC(TEMP_3_PIN);
  1854. break;
  1855. case MeasureTemp_3:
  1856. raw_temp_value[3] += HAL_READ_ADC;
  1857. break;
  1858. #endif
  1859. #if HAS_TEMP_ADC_4
  1860. case PrepareTemp_4:
  1861. HAL_START_ADC(TEMP_4_PIN);
  1862. break;
  1863. case MeasureTemp_4:
  1864. raw_temp_value[4] += HAL_READ_ADC;
  1865. break;
  1866. #endif
  1867. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1868. case Prepare_FILWIDTH:
  1869. HAL_START_ADC(FILWIDTH_PIN);
  1870. break;
  1871. case Measure_FILWIDTH:
  1872. if (HAL_READ_ADC > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1873. raw_filwidth_value -= (raw_filwidth_value >> 7); // Subtract 1/128th of the raw_filwidth_value
  1874. raw_filwidth_value += ((unsigned long)HAL_READ_ADC << 7); // Add new ADC reading, scaled by 128
  1875. }
  1876. break;
  1877. #endif
  1878. #if ENABLED(ADC_KEYPAD)
  1879. case Prepare_ADC_KEY:
  1880. HAL_START_ADC(ADC_KEYPAD_PIN);
  1881. break;
  1882. case Measure_ADC_KEY:
  1883. if (ADCKey_count < 16) {
  1884. raw_ADCKey_value = ADC;
  1885. if (raw_ADCKey_value > 900) {
  1886. //ADC Key release
  1887. ADCKey_count = 0;
  1888. current_ADCKey_raw = 0;
  1889. }
  1890. else {
  1891. current_ADCKey_raw += raw_ADCKey_value;
  1892. ADCKey_count++;
  1893. }
  1894. }
  1895. break;
  1896. #endif // ADC_KEYPAD
  1897. case StartupDelay: break;
  1898. } // switch(adc_sensor_state)
  1899. if (!adc_sensor_state && ++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1900. temp_count = 0;
  1901. // Update the raw values if they've been read. Else we could be updating them during reading.
  1902. if (!temp_meas_ready) set_current_temp_raw();
  1903. // Filament Sensor - can be read any time since IIR filtering is used
  1904. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1905. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1906. #endif
  1907. ZERO(raw_temp_value);
  1908. #if HAS_HEATED_BED
  1909. raw_temp_bed_value = 0;
  1910. #endif
  1911. #if HAS_TEMP_CHAMBER
  1912. raw_temp_chamber_value = 0;
  1913. #endif
  1914. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1915. int constexpr temp_dir[] = {
  1916. #if ENABLED(HEATER_0_USES_MAX6675)
  1917. 0
  1918. #else
  1919. TEMPDIR(0)
  1920. #endif
  1921. #if HOTENDS > 1
  1922. , TEMPDIR(1)
  1923. #if HOTENDS > 2
  1924. , TEMPDIR(2)
  1925. #if HOTENDS > 3
  1926. , TEMPDIR(3)
  1927. #if HOTENDS > 4
  1928. , TEMPDIR(4)
  1929. #endif // HOTENDS > 4
  1930. #endif // HOTENDS > 3
  1931. #endif // HOTENDS > 2
  1932. #endif // HOTENDS > 1
  1933. };
  1934. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1935. const int16_t tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1936. const bool heater_on = 0 <
  1937. #if ENABLED(PIDTEMP)
  1938. soft_pwm_amount[e]
  1939. #else
  1940. target_temperature[e]
  1941. #endif
  1942. ;
  1943. if (rawtemp > maxttemp_raw[e] * tdir && heater_on) max_temp_error(e);
  1944. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && heater_on) {
  1945. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1946. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1947. #endif
  1948. min_temp_error(e);
  1949. }
  1950. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1951. else
  1952. consecutive_low_temperature_error[e] = 0;
  1953. #endif
  1954. }
  1955. #if HAS_HEATED_BED
  1956. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1957. #define GEBED <=
  1958. #else
  1959. #define GEBED >=
  1960. #endif
  1961. const bool bed_on = 0 <
  1962. #if ENABLED(PIDTEMPBED)
  1963. soft_pwm_amount_bed
  1964. #else
  1965. target_temperature_bed
  1966. #endif
  1967. ;
  1968. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && bed_on) max_temp_error(-1);
  1969. if (bed_minttemp_raw GEBED current_temperature_bed_raw && bed_on) min_temp_error(-1);
  1970. #endif
  1971. } // temp_count >= OVERSAMPLENR
  1972. // Go to the next state, up to SensorsReady
  1973. adc_sensor_state = (ADCSensorState)(int(adc_sensor_state) + 1);
  1974. if (adc_sensor_state > SensorsReady) adc_sensor_state = (ADCSensorState)0;
  1975. #if ENABLED(BABYSTEPPING)
  1976. LOOP_XYZ(axis) {
  1977. const int curTodo = babystepsTodo[axis]; // get rid of volatile for performance
  1978. if (curTodo) {
  1979. stepper.babystep((AxisEnum)axis, curTodo > 0);
  1980. if (curTodo > 0) babystepsTodo[axis]--;
  1981. else babystepsTodo[axis]++;
  1982. }
  1983. }
  1984. #endif // BABYSTEPPING
  1985. // Poll endstops state, if required
  1986. endstops.poll();
  1987. // Periodically call the planner timer
  1988. planner.tick();
  1989. }
  1990. #if HAS_TEMP_SENSOR
  1991. #include "../gcode/gcode.h"
  1992. static void print_heater_state(const float &c, const float &t
  1993. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1994. , const float r
  1995. #endif
  1996. #if NUM_SERIAL > 1
  1997. , const int8_t port=-1
  1998. #endif
  1999. , const int8_t e=-3
  2000. ) {
  2001. #if !(HAS_HEATED_BED && HAS_TEMP_HOTEND && HAS_TEMP_CHAMBER) && HOTENDS <= 1
  2002. UNUSED(e);
  2003. #endif
  2004. SERIAL_PROTOCOLCHAR_P(port, ' ');
  2005. SERIAL_PROTOCOLCHAR_P(port,
  2006. #if HAS_TEMP_CHAMBER && HAS_HEATED_BED && HAS_TEMP_HOTEND
  2007. e == -2 ? 'C' : e == -1 ? 'B' : 'T'
  2008. #elif HAS_HEATED_BED && HAS_TEMP_HOTEND
  2009. e == -1 ? 'B' : 'T'
  2010. #elif HAS_TEMP_HOTEND
  2011. 'T'
  2012. #else
  2013. 'B'
  2014. #endif
  2015. );
  2016. #if HOTENDS > 1
  2017. if (e >= 0) SERIAL_PROTOCOLCHAR_P(port, '0' + e);
  2018. #endif
  2019. SERIAL_PROTOCOLCHAR_P(port, ':');
  2020. SERIAL_PROTOCOL_P(port, c);
  2021. SERIAL_PROTOCOLPAIR_P(port, " /" , t);
  2022. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2023. SERIAL_PROTOCOLPAIR_P(port, " (", r / OVERSAMPLENR);
  2024. SERIAL_PROTOCOLCHAR_P(port, ')');
  2025. #endif
  2026. delay(2);
  2027. }
  2028. void Temperature::print_heaterstates(
  2029. #if NUM_SERIAL > 1
  2030. const int8_t port
  2031. #endif
  2032. ) {
  2033. #if HAS_TEMP_HOTEND
  2034. print_heater_state(degHotend(gcode.target_extruder), degTargetHotend(gcode.target_extruder)
  2035. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2036. , rawHotendTemp(gcode.target_extruder)
  2037. #endif
  2038. #if NUM_SERIAL > 1
  2039. , port
  2040. #endif
  2041. );
  2042. #endif
  2043. #if HAS_HEATED_BED
  2044. print_heater_state(degBed(), degTargetBed()
  2045. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2046. , rawBedTemp()
  2047. #endif
  2048. #if NUM_SERIAL > 1
  2049. , port
  2050. #endif
  2051. , -1 // BED
  2052. );
  2053. #endif
  2054. #if HAS_TEMP_CHAMBER
  2055. print_heater_state(degChamber(), 0
  2056. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2057. , rawChamberTemp()
  2058. #endif
  2059. , -2 // CHAMBER
  2060. );
  2061. #endif
  2062. #if HOTENDS > 1
  2063. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e)
  2064. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  2065. , rawHotendTemp(e)
  2066. #endif
  2067. #if NUM_SERIAL > 1
  2068. , port
  2069. #endif
  2070. , e
  2071. );
  2072. #endif
  2073. SERIAL_PROTOCOLPGM_P(port, " @:");
  2074. SERIAL_PROTOCOL_P(port, getHeaterPower(gcode.target_extruder));
  2075. #if HAS_HEATED_BED
  2076. SERIAL_PROTOCOLPGM_P(port, " B@:");
  2077. SERIAL_PROTOCOL_P(port, getHeaterPower(-1));
  2078. #endif
  2079. #if HOTENDS > 1
  2080. HOTEND_LOOP() {
  2081. SERIAL_PROTOCOLPAIR_P(port, " @", e);
  2082. SERIAL_PROTOCOLCHAR_P(port, ':');
  2083. SERIAL_PROTOCOL_P(port, getHeaterPower(e));
  2084. }
  2085. #endif
  2086. }
  2087. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  2088. uint8_t Temperature::auto_report_temp_interval;
  2089. millis_t Temperature::next_temp_report_ms;
  2090. void Temperature::auto_report_temperatures() {
  2091. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  2092. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  2093. print_heaterstates();
  2094. SERIAL_EOL();
  2095. }
  2096. }
  2097. #endif // AUTO_REPORT_TEMPERATURES
  2098. #endif // HAS_TEMP_SENSOR