My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 66KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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 "Marlin.h"
  26. #include "temperature.h"
  27. #include "thermistortables.h"
  28. #include "ultralcd.h"
  29. #include "planner.h"
  30. #include "language.h"
  31. #if ENABLED(HEATER_0_USES_MAX6675)
  32. #include "spi.h"
  33. #endif
  34. #if ENABLED(BABYSTEPPING)
  35. #include "stepper.h"
  36. #endif
  37. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  38. #include "endstops.h"
  39. #endif
  40. #if ENABLED(USE_WATCHDOG)
  41. #include "watchdog.h"
  42. #endif
  43. #ifdef K1 // Defined in Configuration.h in the PID settings
  44. #define K2 (1.0-K1)
  45. #endif
  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 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 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. Temperature thermalManager;
  54. // public:
  55. float Temperature::current_temperature[HOTENDS] = { 0.0 },
  56. Temperature::current_temperature_bed = 0.0;
  57. int16_t Temperature::current_temperature_raw[HOTENDS] = { 0 },
  58. Temperature::target_temperature[HOTENDS] = { 0 },
  59. Temperature::current_temperature_bed_raw = 0;
  60. #if HAS_HEATER_BED
  61. int16_t Temperature::target_temperature_bed = 0;
  62. #endif
  63. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  64. float Temperature::redundant_temperature = 0.0;
  65. #endif
  66. #if ENABLED(PIDTEMP)
  67. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  68. float Temperature::Kp[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kp),
  69. Temperature::Ki[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Ki) * (PID_dT)),
  70. Temperature::Kd[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Kd) / (PID_dT));
  71. #if ENABLED(PID_EXTRUSION_SCALING)
  72. float Temperature::Kc[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kc);
  73. #endif
  74. #else
  75. float Temperature::Kp = DEFAULT_Kp,
  76. Temperature::Ki = (DEFAULT_Ki) * (PID_dT),
  77. Temperature::Kd = (DEFAULT_Kd) / (PID_dT);
  78. #if ENABLED(PID_EXTRUSION_SCALING)
  79. float Temperature::Kc = DEFAULT_Kc;
  80. #endif
  81. #endif
  82. #endif
  83. #if ENABLED(PIDTEMPBED)
  84. float Temperature::bedKp = DEFAULT_bedKp,
  85. Temperature::bedKi = ((DEFAULT_bedKi) * PID_dT),
  86. Temperature::bedKd = ((DEFAULT_bedKd) / PID_dT);
  87. #endif
  88. #if ENABLED(BABYSTEPPING)
  89. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  90. #endif
  91. #if WATCH_HOTENDS
  92. uint16_t Temperature::watch_target_temp[HOTENDS] = { 0 };
  93. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  94. #endif
  95. #if WATCH_THE_BED
  96. uint16_t Temperature::watch_target_bed_temp = 0;
  97. millis_t Temperature::watch_bed_next_ms = 0;
  98. #endif
  99. #if ENABLED(PREVENT_COLD_EXTRUSION)
  100. bool Temperature::allow_cold_extrude = false;
  101. uint16_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  102. #endif
  103. // private:
  104. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  105. uint16_t Temperature::redundant_temperature_raw = 0;
  106. float Temperature::redundant_temperature = 0.0;
  107. #endif
  108. volatile bool Temperature::temp_meas_ready = false;
  109. #if ENABLED(PIDTEMP)
  110. float Temperature::temp_iState[HOTENDS] = { 0 },
  111. Temperature::temp_dState[HOTENDS] = { 0 },
  112. Temperature::pTerm[HOTENDS],
  113. Temperature::iTerm[HOTENDS],
  114. Temperature::dTerm[HOTENDS];
  115. #if ENABLED(PID_EXTRUSION_SCALING)
  116. float Temperature::cTerm[HOTENDS];
  117. long Temperature::last_e_position;
  118. long Temperature::lpq[LPQ_MAX_LEN];
  119. int Temperature::lpq_ptr = 0;
  120. #endif
  121. float Temperature::pid_error[HOTENDS];
  122. bool Temperature::pid_reset[HOTENDS];
  123. #endif
  124. #if ENABLED(PIDTEMPBED)
  125. float Temperature::temp_iState_bed = { 0 },
  126. Temperature::temp_dState_bed = { 0 },
  127. Temperature::pTerm_bed,
  128. Temperature::iTerm_bed,
  129. Temperature::dTerm_bed,
  130. Temperature::pid_error_bed;
  131. #else
  132. millis_t Temperature::next_bed_check_ms;
  133. #endif
  134. uint16_t Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 },
  135. Temperature::raw_temp_bed_value = 0;
  136. // Init min and max temp with extreme values to prevent false errors during startup
  137. 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),
  138. 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),
  139. Temperature::minttemp[HOTENDS] = { 0 },
  140. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  141. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  142. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  143. #endif
  144. #ifdef MILLISECONDS_PREHEAT_TIME
  145. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  146. #endif
  147. #ifdef BED_MINTEMP
  148. int16_t Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  149. #endif
  150. #ifdef BED_MAXTEMP
  151. int16_t Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  152. #endif
  153. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  154. int16_t Temperature::meas_shift_index; // Index of a delayed sample in buffer
  155. #endif
  156. #if HAS_AUTO_FAN
  157. millis_t Temperature::next_auto_fan_check_ms = 0;
  158. #endif
  159. uint8_t Temperature::soft_pwm_amount[HOTENDS],
  160. Temperature::soft_pwm_amount_bed;
  161. #if ENABLED(FAN_SOFT_PWM)
  162. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  163. Temperature::soft_pwm_count_fan[FAN_COUNT];
  164. #endif
  165. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  166. int Temperature::current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  167. #endif
  168. #if ENABLED(PROBING_HEATERS_OFF)
  169. bool Temperature::paused;
  170. #endif
  171. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  172. millis_t Temperature::heater_idle_timeout_ms[HOTENDS] = { 0 };
  173. bool Temperature::heater_idle_timeout_exceeded[HOTENDS] = { false };
  174. #if HAS_TEMP_BED
  175. millis_t Temperature::bed_idle_timeout_ms = 0;
  176. bool Temperature::bed_idle_timeout_exceeded = false;
  177. #endif
  178. #endif
  179. #if HAS_PID_HEATING
  180. void Temperature::PID_autotune(float temp, int hotend, int ncycles, bool set_result/*=false*/) {
  181. float input = 0.0;
  182. int cycles = 0;
  183. bool heating = true;
  184. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  185. long t_high = 0, t_low = 0;
  186. long bias, d;
  187. float Ku, Tu;
  188. float workKp = 0, workKi = 0, workKd = 0;
  189. float max = 0, min = 10000;
  190. #if HAS_AUTO_FAN
  191. next_auto_fan_check_ms = temp_ms + 2500UL;
  192. #endif
  193. if (hotend >=
  194. #if ENABLED(PIDTEMP)
  195. HOTENDS
  196. #else
  197. 0
  198. #endif
  199. || hotend <
  200. #if ENABLED(PIDTEMPBED)
  201. -1
  202. #else
  203. 0
  204. #endif
  205. ) {
  206. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  207. return;
  208. }
  209. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  210. disable_all_heaters(); // switch off all heaters.
  211. #if HAS_PID_FOR_BOTH
  212. if (hotend < 0)
  213. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  214. else
  215. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  216. #elif ENABLED(PIDTEMP)
  217. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  218. #else
  219. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  220. #endif
  221. wait_for_heatup = true;
  222. // PID Tuning loop
  223. while (wait_for_heatup) {
  224. millis_t ms = millis();
  225. if (temp_meas_ready) { // temp sample ready
  226. updateTemperaturesFromRawValues();
  227. input =
  228. #if HAS_PID_FOR_BOTH
  229. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  230. #elif ENABLED(PIDTEMP)
  231. current_temperature[hotend]
  232. #else
  233. current_temperature_bed
  234. #endif
  235. ;
  236. NOLESS(max, input);
  237. NOMORE(min, input);
  238. #if HAS_AUTO_FAN
  239. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  240. checkExtruderAutoFans();
  241. next_auto_fan_check_ms = ms + 2500UL;
  242. }
  243. #endif
  244. if (heating && input > temp) {
  245. if (ELAPSED(ms, t2 + 5000UL)) {
  246. heating = false;
  247. #if HAS_PID_FOR_BOTH
  248. if (hotend < 0)
  249. soft_pwm_amount_bed = (bias - d) >> 1;
  250. else
  251. soft_pwm_amount[hotend] = (bias - d) >> 1;
  252. #elif ENABLED(PIDTEMP)
  253. soft_pwm_amount[hotend] = (bias - d) >> 1;
  254. #elif ENABLED(PIDTEMPBED)
  255. soft_pwm_amount_bed = (bias - d) >> 1;
  256. #endif
  257. t1 = ms;
  258. t_high = t1 - t2;
  259. max = temp;
  260. }
  261. }
  262. if (!heating && input < temp) {
  263. if (ELAPSED(ms, t1 + 5000UL)) {
  264. heating = true;
  265. t2 = ms;
  266. t_low = t2 - t1;
  267. if (cycles > 0) {
  268. long max_pow =
  269. #if HAS_PID_FOR_BOTH
  270. hotend < 0 ? MAX_BED_POWER : PID_MAX
  271. #elif ENABLED(PIDTEMP)
  272. PID_MAX
  273. #else
  274. MAX_BED_POWER
  275. #endif
  276. ;
  277. bias += (d * (t_high - t_low)) / (t_low + t_high);
  278. bias = constrain(bias, 20, max_pow - 20);
  279. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  280. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  281. SERIAL_PROTOCOLPAIR(MSG_D, d);
  282. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  283. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  284. if (cycles > 2) {
  285. Ku = (4.0 * d) / (M_PI * (max - min) * 0.5);
  286. Tu = ((float)(t_low + t_high) * 0.001);
  287. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  288. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  289. workKp = 0.6 * Ku;
  290. workKi = 2 * workKp / Tu;
  291. workKd = workKp * Tu * 0.125;
  292. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  293. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  294. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  295. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  296. /**
  297. workKp = 0.33*Ku;
  298. workKi = workKp/Tu;
  299. workKd = workKp*Tu/3;
  300. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  301. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  302. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  303. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  304. workKp = 0.2*Ku;
  305. workKi = 2*workKp/Tu;
  306. workKd = workKp*Tu/3;
  307. SERIAL_PROTOCOLLNPGM(" No overshoot");
  308. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  309. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  310. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  311. */
  312. }
  313. }
  314. #if HAS_PID_FOR_BOTH
  315. if (hotend < 0)
  316. soft_pwm_amount_bed = (bias + d) >> 1;
  317. else
  318. soft_pwm_amount[hotend] = (bias + d) >> 1;
  319. #elif ENABLED(PIDTEMP)
  320. soft_pwm_amount[hotend] = (bias + d) >> 1;
  321. #else
  322. soft_pwm_amount_bed = (bias + d) >> 1;
  323. #endif
  324. cycles++;
  325. min = temp;
  326. }
  327. }
  328. }
  329. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  330. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  331. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  332. return;
  333. }
  334. // Every 2 seconds...
  335. if (ELAPSED(ms, temp_ms + 2000UL)) {
  336. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  337. print_heaterstates();
  338. SERIAL_EOL;
  339. #endif
  340. temp_ms = ms;
  341. } // every 2 seconds
  342. // Over 2 minutes?
  343. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  344. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  345. return;
  346. }
  347. if (cycles > ncycles) {
  348. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  349. #if HAS_PID_FOR_BOTH
  350. const char* estring = hotend < 0 ? "bed" : "";
  351. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL;
  352. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL;
  353. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL;
  354. #elif ENABLED(PIDTEMP)
  355. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL;
  356. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL;
  357. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL;
  358. #else
  359. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL;
  360. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL;
  361. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL;
  362. #endif
  363. #define _SET_BED_PID() do { \
  364. bedKp = workKp; \
  365. bedKi = scalePID_i(workKi); \
  366. bedKd = scalePID_d(workKd); \
  367. updatePID(); } while(0)
  368. #define _SET_EXTRUDER_PID() do { \
  369. PID_PARAM(Kp, hotend) = workKp; \
  370. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  371. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  372. updatePID(); } while(0)
  373. // Use the result? (As with "M303 U1")
  374. if (set_result) {
  375. #if HAS_PID_FOR_BOTH
  376. if (hotend < 0)
  377. _SET_BED_PID();
  378. else
  379. _SET_EXTRUDER_PID();
  380. #elif ENABLED(PIDTEMP)
  381. _SET_EXTRUDER_PID();
  382. #else
  383. _SET_BED_PID();
  384. #endif
  385. }
  386. return;
  387. }
  388. lcd_update();
  389. }
  390. if (!wait_for_heatup) disable_all_heaters();
  391. }
  392. #endif // HAS_PID_HEATING
  393. /**
  394. * Class and Instance Methods
  395. */
  396. Temperature::Temperature() { }
  397. void Temperature::updatePID() {
  398. #if ENABLED(PIDTEMP)
  399. #if ENABLED(PID_EXTRUSION_SCALING)
  400. last_e_position = 0;
  401. #endif
  402. #endif
  403. }
  404. int Temperature::getHeaterPower(int heater) {
  405. return heater < 0 ? soft_pwm_amount_bed : soft_pwm_amount[heater];
  406. }
  407. #if HAS_AUTO_FAN
  408. void Temperature::checkExtruderAutoFans() {
  409. constexpr int8_t fanPin[] = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN };
  410. constexpr int fanBit[] = {
  411. 0,
  412. AUTO_1_IS_0 ? 0 : 1,
  413. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  414. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3,
  415. AUTO_4_IS_0 ? 0 : AUTO_4_IS_1 ? 1 : AUTO_4_IS_2 ? 2 : AUTO_4_IS_3 ? 3 : 4
  416. };
  417. uint8_t fanState = 0;
  418. HOTEND_LOOP() {
  419. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  420. SBI(fanState, fanBit[e]);
  421. }
  422. uint8_t fanDone = 0;
  423. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  424. int8_t pin = fanPin[f];
  425. if (pin >= 0 && !TEST(fanDone, fanBit[f])) {
  426. uint8_t newFanSpeed = TEST(fanState, fanBit[f]) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  427. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  428. digitalWrite(pin, newFanSpeed);
  429. analogWrite(pin, newFanSpeed);
  430. SBI(fanDone, fanBit[f]);
  431. }
  432. }
  433. }
  434. #endif // HAS_AUTO_FAN
  435. //
  436. // Temperature Error Handlers
  437. //
  438. void Temperature::_temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  439. static bool killed = false;
  440. if (IsRunning()) {
  441. SERIAL_ERROR_START;
  442. serialprintPGM(serial_msg);
  443. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  444. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  445. }
  446. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  447. if (!killed) {
  448. Running = false;
  449. killed = true;
  450. kill(lcd_msg);
  451. }
  452. else
  453. disable_all_heaters(); // paranoia
  454. #endif
  455. }
  456. void Temperature::max_temp_error(int8_t e) {
  457. #if HAS_TEMP_BED
  458. _temp_error(e, PSTR(MSG_T_MAXTEMP), e >= 0 ? PSTR(MSG_ERR_MAXTEMP) : PSTR(MSG_ERR_MAXTEMP_BED));
  459. #else
  460. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  461. #if HOTENDS == 1
  462. UNUSED(e);
  463. #endif
  464. #endif
  465. }
  466. void Temperature::min_temp_error(int8_t e) {
  467. #if HAS_TEMP_BED
  468. _temp_error(e, PSTR(MSG_T_MINTEMP), e >= 0 ? PSTR(MSG_ERR_MINTEMP) : PSTR(MSG_ERR_MINTEMP_BED));
  469. #else
  470. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  471. #if HOTENDS == 1
  472. UNUSED(e);
  473. #endif
  474. #endif
  475. }
  476. float Temperature::get_pid_output(int e) {
  477. #if HOTENDS == 1
  478. UNUSED(e);
  479. #define _HOTEND_TEST true
  480. #else
  481. #define _HOTEND_TEST e == active_extruder
  482. #endif
  483. float pid_output;
  484. #if ENABLED(PIDTEMP)
  485. #if DISABLED(PID_OPENLOOP)
  486. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  487. dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
  488. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  489. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  490. if (heater_idle_timeout_exceeded[HOTEND_INDEX]) {
  491. pid_output = 0;
  492. pid_reset[HOTEND_INDEX] = true;
  493. }
  494. else
  495. #endif
  496. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  497. pid_output = BANG_MAX;
  498. pid_reset[HOTEND_INDEX] = true;
  499. }
  500. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0
  501. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  502. || heater_idle_timeout_exceeded[HOTEND_INDEX]
  503. #endif
  504. ) {
  505. pid_output = 0;
  506. pid_reset[HOTEND_INDEX] = true;
  507. }
  508. else {
  509. if (pid_reset[HOTEND_INDEX]) {
  510. temp_iState[HOTEND_INDEX] = 0.0;
  511. pid_reset[HOTEND_INDEX] = false;
  512. }
  513. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  514. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  515. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  516. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  517. #if ENABLED(PID_EXTRUSION_SCALING)
  518. cTerm[HOTEND_INDEX] = 0;
  519. if (_HOTEND_TEST) {
  520. long e_position = stepper.position(E_AXIS);
  521. if (e_position > last_e_position) {
  522. lpq[lpq_ptr] = e_position - last_e_position;
  523. last_e_position = e_position;
  524. }
  525. else {
  526. lpq[lpq_ptr] = 0;
  527. }
  528. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  529. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  530. pid_output += cTerm[HOTEND_INDEX];
  531. }
  532. #endif // PID_EXTRUSION_SCALING
  533. if (pid_output > PID_MAX) {
  534. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  535. pid_output = PID_MAX;
  536. }
  537. else if (pid_output < 0) {
  538. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  539. pid_output = 0;
  540. }
  541. }
  542. #else
  543. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  544. #endif // PID_OPENLOOP
  545. #if ENABLED(PID_DEBUG)
  546. SERIAL_ECHO_START;
  547. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  548. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  549. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  550. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  551. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  552. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  553. #if ENABLED(PID_EXTRUSION_SCALING)
  554. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  555. #endif
  556. SERIAL_EOL;
  557. #endif // PID_DEBUG
  558. #else /* PID off */
  559. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  560. if (heater_idle_timeout_exceeded[HOTEND_INDEX])
  561. pid_output = 0;
  562. else
  563. #endif
  564. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  565. #endif
  566. return pid_output;
  567. }
  568. #if ENABLED(PIDTEMPBED)
  569. float Temperature::get_pid_output_bed() {
  570. float pid_output;
  571. #if DISABLED(PID_OPENLOOP)
  572. pid_error_bed = target_temperature_bed - current_temperature_bed;
  573. pTerm_bed = bedKp * pid_error_bed;
  574. temp_iState_bed += pid_error_bed;
  575. iTerm_bed = bedKi * temp_iState_bed;
  576. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  577. temp_dState_bed = current_temperature_bed;
  578. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  579. if (pid_output > MAX_BED_POWER) {
  580. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  581. pid_output = MAX_BED_POWER;
  582. }
  583. else if (pid_output < 0) {
  584. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  585. pid_output = 0;
  586. }
  587. #else
  588. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  589. #endif // PID_OPENLOOP
  590. #if ENABLED(PID_BED_DEBUG)
  591. SERIAL_ECHO_START;
  592. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  593. SERIAL_ECHOPGM(": Input ");
  594. SERIAL_ECHO(current_temperature_bed);
  595. SERIAL_ECHOPGM(" Output ");
  596. SERIAL_ECHO(pid_output);
  597. SERIAL_ECHOPGM(" pTerm ");
  598. SERIAL_ECHO(pTerm_bed);
  599. SERIAL_ECHOPGM(" iTerm ");
  600. SERIAL_ECHO(iTerm_bed);
  601. SERIAL_ECHOPGM(" dTerm ");
  602. SERIAL_ECHOLN(dTerm_bed);
  603. #endif // PID_BED_DEBUG
  604. return pid_output;
  605. }
  606. #endif // PIDTEMPBED
  607. /**
  608. * Manage heating activities for extruder hot-ends and a heated bed
  609. * - Acquire updated temperature readings
  610. * - Also resets the watchdog timer
  611. * - Invoke thermal runaway protection
  612. * - Manage extruder auto-fan
  613. * - Apply filament width to the extrusion rate (may move)
  614. * - Update the heated bed PID output value
  615. */
  616. /**
  617. * The following line SOMETIMES results in the dreaded "unable to find a register to spill in class 'POINTER_REGS'"
  618. * compile error.
  619. * 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);
  620. *
  621. * This is due to a bug in the C++ compiler used by the Arduino IDE from 1.6.10 to at least 1.8.1.
  622. *
  623. * The work around is to add the compiler flag "__attribute__((__optimize__("O2")))" to the declaration for manage_heater()
  624. */
  625. //void Temperature::manage_heater() __attribute__((__optimize__("O2")));
  626. void Temperature::manage_heater() {
  627. if (!temp_meas_ready) return;
  628. updateTemperaturesFromRawValues(); // also resets the watchdog
  629. #if ENABLED(HEATER_0_USES_MAX6675)
  630. if (current_temperature[0] > min(HEATER_0_MAXTEMP, MAX6675_TMAX - 1.0)) max_temp_error(0);
  631. if (current_temperature[0] < max(HEATER_0_MINTEMP, MAX6675_TMIN + .01)) min_temp_error(0);
  632. #endif
  633. #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN || ENABLED(ADVANCED_PAUSE_FEATURE)
  634. millis_t ms = millis();
  635. #endif
  636. HOTEND_LOOP() {
  637. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  638. if (!heater_idle_timeout_exceeded[e] && heater_idle_timeout_ms[e] && ELAPSED(ms, heater_idle_timeout_ms[e]))
  639. heater_idle_timeout_exceeded[e] = true;
  640. #endif
  641. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  642. // Check for thermal runaway
  643. 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);
  644. #endif
  645. soft_pwm_amount[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)get_pid_output(e) >> 1 : 0;
  646. #if WATCH_HOTENDS
  647. // Make sure temperature is increasing
  648. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) { // Time to check this extruder?
  649. if (degHotend(e) < watch_target_temp[e]) // Failed to increase enough?
  650. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  651. else // Start again if the target is still far off
  652. start_watching_heater(e);
  653. }
  654. #endif
  655. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  656. // Make sure measured temperatures are close together
  657. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  658. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  659. #endif
  660. } // HOTEND_LOOP
  661. #if HAS_AUTO_FAN
  662. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  663. checkExtruderAutoFans();
  664. next_auto_fan_check_ms = ms + 2500UL;
  665. }
  666. #endif
  667. // Control the extruder rate based on the width sensor
  668. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  669. if (filament_sensor) {
  670. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  671. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  672. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  673. // Get the delayed info and add 100 to reconstitute to a percent of
  674. // the nominal filament diameter then square it to get an area
  675. const float vmroot = measurement_delay[meas_shift_index] * 0.01 + 1.0;
  676. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vmroot <= 0.1 ? 0.01 : sq(vmroot);
  677. }
  678. #endif // FILAMENT_WIDTH_SENSOR
  679. #if WATCH_THE_BED
  680. // Make sure temperature is increasing
  681. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) { // Time to check the bed?
  682. if (degBed() < watch_target_bed_temp) // Failed to increase enough?
  683. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  684. else // Start again if the target is still far off
  685. start_watching_bed();
  686. }
  687. #endif // WATCH_THE_BED
  688. #if DISABLED(PIDTEMPBED)
  689. if (PENDING(ms, next_bed_check_ms)) return;
  690. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  691. #endif
  692. #if HAS_TEMP_BED
  693. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  694. if (!bed_idle_timeout_exceeded && bed_idle_timeout_ms && ELAPSED(ms, bed_idle_timeout_ms))
  695. bed_idle_timeout_exceeded = true;
  696. #endif
  697. #if HAS_THERMALLY_PROTECTED_BED
  698. 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);
  699. #endif
  700. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  701. if (bed_idle_timeout_exceeded)
  702. {
  703. soft_pwm_amount_bed = 0;
  704. #if DISABLED(PIDTEMPBED)
  705. WRITE_HEATER_BED(LOW);
  706. #endif
  707. }
  708. else
  709. #endif
  710. {
  711. #if ENABLED(PIDTEMPBED)
  712. soft_pwm_amount_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  713. #elif ENABLED(BED_LIMIT_SWITCHING)
  714. // Check if temperature is within the correct band
  715. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  716. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  717. soft_pwm_amount_bed = 0;
  718. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  719. soft_pwm_amount_bed = MAX_BED_POWER >> 1;
  720. }
  721. else {
  722. soft_pwm_amount_bed = 0;
  723. WRITE_HEATER_BED(LOW);
  724. }
  725. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  726. // Check if temperature is within the correct range
  727. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  728. soft_pwm_amount_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  729. }
  730. else {
  731. soft_pwm_amount_bed = 0;
  732. WRITE_HEATER_BED(LOW);
  733. }
  734. #endif
  735. }
  736. #endif // HAS_TEMP_BED
  737. }
  738. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  739. // Derived from RepRap FiveD extruder::getTemperature()
  740. // For hot end temperature measurement.
  741. float Temperature::analog2temp(int raw, uint8_t e) {
  742. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  743. if (e > HOTENDS)
  744. #else
  745. if (e >= HOTENDS)
  746. #endif
  747. {
  748. SERIAL_ERROR_START;
  749. SERIAL_ERROR((int)e);
  750. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  751. kill(PSTR(MSG_KILLED));
  752. return 0.0;
  753. }
  754. #if ENABLED(HEATER_0_USES_MAX6675)
  755. if (e == 0) return 0.25 * raw;
  756. #endif
  757. if (heater_ttbl_map[e] != NULL) {
  758. float celsius = 0;
  759. uint8_t i;
  760. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  761. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  762. if (PGM_RD_W((*tt)[i][0]) > raw) {
  763. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  764. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  765. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  766. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  767. break;
  768. }
  769. }
  770. // Overflow: Set to last value in the table
  771. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  772. return celsius;
  773. }
  774. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  775. }
  776. // Derived from RepRap FiveD extruder::getTemperature()
  777. // For bed temperature measurement.
  778. float Temperature::analog2tempBed(int raw) {
  779. #if ENABLED(BED_USES_THERMISTOR)
  780. float celsius = 0;
  781. byte i;
  782. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  783. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  784. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  785. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  786. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  787. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  788. break;
  789. }
  790. }
  791. // Overflow: Set to last value in the table
  792. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  793. return celsius;
  794. #elif defined(BED_USES_AD595)
  795. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  796. #else
  797. UNUSED(raw);
  798. return 0;
  799. #endif
  800. }
  801. /**
  802. * Get the raw values into the actual temperatures.
  803. * The raw values are created in interrupt context,
  804. * and this function is called from normal context
  805. * as it would block the stepper routine.
  806. */
  807. void Temperature::updateTemperaturesFromRawValues() {
  808. #if ENABLED(HEATER_0_USES_MAX6675)
  809. current_temperature_raw[0] = read_max6675();
  810. #endif
  811. HOTEND_LOOP()
  812. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  813. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  814. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  815. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  816. #endif
  817. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  818. filament_width_meas = analog2widthFil();
  819. #endif
  820. #if ENABLED(USE_WATCHDOG)
  821. // Reset the watchdog after we know we have a temperature measurement.
  822. watchdog_reset();
  823. #endif
  824. CRITICAL_SECTION_START;
  825. temp_meas_ready = false;
  826. CRITICAL_SECTION_END;
  827. }
  828. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  829. // Convert raw Filament Width to millimeters
  830. float Temperature::analog2widthFil() {
  831. return current_raw_filwidth / 16383.0 * 5.0;
  832. //return current_raw_filwidth;
  833. }
  834. // Convert raw Filament Width to a ratio
  835. int Temperature::widthFil_to_size_ratio() {
  836. float temp = filament_width_meas;
  837. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  838. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  839. return filament_width_nominal / temp * 100;
  840. }
  841. #endif
  842. #if ENABLED(HEATER_0_USES_MAX6675)
  843. #ifndef MAX6675_SCK_PIN
  844. #define MAX6675_SCK_PIN SCK_PIN
  845. #endif
  846. #ifndef MAX6675_DO_PIN
  847. #define MAX6675_DO_PIN MISO_PIN
  848. #endif
  849. SPI<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  850. #endif
  851. /**
  852. * Initialize the temperature manager
  853. * The manager is implemented by periodic calls to manage_heater()
  854. */
  855. void Temperature::init() {
  856. #if MB(RUMBA) && (TEMP_SENSOR_0 == -1 || TEMP_SENSOR_1 == -1 || TEMP_SENSOR_2 == -1 || TEMP_SENSOR_BED == -1)
  857. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  858. MCUCR = _BV(JTD);
  859. MCUCR = _BV(JTD);
  860. #endif
  861. // Finish init of mult hotend arrays
  862. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  863. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  864. last_e_position = 0;
  865. #endif
  866. #if HAS_HEATER_0
  867. SET_OUTPUT(HEATER_0_PIN);
  868. #endif
  869. #if HAS_HEATER_1
  870. SET_OUTPUT(HEATER_1_PIN);
  871. #endif
  872. #if HAS_HEATER_2
  873. SET_OUTPUT(HEATER_2_PIN);
  874. #endif
  875. #if HAS_HEATER_3
  876. SET_OUTPUT(HEATER_3_PIN);
  877. #endif
  878. #if HAS_HEATER_4
  879. SET_OUTPUT(HEATER_3_PIN);
  880. #endif
  881. #if HAS_HEATER_BED
  882. SET_OUTPUT(HEATER_BED_PIN);
  883. #endif
  884. #if HAS_FAN0
  885. SET_OUTPUT(FAN_PIN);
  886. #if ENABLED(FAST_PWM_FAN)
  887. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  888. #endif
  889. #endif
  890. #if HAS_FAN1
  891. SET_OUTPUT(FAN1_PIN);
  892. #if ENABLED(FAST_PWM_FAN)
  893. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  894. #endif
  895. #endif
  896. #if HAS_FAN2
  897. SET_OUTPUT(FAN2_PIN);
  898. #if ENABLED(FAST_PWM_FAN)
  899. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  900. #endif
  901. #endif
  902. #if ENABLED(HEATER_0_USES_MAX6675)
  903. OUT_WRITE(SCK_PIN, LOW);
  904. OUT_WRITE(MOSI_PIN, HIGH);
  905. SET_INPUT_PULLUP(MISO_PIN);
  906. max6675_spi.init();
  907. OUT_WRITE(SS_PIN, HIGH);
  908. OUT_WRITE(MAX6675_SS, HIGH);
  909. #endif // HEATER_0_USES_MAX6675
  910. #ifdef DIDR2
  911. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  912. #else
  913. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  914. #endif
  915. // Set analog inputs
  916. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  917. DIDR0 = 0;
  918. #ifdef DIDR2
  919. DIDR2 = 0;
  920. #endif
  921. #if HAS_TEMP_0
  922. ANALOG_SELECT(TEMP_0_PIN);
  923. #endif
  924. #if HAS_TEMP_1
  925. ANALOG_SELECT(TEMP_1_PIN);
  926. #endif
  927. #if HAS_TEMP_2
  928. ANALOG_SELECT(TEMP_2_PIN);
  929. #endif
  930. #if HAS_TEMP_3
  931. ANALOG_SELECT(TEMP_3_PIN);
  932. #endif
  933. #if HAS_TEMP_4
  934. ANALOG_SELECT(TEMP_4_PIN);
  935. #endif
  936. #if HAS_TEMP_BED
  937. ANALOG_SELECT(TEMP_BED_PIN);
  938. #endif
  939. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  940. ANALOG_SELECT(FILWIDTH_PIN);
  941. #endif
  942. #if HAS_AUTO_FAN_0
  943. #if E0_AUTO_FAN_PIN == FAN1_PIN
  944. SET_OUTPUT(E0_AUTO_FAN_PIN);
  945. #if ENABLED(FAST_PWM_FAN)
  946. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  947. #endif
  948. #else
  949. SET_OUTPUT(E0_AUTO_FAN_PIN);
  950. #endif
  951. #endif
  952. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  953. #if E1_AUTO_FAN_PIN == FAN1_PIN
  954. SET_OUTPUT(E1_AUTO_FAN_PIN);
  955. #if ENABLED(FAST_PWM_FAN)
  956. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  957. #endif
  958. #else
  959. SET_OUTPUT(E1_AUTO_FAN_PIN);
  960. #endif
  961. #endif
  962. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  963. #if E2_AUTO_FAN_PIN == FAN1_PIN
  964. SET_OUTPUT(E2_AUTO_FAN_PIN);
  965. #if ENABLED(FAST_PWM_FAN)
  966. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  967. #endif
  968. #else
  969. SET_OUTPUT(E2_AUTO_FAN_PIN);
  970. #endif
  971. #endif
  972. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  973. #if E3_AUTO_FAN_PIN == FAN1_PIN
  974. SET_OUTPUT(E3_AUTO_FAN_PIN);
  975. #if ENABLED(FAST_PWM_FAN)
  976. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  977. #endif
  978. #else
  979. SET_OUTPUT(E3_AUTO_FAN_PIN);
  980. #endif
  981. #endif
  982. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  983. #if E4_AUTO_FAN_PIN == FAN1_PIN
  984. SET_OUTPUT(E4_AUTO_FAN_PIN);
  985. #if ENABLED(FAST_PWM_FAN)
  986. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  987. #endif
  988. #else
  989. SET_OUTPUT(E4_AUTO_FAN_PIN);
  990. #endif
  991. #endif
  992. // Use timer0 for temperature measurement
  993. // Interleave temperature interrupt with millies interrupt
  994. OCR0B = 128;
  995. SBI(TIMSK0, OCIE0B);
  996. // Wait for temperature measurement to settle
  997. delay(250);
  998. #define TEMP_MIN_ROUTINE(NR) \
  999. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  1000. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  1001. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1002. minttemp_raw[NR] += OVERSAMPLENR; \
  1003. else \
  1004. minttemp_raw[NR] -= OVERSAMPLENR; \
  1005. }
  1006. #define TEMP_MAX_ROUTINE(NR) \
  1007. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  1008. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  1009. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1010. maxttemp_raw[NR] -= OVERSAMPLENR; \
  1011. else \
  1012. maxttemp_raw[NR] += OVERSAMPLENR; \
  1013. }
  1014. #ifdef HEATER_0_MINTEMP
  1015. TEMP_MIN_ROUTINE(0);
  1016. #endif
  1017. #ifdef HEATER_0_MAXTEMP
  1018. TEMP_MAX_ROUTINE(0);
  1019. #endif
  1020. #if HOTENDS > 1
  1021. #ifdef HEATER_1_MINTEMP
  1022. TEMP_MIN_ROUTINE(1);
  1023. #endif
  1024. #ifdef HEATER_1_MAXTEMP
  1025. TEMP_MAX_ROUTINE(1);
  1026. #endif
  1027. #if HOTENDS > 2
  1028. #ifdef HEATER_2_MINTEMP
  1029. TEMP_MIN_ROUTINE(2);
  1030. #endif
  1031. #ifdef HEATER_2_MAXTEMP
  1032. TEMP_MAX_ROUTINE(2);
  1033. #endif
  1034. #if HOTENDS > 3
  1035. #ifdef HEATER_3_MINTEMP
  1036. TEMP_MIN_ROUTINE(3);
  1037. #endif
  1038. #ifdef HEATER_3_MAXTEMP
  1039. TEMP_MAX_ROUTINE(3);
  1040. #endif
  1041. #if HOTENDS > 4
  1042. #ifdef HEATER_4_MINTEMP
  1043. TEMP_MIN_ROUTINE(4);
  1044. #endif
  1045. #ifdef HEATER_4_MAXTEMP
  1046. TEMP_MAX_ROUTINE(4);
  1047. #endif
  1048. #endif // HOTENDS > 4
  1049. #endif // HOTENDS > 3
  1050. #endif // HOTENDS > 2
  1051. #endif // HOTENDS > 1
  1052. #ifdef BED_MINTEMP
  1053. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1054. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1055. bed_minttemp_raw += OVERSAMPLENR;
  1056. #else
  1057. bed_minttemp_raw -= OVERSAMPLENR;
  1058. #endif
  1059. }
  1060. #endif // BED_MINTEMP
  1061. #ifdef BED_MAXTEMP
  1062. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1063. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1064. bed_maxttemp_raw -= OVERSAMPLENR;
  1065. #else
  1066. bed_maxttemp_raw += OVERSAMPLENR;
  1067. #endif
  1068. }
  1069. #endif // BED_MAXTEMP
  1070. #if ENABLED(PROBING_HEATERS_OFF)
  1071. paused = false;
  1072. #endif
  1073. }
  1074. #if WATCH_HOTENDS
  1075. /**
  1076. * Start Heating Sanity Check for hotends that are below
  1077. * their target temperature by a configurable margin.
  1078. * This is called when the temperature is set. (M104, M109)
  1079. */
  1080. void Temperature::start_watching_heater(uint8_t e) {
  1081. #if HOTENDS == 1
  1082. UNUSED(e);
  1083. #endif
  1084. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1085. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1086. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1087. }
  1088. else
  1089. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1090. }
  1091. #endif
  1092. #if WATCH_THE_BED
  1093. /**
  1094. * Start Heating Sanity Check for hotends that are below
  1095. * their target temperature by a configurable margin.
  1096. * This is called when the temperature is set. (M140, M190)
  1097. */
  1098. void Temperature::start_watching_bed() {
  1099. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1100. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1101. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1102. }
  1103. else
  1104. watch_bed_next_ms = 0;
  1105. }
  1106. #endif
  1107. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1108. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1109. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1110. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1111. #endif
  1112. #if HAS_THERMALLY_PROTECTED_BED
  1113. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1114. millis_t Temperature::thermal_runaway_bed_timer;
  1115. #endif
  1116. void Temperature::thermal_runaway_protection(Temperature::TRState* state, millis_t* timer, float current, float target, int heater_id, int period_seconds, int hysteresis_degc) {
  1117. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1118. /**
  1119. SERIAL_ECHO_START;
  1120. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1121. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1122. SERIAL_ECHOPAIR(" ; State:", *state);
  1123. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1124. SERIAL_ECHOPAIR(" ; Temperature:", current);
  1125. SERIAL_ECHOPAIR(" ; Target Temp:", target);
  1126. if (heater_id >= 0)
  1127. SERIAL_ECHOPAIR(" ; Idle Timeout:", heater_idle_timeout_exceeded[heater_id]);
  1128. else
  1129. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle_timeout_exceeded);
  1130. SERIAL_EOL;
  1131. */
  1132. int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1133. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  1134. // If the heater idle timeout expires, restart
  1135. if (heater_id >= 0 && heater_idle_timeout_exceeded[heater_id]) {
  1136. *state = TRInactive;
  1137. tr_target_temperature[heater_index] = 0;
  1138. }
  1139. #if HAS_TEMP_BED
  1140. else if (heater_id < 0 && bed_idle_timeout_exceeded) {
  1141. *state = TRInactive;
  1142. tr_target_temperature[heater_index] = 0;
  1143. }
  1144. #endif
  1145. else
  1146. #endif
  1147. // If the target temperature changes, restart
  1148. if (tr_target_temperature[heater_index] != target) {
  1149. tr_target_temperature[heater_index] = target;
  1150. *state = target > 0 ? TRFirstHeating : TRInactive;
  1151. }
  1152. switch (*state) {
  1153. // Inactive state waits for a target temperature to be set
  1154. case TRInactive: break;
  1155. // When first heating, wait for the temperature to be reached then go to Stable state
  1156. case TRFirstHeating:
  1157. if (current < tr_target_temperature[heater_index]) break;
  1158. *state = TRStable;
  1159. // While the temperature is stable watch for a bad temperature
  1160. case TRStable:
  1161. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1162. *timer = millis() + period_seconds * 1000UL;
  1163. break;
  1164. }
  1165. else if (PENDING(millis(), *timer)) break;
  1166. *state = TRRunaway;
  1167. case TRRunaway:
  1168. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1169. }
  1170. }
  1171. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1172. void Temperature::disable_all_heaters() {
  1173. #if ENABLED(AUTOTEMP)
  1174. planner.autotemp_enabled = false;
  1175. #endif
  1176. HOTEND_LOOP() setTargetHotend(0, e);
  1177. setTargetBed(0);
  1178. // Unpause and reset everything
  1179. #if ENABLED(PROBING_HEATERS_OFF)
  1180. pause(false);
  1181. #endif
  1182. // If all heaters go down then for sure our print job has stopped
  1183. print_job_timer.stop();
  1184. #define DISABLE_HEATER(NR) { \
  1185. setTargetHotend(0, NR); \
  1186. soft_pwm_amount[NR] = 0; \
  1187. WRITE_HEATER_ ##NR (LOW); \
  1188. }
  1189. #if HAS_TEMP_HOTEND
  1190. DISABLE_HEATER(0);
  1191. #if HOTENDS > 1
  1192. DISABLE_HEATER(1);
  1193. #if HOTENDS > 2
  1194. DISABLE_HEATER(2);
  1195. #if HOTENDS > 3
  1196. DISABLE_HEATER(3);
  1197. #if HOTENDS > 4
  1198. DISABLE_HEATER(4);
  1199. #endif // HOTENDS > 4
  1200. #endif // HOTENDS > 3
  1201. #endif // HOTENDS > 2
  1202. #endif // HOTENDS > 1
  1203. #endif
  1204. #if HAS_TEMP_BED
  1205. target_temperature_bed = 0;
  1206. soft_pwm_amount_bed = 0;
  1207. #if HAS_HEATER_BED
  1208. WRITE_HEATER_BED(LOW);
  1209. #endif
  1210. #endif
  1211. }
  1212. #if ENABLED(PROBING_HEATERS_OFF)
  1213. void Temperature::pause(const bool p) {
  1214. if (p != paused) {
  1215. paused = p;
  1216. if (p) {
  1217. HOTEND_LOOP() start_heater_idle_timer(e, 0); // timeout immediately
  1218. #if HAS_TEMP_BED
  1219. start_bed_idle_timer(0); // timeout immediately
  1220. #endif
  1221. }
  1222. else {
  1223. HOTEND_LOOP() reset_heater_idle_timer(e);
  1224. #if HAS_TEMP_BED
  1225. reset_bed_idle_timer();
  1226. #endif
  1227. }
  1228. }
  1229. }
  1230. #endif // PROBING_HEATERS_OFF
  1231. #if ENABLED(HEATER_0_USES_MAX6675)
  1232. #define MAX6675_HEAT_INTERVAL 250u
  1233. #if ENABLED(MAX6675_IS_MAX31855)
  1234. uint32_t max6675_temp = 2000;
  1235. #define MAX6675_ERROR_MASK 7
  1236. #define MAX6675_DISCARD_BITS 18
  1237. #define MAX6675_SPEED_BITS (_BV(SPR1)) // clock ÷ 64
  1238. #else
  1239. uint16_t max6675_temp = 2000;
  1240. #define MAX6675_ERROR_MASK 4
  1241. #define MAX6675_DISCARD_BITS 3
  1242. #define MAX6675_SPEED_BITS (_BV(SPR0)) // clock ÷ 16
  1243. #endif
  1244. int Temperature::read_max6675() {
  1245. static millis_t next_max6675_ms = 0;
  1246. millis_t ms = millis();
  1247. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1248. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1249. CBI(
  1250. #ifdef PRR
  1251. PRR
  1252. #elif defined(PRR0)
  1253. PRR0
  1254. #endif
  1255. , PRSPI);
  1256. SPCR = _BV(MSTR) | _BV(SPE) | MAX6675_SPEED_BITS;
  1257. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1258. // ensure 100ns delay - a bit extra is fine
  1259. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1260. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1261. // Read a big-endian temperature value
  1262. max6675_temp = 0;
  1263. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1264. max6675_temp |= max6675_spi.receive();
  1265. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1266. }
  1267. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1268. if (max6675_temp & MAX6675_ERROR_MASK) {
  1269. SERIAL_ERROR_START;
  1270. SERIAL_ERRORPGM("Temp measurement error! ");
  1271. #if MAX6675_ERROR_MASK == 7
  1272. SERIAL_ERRORPGM("MAX31855 ");
  1273. if (max6675_temp & 1)
  1274. SERIAL_ERRORLNPGM("Open Circuit");
  1275. else if (max6675_temp & 2)
  1276. SERIAL_ERRORLNPGM("Short to GND");
  1277. else if (max6675_temp & 4)
  1278. SERIAL_ERRORLNPGM("Short to VCC");
  1279. #else
  1280. SERIAL_ERRORLNPGM("MAX6675");
  1281. #endif
  1282. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1283. }
  1284. else
  1285. max6675_temp >>= MAX6675_DISCARD_BITS;
  1286. #if ENABLED(MAX6675_IS_MAX31855)
  1287. // Support negative temperature
  1288. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000;
  1289. #endif
  1290. return (int)max6675_temp;
  1291. }
  1292. #endif // HEATER_0_USES_MAX6675
  1293. /**
  1294. * Get raw temperatures
  1295. */
  1296. void Temperature::set_current_temp_raw() {
  1297. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1298. current_temperature_raw[0] = raw_temp_value[0];
  1299. #endif
  1300. #if HAS_TEMP_1
  1301. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1302. redundant_temperature_raw = raw_temp_value[1];
  1303. #else
  1304. current_temperature_raw[1] = raw_temp_value[1];
  1305. #endif
  1306. #if HAS_TEMP_2
  1307. current_temperature_raw[2] = raw_temp_value[2];
  1308. #if HAS_TEMP_3
  1309. current_temperature_raw[3] = raw_temp_value[3];
  1310. #if HAS_TEMP_4
  1311. current_temperature_raw[4] = raw_temp_value[4];
  1312. #endif
  1313. #endif
  1314. #endif
  1315. #endif
  1316. current_temperature_bed_raw = raw_temp_bed_value;
  1317. temp_meas_ready = true;
  1318. }
  1319. #if ENABLED(PINS_DEBUGGING)
  1320. /**
  1321. * monitors endstops & Z probe for changes
  1322. *
  1323. * If a change is detected then the LED is toggled and
  1324. * a message is sent out the serial port
  1325. *
  1326. * Yes, we could miss a rapid back & forth change but
  1327. * that won't matter because this is all manual.
  1328. *
  1329. */
  1330. void endstop_monitor() {
  1331. static uint16_t old_endstop_bits_local = 0;
  1332. static uint8_t local_LED_status = 0;
  1333. uint16_t current_endstop_bits_local = 0;
  1334. #if HAS_X_MIN
  1335. if (READ(X_MIN_PIN)) SBI(current_endstop_bits_local, X_MIN);
  1336. #endif
  1337. #if HAS_X_MAX
  1338. if (READ(X_MAX_PIN)) SBI(current_endstop_bits_local, X_MAX);
  1339. #endif
  1340. #if HAS_Y_MIN
  1341. if (READ(Y_MIN_PIN)) SBI(current_endstop_bits_local, Y_MIN);
  1342. #endif
  1343. #if HAS_Y_MAX
  1344. if (READ(Y_MAX_PIN)) SBI(current_endstop_bits_local, Y_MAX);
  1345. #endif
  1346. #if HAS_Z_MIN
  1347. if (READ(Z_MIN_PIN)) SBI(current_endstop_bits_local, Z_MIN);
  1348. #endif
  1349. #if HAS_Z_MAX
  1350. if (READ(Z_MAX_PIN)) SBI(current_endstop_bits_local, Z_MAX);
  1351. #endif
  1352. #if HAS_Z_MIN_PROBE_PIN
  1353. if (READ(Z_MIN_PROBE_PIN)) SBI(current_endstop_bits_local, Z_MIN_PROBE);
  1354. #endif
  1355. #if HAS_Z2_MIN
  1356. if (READ(Z2_MIN_PIN)) SBI(current_endstop_bits_local, Z2_MIN);
  1357. #endif
  1358. #if HAS_Z2_MAX
  1359. if (READ(Z2_MAX_PIN)) SBI(current_endstop_bits_local, Z2_MAX);
  1360. #endif
  1361. uint16_t endstop_change = current_endstop_bits_local ^ old_endstop_bits_local;
  1362. if (endstop_change) {
  1363. #if HAS_X_MIN
  1364. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR("X_MIN:", !!TEST(current_endstop_bits_local, X_MIN));
  1365. #endif
  1366. #if HAS_X_MAX
  1367. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", !!TEST(current_endstop_bits_local, X_MAX));
  1368. #endif
  1369. #if HAS_Y_MIN
  1370. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", !!TEST(current_endstop_bits_local, Y_MIN));
  1371. #endif
  1372. #if HAS_Y_MAX
  1373. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", !!TEST(current_endstop_bits_local, Y_MAX));
  1374. #endif
  1375. #if HAS_Z_MIN
  1376. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", !!TEST(current_endstop_bits_local, Z_MIN));
  1377. #endif
  1378. #if HAS_Z_MAX
  1379. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", !!TEST(current_endstop_bits_local, Z_MAX));
  1380. #endif
  1381. #if HAS_Z_MIN_PROBE_PIN
  1382. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", !!TEST(current_endstop_bits_local, Z_MIN_PROBE));
  1383. #endif
  1384. #if HAS_Z2_MIN
  1385. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", !!TEST(current_endstop_bits_local, Z2_MIN));
  1386. #endif
  1387. #if HAS_Z2_MAX
  1388. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", !!TEST(current_endstop_bits_local, Z2_MAX));
  1389. #endif
  1390. SERIAL_PROTOCOLPGM("\n\n");
  1391. analogWrite(LED_PIN, local_LED_status);
  1392. local_LED_status ^= 255;
  1393. old_endstop_bits_local = current_endstop_bits_local;
  1394. }
  1395. }
  1396. #endif // PINS_DEBUGGING
  1397. /**
  1398. * Timer 0 is shared with millies so don't change the prescaler.
  1399. *
  1400. * This ISR uses the compare method so it runs at the base
  1401. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1402. * in OCR0B above (128 or halfway between OVFs).
  1403. *
  1404. * - Manage PWM to all the heaters and fan
  1405. * - Prepare or Measure one of the raw ADC sensor values
  1406. * - Check new temperature values for MIN/MAX errors (kill on error)
  1407. * - Step the babysteps value for each axis towards 0
  1408. * - For PINS_DEBUGGING, monitor and report endstop pins
  1409. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1410. */
  1411. ISR(TIMER0_COMPB_vect) { Temperature::isr(); }
  1412. volatile bool Temperature::in_temp_isr = false;
  1413. void Temperature::isr() {
  1414. // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR
  1415. // at the end of its run, potentially causing re-entry. This flag prevents it.
  1416. if (in_temp_isr) return;
  1417. in_temp_isr = true;
  1418. // Allow UART and stepper ISRs
  1419. CBI(TIMSK0, OCIE0B); //Disable Temperature ISR
  1420. sei();
  1421. static int8_t temp_count = -1;
  1422. static ADCSensorState adc_sensor_state = StartupDelay;
  1423. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1424. // avoid multiple loads of pwm_count
  1425. uint8_t pwm_count_tmp = pwm_count;
  1426. // Static members for each heater
  1427. #if ENABLED(SLOW_PWM_HEATERS)
  1428. static uint8_t slow_pwm_count = 0;
  1429. #define ISR_STATICS(n) \
  1430. static uint8_t soft_pwm_count_ ## n, \
  1431. state_heater_ ## n = 0, \
  1432. state_timer_heater_ ## n = 0
  1433. #else
  1434. #define ISR_STATICS(n) static uint8_t soft_pwm_count_ ## n = 0
  1435. #endif
  1436. // Statics per heater
  1437. ISR_STATICS(0);
  1438. #if HOTENDS > 1
  1439. ISR_STATICS(1);
  1440. #if HOTENDS > 2
  1441. ISR_STATICS(2);
  1442. #if HOTENDS > 3
  1443. ISR_STATICS(3);
  1444. #if HOTENDS > 4
  1445. ISR_STATICS(4);
  1446. #endif // HOTENDS > 4
  1447. #endif // HOTENDS > 3
  1448. #endif // HOTENDS > 2
  1449. #endif // HOTENDS > 1
  1450. #if HAS_HEATER_BED
  1451. ISR_STATICS(BED);
  1452. #endif
  1453. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1454. static unsigned long raw_filwidth_value = 0;
  1455. #endif
  1456. #if DISABLED(SLOW_PWM_HEATERS)
  1457. constexpr uint8_t pwm_mask =
  1458. #if ENABLED(SOFT_PWM_DITHER)
  1459. _BV(SOFT_PWM_SCALE) - 1
  1460. #else
  1461. 0
  1462. #endif
  1463. ;
  1464. /**
  1465. * Standard PWM modulation
  1466. */
  1467. if (pwm_count_tmp >= 127) {
  1468. pwm_count_tmp -= 127;
  1469. soft_pwm_count_0 = (soft_pwm_count_0 & pwm_mask) + soft_pwm_amount[0];
  1470. WRITE_HEATER_0(soft_pwm_count_0 > pwm_mask ? HIGH : LOW);
  1471. #if HOTENDS > 1
  1472. soft_pwm_count_1 = (soft_pwm_count_1 & pwm_mask) + soft_pwm_amount[1];
  1473. WRITE_HEATER_1(soft_pwm_count_1 > pwm_mask ? HIGH : LOW);
  1474. #if HOTENDS > 2
  1475. soft_pwm_count_2 = (soft_pwm_count_2 & pwm_mask) + soft_pwm_amount[2];
  1476. WRITE_HEATER_2(soft_pwm_count_2 > pwm_mask ? HIGH : LOW);
  1477. #if HOTENDS > 3
  1478. soft_pwm_count_3 = (soft_pwm_count_3 & pwm_mask) + soft_pwm_amount[3];
  1479. WRITE_HEATER_3(soft_pwm_count_3 > pwm_mask ? HIGH : LOW);
  1480. #if HOTENDS > 4
  1481. soft_pwm_count_4 = (soft_pwm_count_4 & pwm_mask) + soft_pwm_amount[4];
  1482. WRITE_HEATER_4(soft_pwm_count_4 > pwm_mask ? HIGH : LOW);
  1483. #endif // HOTENDS > 4
  1484. #endif // HOTENDS > 3
  1485. #endif // HOTENDS > 2
  1486. #endif // HOTENDS > 1
  1487. #if HAS_HEATER_BED
  1488. soft_pwm_count_BED = (soft_pwm_count_BED & pwm_mask) + soft_pwm_amount_bed;
  1489. WRITE_HEATER_BED(soft_pwm_count_BED > pwm_mask ? HIGH : LOW);
  1490. #endif
  1491. #if ENABLED(FAN_SOFT_PWM)
  1492. #if HAS_FAN0
  1493. soft_pwm_count_fan[0] = (soft_pwm_count_fan[0] & pwm_mask) + soft_pwm_amount_fan[0] >> 1;
  1494. WRITE_FAN(soft_pwm_count_fan[0] > pwm_mask ? HIGH : LOW);
  1495. #endif
  1496. #if HAS_FAN1
  1497. soft_pwm_count_fan[1] = (soft_pwm_count_fan[1] & pwm_mask) + soft_pwm_amount_fan[1] >> 1;
  1498. WRITE_FAN1(soft_pwm_count_fan[1] > pwm_mask ? HIGH : LOW);
  1499. #endif
  1500. #if HAS_FAN2
  1501. soft_pwm_count_fan[2] = (soft_pwm_count_fan[2] & pwm_mask) + soft_pwm_amount_fan[2] >> 1;
  1502. WRITE_FAN2(soft_pwm_count_fan[2] > pwm_mask ? HIGH : LOW);
  1503. #endif
  1504. #endif
  1505. }
  1506. else {
  1507. if (soft_pwm_count_0 <= pwm_count_tmp) WRITE_HEATER_0(0);
  1508. #if HOTENDS > 1
  1509. if (soft_pwm_count_1 <= pwm_count_tmp) WRITE_HEATER_1(0);
  1510. #if HOTENDS > 2
  1511. if (soft_pwm_count_2 <= pwm_count_tmp) WRITE_HEATER_2(0);
  1512. #if HOTENDS > 3
  1513. if (soft_pwm_count_3 <= pwm_count_tmp) WRITE_HEATER_3(0);
  1514. #if HOTENDS > 4
  1515. if (soft_pwm_count_4 <= pwm_count_tmp) WRITE_HEATER_4(0);
  1516. #endif // HOTENDS > 4
  1517. #endif // HOTENDS > 3
  1518. #endif // HOTENDS > 2
  1519. #endif // HOTENDS > 1
  1520. #if HAS_HEATER_BED
  1521. if (soft_pwm_count_BED <= pwm_count_tmp) WRITE_HEATER_BED(0);
  1522. #endif
  1523. #if ENABLED(FAN_SOFT_PWM)
  1524. #if HAS_FAN0
  1525. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1526. #endif
  1527. #if HAS_FAN1
  1528. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1529. #endif
  1530. #if HAS_FAN2
  1531. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1532. #endif
  1533. #endif
  1534. }
  1535. // SOFT_PWM_SCALE to frequency:
  1536. //
  1537. // 0: 16000000/64/256/128 = 7.6294 Hz
  1538. // 1: / 64 = 15.2588 Hz
  1539. // 2: / 32 = 30.5176 Hz
  1540. // 3: / 16 = 61.0352 Hz
  1541. // 4: / 8 = 122.0703 Hz
  1542. // 5: / 4 = 244.1406 Hz
  1543. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1544. #else // SLOW_PWM_HEATERS
  1545. /**
  1546. * SLOW PWM HEATERS
  1547. *
  1548. * For relay-driven heaters
  1549. */
  1550. #ifndef MIN_STATE_TIME
  1551. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1552. #endif
  1553. // Macros for Slow PWM timer logic
  1554. #define _SLOW_PWM_ROUTINE(NR, src) \
  1555. soft_pwm_ ##NR = src; \
  1556. if (soft_pwm_ ##NR > 0) { \
  1557. if (state_timer_heater_ ##NR == 0) { \
  1558. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1559. state_heater_ ##NR = 1; \
  1560. WRITE_HEATER_ ##NR(1); \
  1561. } \
  1562. } \
  1563. else { \
  1564. if (state_timer_heater_ ##NR == 0) { \
  1565. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1566. state_heater_ ##NR = 0; \
  1567. WRITE_HEATER_ ##NR(0); \
  1568. } \
  1569. }
  1570. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm_amount[n])
  1571. #define PWM_OFF_ROUTINE(NR) \
  1572. if (soft_pwm_ ##NR < slow_pwm_count) { \
  1573. if (state_timer_heater_ ##NR == 0) { \
  1574. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1575. state_heater_ ##NR = 0; \
  1576. WRITE_HEATER_ ##NR (0); \
  1577. } \
  1578. }
  1579. if (slow_pwm_count == 0) {
  1580. SLOW_PWM_ROUTINE(0);
  1581. #if HOTENDS > 1
  1582. SLOW_PWM_ROUTINE(1);
  1583. #if HOTENDS > 2
  1584. SLOW_PWM_ROUTINE(2);
  1585. #if HOTENDS > 3
  1586. SLOW_PWM_ROUTINE(3);
  1587. #if HOTENDS > 4
  1588. SLOW_PWM_ROUTINE(4);
  1589. #endif // HOTENDS > 4
  1590. #endif // HOTENDS > 3
  1591. #endif // HOTENDS > 2
  1592. #endif // HOTENDS > 1
  1593. #if HAS_HEATER_BED
  1594. _SLOW_PWM_ROUTINE(BED, soft_pwm_amount_bed); // BED
  1595. #endif
  1596. } // slow_pwm_count == 0
  1597. PWM_OFF_ROUTINE(0);
  1598. #if HOTENDS > 1
  1599. PWM_OFF_ROUTINE(1);
  1600. #if HOTENDS > 2
  1601. PWM_OFF_ROUTINE(2);
  1602. #if HOTENDS > 3
  1603. PWM_OFF_ROUTINE(3);
  1604. #if HOTENDS > 4
  1605. PWM_OFF_ROUTINE(4);
  1606. #endif // HOTENDS > 4
  1607. #endif // HOTENDS > 3
  1608. #endif // HOTENDS > 2
  1609. #endif // HOTENDS > 1
  1610. #if HAS_HEATER_BED
  1611. PWM_OFF_ROUTINE(BED); // BED
  1612. #endif
  1613. #if ENABLED(FAN_SOFT_PWM)
  1614. if (pwm_count_tmp >= 127) {
  1615. pwm_count_tmp = 0;
  1616. #if HAS_FAN0
  1617. soft_pwm_count_fan[0] = soft_pwm_amount_fan[0] >> 1;
  1618. WRITE_FAN(soft_pwm_count_fan[0] > 0 ? HIGH : LOW);
  1619. #endif
  1620. #if HAS_FAN1
  1621. soft_pwm_count_fan[1] = soft_pwm_amount_fan[1] >> 1;
  1622. WRITE_FAN1(soft_pwm_count_fan[1] > 0 ? HIGH : LOW);
  1623. #endif
  1624. #if HAS_FAN2
  1625. soft_pwm_count_fan[2] = soft_pwm_amount_fan[2] >> 1;
  1626. WRITE_FAN2(soft_pwm_count_fan[2] > 0 ? HIGH : LOW);
  1627. #endif
  1628. }
  1629. #if HAS_FAN0
  1630. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1631. #endif
  1632. #if HAS_FAN1
  1633. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1634. #endif
  1635. #if HAS_FAN2
  1636. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1637. #endif
  1638. #endif // FAN_SOFT_PWM
  1639. // SOFT_PWM_SCALE to frequency:
  1640. //
  1641. // 0: 16000000/64/256/128 = 7.6294 Hz
  1642. // 1: / 64 = 15.2588 Hz
  1643. // 2: / 32 = 30.5176 Hz
  1644. // 3: / 16 = 61.0352 Hz
  1645. // 4: / 8 = 122.0703 Hz
  1646. // 5: / 4 = 244.1406 Hz
  1647. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1648. // increment slow_pwm_count only every 64th pwm_count,
  1649. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1650. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1651. slow_pwm_count++;
  1652. slow_pwm_count &= 0x7F;
  1653. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1654. #if HOTENDS > 1
  1655. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1656. #if HOTENDS > 2
  1657. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1658. #if HOTENDS > 3
  1659. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1660. #if HOTENDS > 4
  1661. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1662. #endif // HOTENDS > 4
  1663. #endif // HOTENDS > 3
  1664. #endif // HOTENDS > 2
  1665. #endif // HOTENDS > 1
  1666. #if HAS_HEATER_BED
  1667. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1668. #endif
  1669. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1670. #endif // SLOW_PWM_HEATERS
  1671. //
  1672. // Update lcd buttons 488 times per second
  1673. //
  1674. static bool do_buttons;
  1675. if ((do_buttons ^= true)) lcd_buttons_update();
  1676. /**
  1677. * One sensor is sampled on every other call of the ISR.
  1678. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1679. *
  1680. * On each Prepare pass, ADC is started for a sensor pin.
  1681. * On the next pass, the ADC value is read and accumulated.
  1682. *
  1683. * This gives each ADC 0.9765ms to charge up.
  1684. */
  1685. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1686. #ifdef MUX5
  1687. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1688. #else
  1689. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1690. #endif
  1691. switch (adc_sensor_state) {
  1692. case SensorsReady: {
  1693. // All sensors have been read. Stay in this state for a few
  1694. // ISRs to save on calls to temp update/checking code below.
  1695. constexpr int extra_loops = MIN_ADC_ISR_LOOPS - (int)SensorsReady;
  1696. static uint8_t delay_count = 0;
  1697. if (extra_loops > 0) {
  1698. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1699. if (--delay_count) // While delaying...
  1700. adc_sensor_state = (ADCSensorState)(int(SensorsReady) - 1); // retain this state (else, next state will be 0)
  1701. break;
  1702. }
  1703. else
  1704. adc_sensor_state = (ADCSensorState)0; // Fall-through to start first sensor now
  1705. }
  1706. #if HAS_TEMP_0
  1707. case PrepareTemp_0:
  1708. START_ADC(TEMP_0_PIN);
  1709. break;
  1710. case MeasureTemp_0:
  1711. raw_temp_value[0] += ADC;
  1712. break;
  1713. #endif
  1714. #if HAS_TEMP_BED
  1715. case PrepareTemp_BED:
  1716. START_ADC(TEMP_BED_PIN);
  1717. break;
  1718. case MeasureTemp_BED:
  1719. raw_temp_bed_value += ADC;
  1720. break;
  1721. #endif
  1722. #if HAS_TEMP_1
  1723. case PrepareTemp_1:
  1724. START_ADC(TEMP_1_PIN);
  1725. break;
  1726. case MeasureTemp_1:
  1727. raw_temp_value[1] += ADC;
  1728. break;
  1729. #endif
  1730. #if HAS_TEMP_2
  1731. case PrepareTemp_2:
  1732. START_ADC(TEMP_2_PIN);
  1733. break;
  1734. case MeasureTemp_2:
  1735. raw_temp_value[2] += ADC;
  1736. break;
  1737. #endif
  1738. #if HAS_TEMP_3
  1739. case PrepareTemp_3:
  1740. START_ADC(TEMP_3_PIN);
  1741. break;
  1742. case MeasureTemp_3:
  1743. raw_temp_value[3] += ADC;
  1744. break;
  1745. #endif
  1746. #if HAS_TEMP_4
  1747. case PrepareTemp_4:
  1748. START_ADC(TEMP_4_PIN);
  1749. break;
  1750. case MeasureTemp_4:
  1751. raw_temp_value[4] += ADC;
  1752. break;
  1753. #endif
  1754. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1755. case Prepare_FILWIDTH:
  1756. START_ADC(FILWIDTH_PIN);
  1757. break;
  1758. case Measure_FILWIDTH:
  1759. if (ADC > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1760. raw_filwidth_value -= (raw_filwidth_value >> 7); // Subtract 1/128th of the raw_filwidth_value
  1761. raw_filwidth_value += ((unsigned long)ADC << 7); // Add new ADC reading, scaled by 128
  1762. }
  1763. break;
  1764. #endif
  1765. case StartupDelay: break;
  1766. } // switch(adc_sensor_state)
  1767. if (!adc_sensor_state && ++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1768. temp_count = 0;
  1769. // Update the raw values if they've been read. Else we could be updating them during reading.
  1770. if (!temp_meas_ready) set_current_temp_raw();
  1771. // Filament Sensor - can be read any time since IIR filtering is used
  1772. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1773. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1774. #endif
  1775. ZERO(raw_temp_value);
  1776. raw_temp_bed_value = 0;
  1777. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1778. int constexpr temp_dir[] = {
  1779. #if ENABLED(HEATER_0_USES_MAX6675)
  1780. 0
  1781. #else
  1782. TEMPDIR(0)
  1783. #endif
  1784. #if HOTENDS > 1
  1785. , TEMPDIR(1)
  1786. #if HOTENDS > 2
  1787. , TEMPDIR(2)
  1788. #if HOTENDS > 3
  1789. , TEMPDIR(3)
  1790. #if HOTENDS > 4
  1791. , TEMPDIR(4)
  1792. #endif // HOTENDS > 4
  1793. #endif // HOTENDS > 3
  1794. #endif // HOTENDS > 2
  1795. #endif // HOTENDS > 1
  1796. };
  1797. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1798. const int16_t tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1799. if (rawtemp > maxttemp_raw[e] * tdir && target_temperature[e] > 0) max_temp_error(e);
  1800. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && target_temperature[e] > 0) {
  1801. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1802. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1803. #endif
  1804. min_temp_error(e);
  1805. }
  1806. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1807. else
  1808. consecutive_low_temperature_error[e] = 0;
  1809. #endif
  1810. }
  1811. #if HAS_TEMP_BED
  1812. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1813. #define GEBED <=
  1814. #else
  1815. #define GEBED >=
  1816. #endif
  1817. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && target_temperature_bed > 0) max_temp_error(-1);
  1818. if (bed_minttemp_raw GEBED current_temperature_bed_raw && target_temperature_bed > 0) min_temp_error(-1);
  1819. #endif
  1820. } // temp_count >= OVERSAMPLENR
  1821. // Go to the next state, up to SensorsReady
  1822. adc_sensor_state = (ADCSensorState)((int(adc_sensor_state) + 1) % int(StartupDelay));
  1823. #if ENABLED(BABYSTEPPING)
  1824. LOOP_XYZ(axis) {
  1825. const int curTodo = babystepsTodo[axis]; // get rid of volatile for performance
  1826. if (curTodo > 0) {
  1827. stepper.babystep((AxisEnum)axis, /*fwd*/true);
  1828. babystepsTodo[axis]--;
  1829. }
  1830. else if (curTodo < 0) {
  1831. stepper.babystep((AxisEnum)axis, /*fwd*/false);
  1832. babystepsTodo[axis]++;
  1833. }
  1834. }
  1835. #endif // BABYSTEPPING
  1836. #if ENABLED(PINS_DEBUGGING)
  1837. extern bool endstop_monitor_flag;
  1838. // run the endstop monitor at 15Hz
  1839. static uint8_t endstop_monitor_count = 16; // offset this check from the others
  1840. if (endstop_monitor_flag) {
  1841. endstop_monitor_count += _BV(1); // 15 Hz
  1842. endstop_monitor_count &= 0x7F;
  1843. if (!endstop_monitor_count) endstop_monitor(); // report changes in endstop status
  1844. }
  1845. #endif
  1846. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1847. extern volatile uint8_t e_hit;
  1848. if (e_hit && ENDSTOPS_ENABLED) {
  1849. endstops.update(); // call endstop update routine
  1850. e_hit--;
  1851. }
  1852. #endif
  1853. cli();
  1854. in_temp_isr = false;
  1855. SBI(TIMSK0, OCIE0B); //re-enable Temperature ISR
  1856. }