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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. Part of Marlin
  25. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  26. This program is free software: you can redistribute it and/or modify
  27. it under the terms of the GNU General Public License as published by
  28. the Free Software Foundation, either version 3 of the License, or
  29. (at your option) any later version.
  30. This program is distributed in the hope that it will be useful,
  31. but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. GNU General Public License for more details.
  34. You should have received a copy of the GNU General Public License
  35. along with this program. If not, see <http://www.gnu.org/licenses/>.
  36. */
  37. #include "Marlin.h"
  38. #include "ultralcd.h"
  39. #include "temperature.h"
  40. #include "language.h"
  41. #include "Sd2PinMap.h"
  42. #if ENABLED(USE_WATCHDOG)
  43. #include "watchdog.h"
  44. #endif
  45. //===========================================================================
  46. //================================== macros =================================
  47. //===========================================================================
  48. #ifdef K1 // Defined in Configuration.h in the PID settings
  49. #define K2 (1.0-K1)
  50. #endif
  51. #if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMP)
  52. #define PID_dT ((OVERSAMPLENR * 12.0)/(F_CPU / 64.0 / 256.0))
  53. #endif
  54. //===========================================================================
  55. //============================= public variables ============================
  56. //===========================================================================
  57. int target_temperature[4] = { 0 };
  58. int target_temperature_bed = 0;
  59. int current_temperature_raw[4] = { 0 };
  60. float current_temperature[4] = { 0.0 };
  61. int current_temperature_bed_raw = 0;
  62. float current_temperature_bed = 0.0;
  63. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  64. int redundant_temperature_raw = 0;
  65. float redundant_temperature = 0.0;
  66. #endif
  67. #if ENABLED(PIDTEMPBED)
  68. float bedKp = DEFAULT_bedKp;
  69. float bedKi = (DEFAULT_bedKi* PID_dT);
  70. float bedKd = (DEFAULT_bedKd / PID_dT);
  71. #endif //PIDTEMPBED
  72. #if ENABLED(FAN_SOFT_PWM)
  73. unsigned char fanSpeedSoftPwm[FAN_COUNT];
  74. #endif
  75. unsigned char soft_pwm_bed;
  76. #if ENABLED(BABYSTEPPING)
  77. volatile int babystepsTodo[3] = { 0 };
  78. #endif
  79. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  80. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  81. #endif
  82. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED)
  83. enum TRState { TRReset, TRInactive, TRFirstHeating, TRStable, TRRunaway };
  84. void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
  85. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  86. static TRState thermal_runaway_state_machine[4] = { TRReset, TRReset, TRReset, TRReset };
  87. static millis_t thermal_runaway_timer[4]; // = {0,0,0,0};
  88. #endif
  89. #if ENABLED(THERMAL_PROTECTION_BED) && TEMP_SENSOR_BED != 0
  90. static TRState thermal_runaway_bed_state_machine = TRReset;
  91. static millis_t thermal_runaway_bed_timer;
  92. #endif
  93. #endif
  94. //===========================================================================
  95. //============================ private variables ============================
  96. //===========================================================================
  97. static volatile bool temp_meas_ready = false;
  98. #if ENABLED(PIDTEMP)
  99. //static cannot be external:
  100. static float temp_iState[EXTRUDERS] = { 0 };
  101. static float temp_dState[EXTRUDERS] = { 0 };
  102. static float pTerm[EXTRUDERS];
  103. static float iTerm[EXTRUDERS];
  104. static float dTerm[EXTRUDERS];
  105. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  106. static float cTerm[EXTRUDERS];
  107. static long last_position[EXTRUDERS];
  108. static long lpq[LPQ_MAX_LEN];
  109. static int lpq_ptr = 0;
  110. #endif
  111. //int output;
  112. static float pid_error[EXTRUDERS];
  113. static float temp_iState_min[EXTRUDERS];
  114. static float temp_iState_max[EXTRUDERS];
  115. static bool pid_reset[EXTRUDERS];
  116. #endif //PIDTEMP
  117. #if ENABLED(PIDTEMPBED)
  118. //static cannot be external:
  119. static float temp_iState_bed = { 0 };
  120. static float temp_dState_bed = { 0 };
  121. static float pTerm_bed;
  122. static float iTerm_bed;
  123. static float dTerm_bed;
  124. //int output;
  125. static float pid_error_bed;
  126. static float temp_iState_min_bed;
  127. static float temp_iState_max_bed;
  128. #else //PIDTEMPBED
  129. static millis_t next_bed_check_ms;
  130. #endif //PIDTEMPBED
  131. static unsigned char soft_pwm[EXTRUDERS];
  132. #if ENABLED(FAN_SOFT_PWM)
  133. static unsigned char soft_pwm_fan[FAN_COUNT];
  134. #endif
  135. #if HAS_AUTO_FAN
  136. static millis_t next_auto_fan_check_ms;
  137. #endif
  138. #if ENABLED(PIDTEMP)
  139. #if ENABLED(PID_PARAMS_PER_EXTRUDER)
  140. float Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kp);
  141. float Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Ki) * (PID_dT));
  142. float Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Kd) / (PID_dT));
  143. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  144. float Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kc);
  145. #endif // PID_ADD_EXTRUSION_RATE
  146. #else //PID_PARAMS_PER_EXTRUDER
  147. float Kp = DEFAULT_Kp;
  148. float Ki = (DEFAULT_Ki) * (PID_dT);
  149. float Kd = (DEFAULT_Kd) / (PID_dT);
  150. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  151. float Kc = DEFAULT_Kc;
  152. #endif // PID_ADD_EXTRUSION_RATE
  153. #endif // PID_PARAMS_PER_EXTRUDER
  154. #endif //PIDTEMP
  155. // Init min and max temp with extreme values to prevent false errors during startup
  156. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  157. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  158. static int minttemp[EXTRUDERS] = { 0 };
  159. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(16383);
  160. #ifdef BED_MINTEMP
  161. static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  162. #endif
  163. #ifdef BED_MAXTEMP
  164. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  165. #endif
  166. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  167. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  168. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  169. #else
  170. static void* heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE);
  171. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN);
  172. #endif
  173. static float analog2temp(int raw, uint8_t e);
  174. static float analog2tempBed(int raw);
  175. static void updateTemperaturesFromRawValues();
  176. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  177. int watch_target_temp[EXTRUDERS] = { 0 };
  178. millis_t watch_heater_next_ms[EXTRUDERS] = { 0 };
  179. #endif
  180. #ifndef SOFT_PWM_SCALE
  181. #define SOFT_PWM_SCALE 0
  182. #endif
  183. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  184. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  185. #endif
  186. #if ENABLED(HEATER_0_USES_MAX6675)
  187. static int read_max6675();
  188. #endif
  189. //===========================================================================
  190. //================================ Functions ================================
  191. //===========================================================================
  192. #if HAS_PID_HEATING
  193. void PID_autotune(float temp, int extruder, int ncycles, bool set_result/*=false*/) {
  194. float input = 0.0;
  195. int cycles = 0;
  196. bool heating = true;
  197. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  198. long t_high = 0, t_low = 0;
  199. long bias, d;
  200. float Ku, Tu;
  201. float workKp = 0, workKi = 0, workKd = 0;
  202. float max = 0, min = 10000;
  203. #if HAS_AUTO_FAN
  204. millis_t next_auto_fan_check_ms = temp_ms + 2500UL;
  205. #endif
  206. if (false
  207. #if ENABLED(PIDTEMP)
  208. || extruder >= EXTRUDERS
  209. #else
  210. || extruder >= 0
  211. #endif
  212. #if DISABLED(PIDTEMPBED)
  213. || extruder < 0
  214. #endif
  215. ) {
  216. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  217. return;
  218. }
  219. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  220. disable_all_heaters(); // switch off all heaters.
  221. #if HAS_PID_FOR_BOTH
  222. if (extruder < 0)
  223. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  224. else
  225. soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
  226. #elif ENABLED(PIDTEMP)
  227. soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
  228. #else
  229. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  230. #endif
  231. // PID Tuning loop
  232. for (;;) {
  233. millis_t ms = millis();
  234. if (temp_meas_ready) { // temp sample ready
  235. updateTemperaturesFromRawValues();
  236. input =
  237. #if HAS_PID_FOR_BOTH
  238. extruder < 0 ? current_temperature_bed : current_temperature[extruder]
  239. #elif ENABLED(PIDTEMP)
  240. current_temperature[extruder]
  241. #else
  242. current_temperature_bed
  243. #endif
  244. ;
  245. max = max(max, input);
  246. min = min(min, input);
  247. #if HAS_AUTO_FAN
  248. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  249. checkExtruderAutoFans();
  250. next_auto_fan_check_ms = ms + 2500UL;
  251. }
  252. #endif
  253. if (heating && input > temp) {
  254. if (ELAPSED(ms, t2 + 5000UL)) {
  255. heating = false;
  256. #if HAS_PID_FOR_BOTH
  257. if (extruder < 0)
  258. soft_pwm_bed = (bias - d) >> 1;
  259. else
  260. soft_pwm[extruder] = (bias - d) >> 1;
  261. #elif ENABLED(PIDTEMP)
  262. soft_pwm[extruder] = (bias - d) >> 1;
  263. #elif ENABLED(PIDTEMPBED)
  264. soft_pwm_bed = (bias - d) >> 1;
  265. #endif
  266. t1 = ms;
  267. t_high = t1 - t2;
  268. max = temp;
  269. }
  270. }
  271. if (!heating && input < temp) {
  272. if (ELAPSED(ms, t1 + 5000UL)) {
  273. heating = true;
  274. t2 = ms;
  275. t_low = t2 - t1;
  276. if (cycles > 0) {
  277. long max_pow =
  278. #if HAS_PID_FOR_BOTH
  279. extruder < 0 ? MAX_BED_POWER : PID_MAX
  280. #elif ENABLED(PIDTEMP)
  281. PID_MAX
  282. #else
  283. MAX_BED_POWER
  284. #endif
  285. ;
  286. bias += (d * (t_high - t_low)) / (t_low + t_high);
  287. bias = constrain(bias, 20, max_pow - 20);
  288. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  289. SERIAL_PROTOCOLPGM(MSG_BIAS); SERIAL_PROTOCOL(bias);
  290. SERIAL_PROTOCOLPGM(MSG_D); SERIAL_PROTOCOL(d);
  291. SERIAL_PROTOCOLPGM(MSG_T_MIN); SERIAL_PROTOCOL(min);
  292. SERIAL_PROTOCOLPGM(MSG_T_MAX); SERIAL_PROTOCOLLN(max);
  293. if (cycles > 2) {
  294. Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
  295. Tu = ((float)(t_low + t_high) / 1000.0);
  296. SERIAL_PROTOCOLPGM(MSG_KU); SERIAL_PROTOCOL(Ku);
  297. SERIAL_PROTOCOLPGM(MSG_TU); SERIAL_PROTOCOLLN(Tu);
  298. workKp = 0.6 * Ku;
  299. workKi = 2 * workKp / Tu;
  300. workKd = workKp * Tu / 8;
  301. SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
  302. SERIAL_PROTOCOLPGM(MSG_KP); SERIAL_PROTOCOLLN(workKp);
  303. SERIAL_PROTOCOLPGM(MSG_KI); SERIAL_PROTOCOLLN(workKi);
  304. SERIAL_PROTOCOLPGM(MSG_KD); SERIAL_PROTOCOLLN(workKd);
  305. /**
  306. workKp = 0.33*Ku;
  307. workKi = workKp/Tu;
  308. workKd = workKp*Tu/3;
  309. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  310. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(workKp);
  311. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(workKi);
  312. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(workKd);
  313. workKp = 0.2*Ku;
  314. workKi = 2*workKp/Tu;
  315. workKd = workKp*Tu/3;
  316. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  317. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(workKp);
  318. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(workKi);
  319. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(workKd);
  320. */
  321. }
  322. }
  323. #if HAS_PID_FOR_BOTH
  324. if (extruder < 0)
  325. soft_pwm_bed = (bias + d) >> 1;
  326. else
  327. soft_pwm[extruder] = (bias + d) >> 1;
  328. #elif ENABLED(PIDTEMP)
  329. soft_pwm[extruder] = (bias + d) >> 1;
  330. #else
  331. soft_pwm_bed = (bias + d) >> 1;
  332. #endif
  333. cycles++;
  334. min = temp;
  335. }
  336. }
  337. }
  338. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  339. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  340. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  341. return;
  342. }
  343. // Every 2 seconds...
  344. if (ELAPSED(ms, temp_ms + 2000UL)) {
  345. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  346. print_heaterstates();
  347. SERIAL_EOL;
  348. #endif
  349. temp_ms = ms;
  350. } // every 2 seconds
  351. // Over 2 minutes?
  352. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  353. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  354. return;
  355. }
  356. if (cycles > ncycles) {
  357. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  358. #if HAS_PID_FOR_BOTH
  359. const char* estring = extruder < 0 ? "bed" : "";
  360. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kp "); SERIAL_PROTOCOLLN(workKp);
  361. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Ki "); SERIAL_PROTOCOLLN(workKi);
  362. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kd "); SERIAL_PROTOCOLLN(workKd);
  363. #elif ENABLED(PIDTEMP)
  364. SERIAL_PROTOCOLPGM("#define DEFAULT_Kp "); SERIAL_PROTOCOLLN(workKp);
  365. SERIAL_PROTOCOLPGM("#define DEFAULT_Ki "); SERIAL_PROTOCOLLN(workKi);
  366. SERIAL_PROTOCOLPGM("#define DEFAULT_Kd "); SERIAL_PROTOCOLLN(workKd);
  367. #else
  368. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKp "); SERIAL_PROTOCOLLN(workKp);
  369. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKi "); SERIAL_PROTOCOLLN(workKi);
  370. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKd "); SERIAL_PROTOCOLLN(workKd);
  371. #endif
  372. #define _SET_BED_PID() \
  373. bedKp = workKp; \
  374. bedKi = scalePID_i(workKi); \
  375. bedKd = scalePID_d(workKd); \
  376. updatePID()
  377. #define _SET_EXTRUDER_PID() \
  378. PID_PARAM(Kp, extruder) = workKp; \
  379. PID_PARAM(Ki, extruder) = scalePID_i(workKi); \
  380. PID_PARAM(Kd, extruder) = scalePID_d(workKd); \
  381. updatePID()
  382. // Use the result? (As with "M303 U1")
  383. if (set_result) {
  384. #if HAS_PID_FOR_BOTH
  385. if (extruder < 0) {
  386. _SET_BED_PID();
  387. }
  388. else {
  389. _SET_EXTRUDER_PID();
  390. }
  391. #elif ENABLED(PIDTEMP)
  392. _SET_EXTRUDER_PID();
  393. #else
  394. _SET_BED_PID();
  395. #endif
  396. }
  397. return;
  398. }
  399. lcd_update();
  400. }
  401. }
  402. #endif // PIDTEMP
  403. void updatePID() {
  404. #if ENABLED(PIDTEMP)
  405. for (int e = 0; e < EXTRUDERS; e++) {
  406. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  407. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  408. last_position[e] = 0;
  409. #endif
  410. }
  411. #endif
  412. #if ENABLED(PIDTEMPBED)
  413. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  414. #endif
  415. }
  416. int getHeaterPower(int heater) {
  417. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  418. }
  419. #if HAS_AUTO_FAN
  420. void setExtruderAutoFanState(int pin, bool state) {
  421. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  422. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  423. digitalWrite(pin, newFanSpeed);
  424. analogWrite(pin, newFanSpeed);
  425. }
  426. void checkExtruderAutoFans() {
  427. uint8_t fanState = 0;
  428. // which fan pins need to be turned on?
  429. #if HAS_AUTO_FAN_0
  430. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  431. fanState |= 1;
  432. #endif
  433. #if HAS_AUTO_FAN_1
  434. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
  435. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  436. fanState |= 1;
  437. else
  438. fanState |= 2;
  439. }
  440. #endif
  441. #if HAS_AUTO_FAN_2
  442. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
  443. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  444. fanState |= 1;
  445. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  446. fanState |= 2;
  447. else
  448. fanState |= 4;
  449. }
  450. #endif
  451. #if HAS_AUTO_FAN_3
  452. if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
  453. if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  454. fanState |= 1;
  455. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  456. fanState |= 2;
  457. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN)
  458. fanState |= 4;
  459. else
  460. fanState |= 8;
  461. }
  462. #endif
  463. // update extruder auto fan states
  464. #if HAS_AUTO_FAN_0
  465. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  466. #endif
  467. #if HAS_AUTO_FAN_1
  468. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  469. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  470. #endif
  471. #if HAS_AUTO_FAN_2
  472. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  473. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  474. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  475. #endif
  476. #if HAS_AUTO_FAN_3
  477. if (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  478. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN
  479. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  480. setExtruderAutoFanState(EXTRUDER_3_AUTO_FAN_PIN, (fanState & 8) != 0);
  481. #endif
  482. }
  483. #endif // HAS_AUTO_FAN
  484. //
  485. // Temperature Error Handlers
  486. //
  487. inline void _temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  488. static bool killed = false;
  489. if (IsRunning()) {
  490. SERIAL_ERROR_START;
  491. serialprintPGM(serial_msg);
  492. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  493. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  494. }
  495. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  496. if (!killed) {
  497. Running = false;
  498. killed = true;
  499. kill(lcd_msg);
  500. }
  501. else
  502. disable_all_heaters(); // paranoia
  503. #endif
  504. }
  505. void max_temp_error(uint8_t e) {
  506. _temp_error(e, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  507. }
  508. void min_temp_error(uint8_t e) {
  509. _temp_error(e, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  510. }
  511. float get_pid_output(int e) {
  512. float pid_output;
  513. #if ENABLED(PIDTEMP)
  514. #if DISABLED(PID_OPENLOOP)
  515. pid_error[e] = target_temperature[e] - current_temperature[e];
  516. dTerm[e] = K2 * PID_PARAM(Kd, e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
  517. temp_dState[e] = current_temperature[e];
  518. if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
  519. pid_output = BANG_MAX;
  520. pid_reset[e] = true;
  521. }
  522. else if (pid_error[e] < -(PID_FUNCTIONAL_RANGE) || target_temperature[e] == 0) {
  523. pid_output = 0;
  524. pid_reset[e] = true;
  525. }
  526. else {
  527. if (pid_reset[e]) {
  528. temp_iState[e] = 0.0;
  529. pid_reset[e] = false;
  530. }
  531. pTerm[e] = PID_PARAM(Kp, e) * pid_error[e];
  532. temp_iState[e] += pid_error[e];
  533. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  534. iTerm[e] = PID_PARAM(Ki, e) * temp_iState[e];
  535. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  536. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  537. cTerm[e] = 0;
  538. if (e == active_extruder) {
  539. long e_position = st_get_position(E_AXIS);
  540. if (e_position > last_position[e]) {
  541. lpq[lpq_ptr++] = e_position - last_position[e];
  542. last_position[e] = e_position;
  543. }
  544. else {
  545. lpq[lpq_ptr++] = 0;
  546. }
  547. if (lpq_ptr >= lpq_len) lpq_ptr = 0;
  548. cTerm[e] = (lpq[lpq_ptr] / axis_steps_per_unit[E_AXIS]) * PID_PARAM(Kc, e);
  549. pid_output += cTerm[e];
  550. }
  551. #endif //PID_ADD_EXTRUSION_RATE
  552. if (pid_output > PID_MAX) {
  553. if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  554. pid_output = PID_MAX;
  555. }
  556. else if (pid_output < 0) {
  557. if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  558. pid_output = 0;
  559. }
  560. }
  561. #else
  562. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  563. #endif //PID_OPENLOOP
  564. #if ENABLED(PID_DEBUG)
  565. SERIAL_ECHO_START;
  566. SERIAL_ECHOPAIR(MSG_PID_DEBUG, e);
  567. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[e]);
  568. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  569. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[e]);
  570. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[e]);
  571. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[e]);
  572. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  573. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[e]);
  574. #endif
  575. SERIAL_EOL;
  576. #endif //PID_DEBUG
  577. #else /* PID off */
  578. pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
  579. #endif
  580. return pid_output;
  581. }
  582. #if ENABLED(PIDTEMPBED)
  583. float get_pid_output_bed() {
  584. float pid_output;
  585. #if DISABLED(PID_OPENLOOP)
  586. pid_error_bed = target_temperature_bed - current_temperature_bed;
  587. pTerm_bed = bedKp * pid_error_bed;
  588. temp_iState_bed += pid_error_bed;
  589. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  590. iTerm_bed = bedKi * temp_iState_bed;
  591. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  592. temp_dState_bed = current_temperature_bed;
  593. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  594. if (pid_output > MAX_BED_POWER) {
  595. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  596. pid_output = MAX_BED_POWER;
  597. }
  598. else if (pid_output < 0) {
  599. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  600. pid_output = 0;
  601. }
  602. #else
  603. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  604. #endif // PID_OPENLOOP
  605. #if ENABLED(PID_BED_DEBUG)
  606. SERIAL_ECHO_START;
  607. SERIAL_ECHO(" PID_BED_DEBUG ");
  608. SERIAL_ECHO(": Input ");
  609. SERIAL_ECHO(current_temperature_bed);
  610. SERIAL_ECHO(" Output ");
  611. SERIAL_ECHO(pid_output);
  612. SERIAL_ECHO(" pTerm ");
  613. SERIAL_ECHO(pTerm_bed);
  614. SERIAL_ECHO(" iTerm ");
  615. SERIAL_ECHO(iTerm_bed);
  616. SERIAL_ECHO(" dTerm ");
  617. SERIAL_ECHOLN(dTerm_bed);
  618. #endif //PID_BED_DEBUG
  619. return pid_output;
  620. }
  621. #endif //PIDTEMPBED
  622. /**
  623. * Manage heating activities for extruder hot-ends and a heated bed
  624. * - Acquire updated temperature readings
  625. * - Invoke thermal runaway protection
  626. * - Manage extruder auto-fan
  627. * - Apply filament width to the extrusion rate (may move)
  628. * - Update the heated bed PID output value
  629. */
  630. void manage_heater() {
  631. if (!temp_meas_ready) return;
  632. updateTemperaturesFromRawValues();
  633. #if ENABLED(HEATER_0_USES_MAX6675)
  634. float ct = current_temperature[0];
  635. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  636. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  637. #endif
  638. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN
  639. millis_t ms = millis();
  640. #endif
  641. // Loop through all extruders
  642. for (int e = 0; e < EXTRUDERS; e++) {
  643. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  644. 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);
  645. #endif
  646. float pid_output = get_pid_output(e);
  647. // Check if temperature is within the correct range
  648. soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  649. // Check if the temperature is failing to increase
  650. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  651. // Is it time to check this extruder's heater?
  652. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) {
  653. // Has it failed to increase enough?
  654. if (degHotend(e) < watch_target_temp[e]) {
  655. // Stop!
  656. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  657. }
  658. else {
  659. // Start again if the target is still far off
  660. start_watching_heater(e);
  661. }
  662. }
  663. #endif // THERMAL_PROTECTION_HOTENDS
  664. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  665. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  666. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  667. }
  668. #endif
  669. } // Extruders Loop
  670. #if HAS_AUTO_FAN
  671. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  672. checkExtruderAutoFans();
  673. next_auto_fan_check_ms = ms + 2500UL;
  674. }
  675. #endif
  676. // Control the extruder rate based on the width sensor
  677. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  678. if (filament_sensor) {
  679. meas_shift_index = filwidth_delay_index1 - meas_delay_cm;
  680. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  681. // Get the delayed info and add 100 to reconstitute to a percent of
  682. // the nominal filament diameter then square it to get an area
  683. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  684. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  685. NOLESS(vm, 0.01);
  686. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  687. }
  688. #endif //FILAMENT_WIDTH_SENSOR
  689. #if DISABLED(PIDTEMPBED)
  690. if (PENDING(ms, next_bed_check_ms)) return;
  691. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  692. #endif
  693. #if TEMP_SENSOR_BED != 0
  694. #if ENABLED(THERMAL_PROTECTION_BED)
  695. 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);
  696. #endif
  697. #if ENABLED(PIDTEMPBED)
  698. float pid_output = get_pid_output_bed();
  699. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  700. #elif ENABLED(BED_LIMIT_SWITCHING)
  701. // Check if temperature is within the correct band
  702. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  703. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  704. soft_pwm_bed = 0;
  705. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  706. soft_pwm_bed = MAX_BED_POWER >> 1;
  707. }
  708. else {
  709. soft_pwm_bed = 0;
  710. WRITE_HEATER_BED(LOW);
  711. }
  712. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  713. // Check if temperature is within the correct range
  714. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  715. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  716. }
  717. else {
  718. soft_pwm_bed = 0;
  719. WRITE_HEATER_BED(LOW);
  720. }
  721. #endif
  722. #endif //TEMP_SENSOR_BED != 0
  723. }
  724. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  725. // Derived from RepRap FiveD extruder::getTemperature()
  726. // For hot end temperature measurement.
  727. static float analog2temp(int raw, uint8_t e) {
  728. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  729. if (e > EXTRUDERS)
  730. #else
  731. if (e >= EXTRUDERS)
  732. #endif
  733. {
  734. SERIAL_ERROR_START;
  735. SERIAL_ERROR((int)e);
  736. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  737. kill(PSTR(MSG_KILLED));
  738. return 0.0;
  739. }
  740. #if ENABLED(HEATER_0_USES_MAX6675)
  741. if (e == 0) return 0.25 * raw;
  742. #endif
  743. if (heater_ttbl_map[e] != NULL) {
  744. float celsius = 0;
  745. uint8_t i;
  746. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  747. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  748. if (PGM_RD_W((*tt)[i][0]) > raw) {
  749. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  750. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  751. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  752. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  753. break;
  754. }
  755. }
  756. // Overflow: Set to last value in the table
  757. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  758. return celsius;
  759. }
  760. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  761. }
  762. // Derived from RepRap FiveD extruder::getTemperature()
  763. // For bed temperature measurement.
  764. static float analog2tempBed(int raw) {
  765. #if ENABLED(BED_USES_THERMISTOR)
  766. float celsius = 0;
  767. byte i;
  768. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  769. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  770. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  771. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  772. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  773. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  774. break;
  775. }
  776. }
  777. // Overflow: Set to last value in the table
  778. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  779. return celsius;
  780. #elif defined(BED_USES_AD595)
  781. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  782. #else
  783. UNUSED(raw);
  784. return 0;
  785. #endif
  786. }
  787. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  788. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  789. static void updateTemperaturesFromRawValues() {
  790. #if ENABLED(HEATER_0_USES_MAX6675)
  791. current_temperature_raw[0] = read_max6675();
  792. #endif
  793. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  794. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  795. }
  796. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  797. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  798. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  799. #endif
  800. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  801. filament_width_meas = analog2widthFil();
  802. #endif
  803. #if ENABLED(USE_WATCHDOG)
  804. // Reset the watchdog after we know we have a temperature measurement.
  805. watchdog_reset();
  806. #endif
  807. CRITICAL_SECTION_START;
  808. temp_meas_ready = false;
  809. CRITICAL_SECTION_END;
  810. }
  811. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  812. // Convert raw Filament Width to millimeters
  813. float analog2widthFil() {
  814. return current_raw_filwidth / 16383.0 * 5.0;
  815. //return current_raw_filwidth;
  816. }
  817. // Convert raw Filament Width to a ratio
  818. int widthFil_to_size_ratio() {
  819. float temp = filament_width_meas;
  820. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  821. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  822. return filament_width_nominal / temp * 100;
  823. }
  824. #endif
  825. /**
  826. * Initialize the temperature manager
  827. * The manager is implemented by periodic calls to manage_heater()
  828. */
  829. void tp_init() {
  830. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  831. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  832. MCUCR = _BV(JTD);
  833. MCUCR = _BV(JTD);
  834. #endif
  835. // Finish init of mult extruder arrays
  836. for (int e = 0; e < EXTRUDERS; e++) {
  837. // populate with the first value
  838. maxttemp[e] = maxttemp[0];
  839. #if ENABLED(PIDTEMP)
  840. temp_iState_min[e] = 0.0;
  841. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  842. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  843. last_position[e] = 0;
  844. #endif
  845. #endif //PIDTEMP
  846. #if ENABLED(PIDTEMPBED)
  847. temp_iState_min_bed = 0.0;
  848. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  849. #endif //PIDTEMPBED
  850. }
  851. #if HAS_HEATER_0
  852. SET_OUTPUT(HEATER_0_PIN);
  853. #endif
  854. #if HAS_HEATER_1
  855. SET_OUTPUT(HEATER_1_PIN);
  856. #endif
  857. #if HAS_HEATER_2
  858. SET_OUTPUT(HEATER_2_PIN);
  859. #endif
  860. #if HAS_HEATER_3
  861. SET_OUTPUT(HEATER_3_PIN);
  862. #endif
  863. #if HAS_HEATER_BED
  864. SET_OUTPUT(HEATER_BED_PIN);
  865. #endif
  866. #if ENABLED(FAST_PWM_FAN) || ENABLED(FAN_SOFT_PWM)
  867. #if HAS_FAN0
  868. SET_OUTPUT(FAN_PIN);
  869. #if ENABLED(FAST_PWM_FAN)
  870. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  871. #endif
  872. #if ENABLED(FAN_SOFT_PWM)
  873. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  874. #endif
  875. #endif
  876. #if HAS_FAN1
  877. SET_OUTPUT(FAN1_PIN);
  878. #if ENABLED(FAST_PWM_FAN)
  879. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  880. #endif
  881. #if ENABLED(FAN_SOFT_PWM)
  882. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  883. #endif
  884. #endif
  885. #if HAS_FAN2
  886. SET_OUTPUT(FAN2_PIN);
  887. #if ENABLED(FAST_PWM_FAN)
  888. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  889. #endif
  890. #if ENABLED(FAN_SOFT_PWM)
  891. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  892. #endif
  893. #endif
  894. #endif // FAST_PWM_FAN || FAN_SOFT_PWM
  895. #if ENABLED(HEATER_0_USES_MAX6675)
  896. #if DISABLED(SDSUPPORT)
  897. OUT_WRITE(SCK_PIN, LOW);
  898. OUT_WRITE(MOSI_PIN, HIGH);
  899. OUT_WRITE(MISO_PIN, HIGH);
  900. #else
  901. pinMode(SS_PIN, OUTPUT);
  902. digitalWrite(SS_PIN, HIGH);
  903. #endif
  904. OUT_WRITE(MAX6675_SS, HIGH);
  905. #endif //HEATER_0_USES_MAX6675
  906. #ifdef DIDR2
  907. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  908. #else
  909. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  910. #endif
  911. // Set analog inputs
  912. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  913. DIDR0 = 0;
  914. #ifdef DIDR2
  915. DIDR2 = 0;
  916. #endif
  917. #if HAS_TEMP_0
  918. ANALOG_SELECT(TEMP_0_PIN);
  919. #endif
  920. #if HAS_TEMP_1
  921. ANALOG_SELECT(TEMP_1_PIN);
  922. #endif
  923. #if HAS_TEMP_2
  924. ANALOG_SELECT(TEMP_2_PIN);
  925. #endif
  926. #if HAS_TEMP_3
  927. ANALOG_SELECT(TEMP_3_PIN);
  928. #endif
  929. #if HAS_TEMP_BED
  930. ANALOG_SELECT(TEMP_BED_PIN);
  931. #endif
  932. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  933. ANALOG_SELECT(FILWIDTH_PIN);
  934. #endif
  935. #if HAS_AUTO_FAN_0
  936. pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT);
  937. #endif
  938. #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  939. pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT);
  940. #endif
  941. #if HAS_AUTO_FAN_2 && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  942. pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT);
  943. #endif
  944. #if HAS_AUTO_FAN_3 && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  945. pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT);
  946. #endif
  947. // Use timer0 for temperature measurement
  948. // Interleave temperature interrupt with millies interrupt
  949. OCR0B = 128;
  950. SBI(TIMSK0, OCIE0B);
  951. // Wait for temperature measurement to settle
  952. delay(250);
  953. #define TEMP_MIN_ROUTINE(NR) \
  954. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  955. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  956. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  957. minttemp_raw[NR] += OVERSAMPLENR; \
  958. else \
  959. minttemp_raw[NR] -= OVERSAMPLENR; \
  960. }
  961. #define TEMP_MAX_ROUTINE(NR) \
  962. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  963. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  964. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  965. maxttemp_raw[NR] -= OVERSAMPLENR; \
  966. else \
  967. maxttemp_raw[NR] += OVERSAMPLENR; \
  968. }
  969. #ifdef HEATER_0_MINTEMP
  970. TEMP_MIN_ROUTINE(0);
  971. #endif
  972. #ifdef HEATER_0_MAXTEMP
  973. TEMP_MAX_ROUTINE(0);
  974. #endif
  975. #if EXTRUDERS > 1
  976. #ifdef HEATER_1_MINTEMP
  977. TEMP_MIN_ROUTINE(1);
  978. #endif
  979. #ifdef HEATER_1_MAXTEMP
  980. TEMP_MAX_ROUTINE(1);
  981. #endif
  982. #if EXTRUDERS > 2
  983. #ifdef HEATER_2_MINTEMP
  984. TEMP_MIN_ROUTINE(2);
  985. #endif
  986. #ifdef HEATER_2_MAXTEMP
  987. TEMP_MAX_ROUTINE(2);
  988. #endif
  989. #if EXTRUDERS > 3
  990. #ifdef HEATER_3_MINTEMP
  991. TEMP_MIN_ROUTINE(3);
  992. #endif
  993. #ifdef HEATER_3_MAXTEMP
  994. TEMP_MAX_ROUTINE(3);
  995. #endif
  996. #endif // EXTRUDERS > 3
  997. #endif // EXTRUDERS > 2
  998. #endif // EXTRUDERS > 1
  999. #ifdef BED_MINTEMP
  1000. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1001. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1002. bed_minttemp_raw += OVERSAMPLENR;
  1003. #else
  1004. bed_minttemp_raw -= OVERSAMPLENR;
  1005. #endif
  1006. }
  1007. #endif //BED_MINTEMP
  1008. #ifdef BED_MAXTEMP
  1009. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1010. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1011. bed_maxttemp_raw -= OVERSAMPLENR;
  1012. #else
  1013. bed_maxttemp_raw += OVERSAMPLENR;
  1014. #endif
  1015. }
  1016. #endif //BED_MAXTEMP
  1017. }
  1018. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1019. /**
  1020. * Start Heating Sanity Check for hotends that are below
  1021. * their target temperature by a configurable margin.
  1022. * This is called when the temperature is set. (M104, M109)
  1023. */
  1024. void start_watching_heater(int e) {
  1025. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1026. watch_target_temp[e] = degHotend(e) + WATCH_TEMP_INCREASE;
  1027. watch_heater_next_ms[e] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1028. }
  1029. else
  1030. watch_heater_next_ms[e] = 0;
  1031. }
  1032. #endif
  1033. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED)
  1034. void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  1035. static float tr_target_temperature[EXTRUDERS + 1] = { 0.0 };
  1036. /**
  1037. SERIAL_ECHO_START;
  1038. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1039. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHOPGM(heater_id);
  1040. SERIAL_ECHOPGM(" ; State:");
  1041. SERIAL_ECHOPGM(*state);
  1042. SERIAL_ECHOPGM(" ; Timer:");
  1043. SERIAL_ECHOPGM(*timer);
  1044. SERIAL_ECHOPGM(" ; Temperature:");
  1045. SERIAL_ECHOPGM(temperature);
  1046. SERIAL_ECHOPGM(" ; Target Temp:");
  1047. SERIAL_ECHOPGM(target_temperature);
  1048. SERIAL_EOL;
  1049. */
  1050. int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
  1051. // If the target temperature changes, restart
  1052. if (tr_target_temperature[heater_index] != target_temperature)
  1053. *state = TRReset;
  1054. switch (*state) {
  1055. case TRReset:
  1056. *timer = 0;
  1057. *state = TRInactive;
  1058. // Inactive state waits for a target temperature to be set
  1059. case TRInactive:
  1060. if (target_temperature > 0) {
  1061. tr_target_temperature[heater_index] = target_temperature;
  1062. *state = TRFirstHeating;
  1063. }
  1064. break;
  1065. // When first heating, wait for the temperature to be reached then go to Stable state
  1066. case TRFirstHeating:
  1067. if (temperature >= tr_target_temperature[heater_index]) *state = TRStable;
  1068. break;
  1069. // While the temperature is stable watch for a bad temperature
  1070. case TRStable:
  1071. // If the temperature is over the target (-hysteresis) restart the timer
  1072. if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc)
  1073. *timer = millis();
  1074. // If the timer goes too long without a reset, trigger shutdown
  1075. else if (ELAPSED(millis(), *timer + period_seconds * 1000UL))
  1076. *state = TRRunaway;
  1077. break;
  1078. case TRRunaway:
  1079. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1080. }
  1081. }
  1082. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1083. void disable_all_heaters() {
  1084. for (int i = 0; i < EXTRUDERS; i++) setTargetHotend(0, i);
  1085. setTargetBed(0);
  1086. // If all heaters go down then for sure our print job has stopped
  1087. print_job_timer.stop();
  1088. #define DISABLE_HEATER(NR) { \
  1089. setTargetHotend(NR, 0); \
  1090. soft_pwm[NR] = 0; \
  1091. WRITE_HEATER_ ## NR (LOW); \
  1092. }
  1093. #if HAS_TEMP_HOTEND
  1094. setTargetHotend(0, 0);
  1095. soft_pwm[0] = 0;
  1096. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  1097. #endif
  1098. #if EXTRUDERS > 1 && HAS_TEMP_1
  1099. DISABLE_HEATER(1);
  1100. #endif
  1101. #if EXTRUDERS > 2 && HAS_TEMP_2
  1102. DISABLE_HEATER(2);
  1103. #endif
  1104. #if EXTRUDERS > 3 && HAS_TEMP_3
  1105. DISABLE_HEATER(3);
  1106. #endif
  1107. #if HAS_TEMP_BED
  1108. target_temperature_bed = 0;
  1109. soft_pwm_bed = 0;
  1110. #if HAS_HEATER_BED
  1111. WRITE_HEATER_BED(LOW);
  1112. #endif
  1113. #endif
  1114. }
  1115. #if ENABLED(HEATER_0_USES_MAX6675)
  1116. #define MAX6675_HEAT_INTERVAL 250u
  1117. #if ENABLED(MAX6675_IS_MAX31855)
  1118. uint32_t max6675_temp = 2000;
  1119. #define MAX6675_ERROR_MASK 7
  1120. #define MAX6675_DISCARD_BITS 18
  1121. #else
  1122. uint16_t max6675_temp = 2000;
  1123. #define MAX6675_ERROR_MASK 4
  1124. #define MAX6675_DISCARD_BITS 3
  1125. #endif
  1126. static millis_t next_max6675_ms = 0;
  1127. static int read_max6675() {
  1128. millis_t ms = millis();
  1129. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1130. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1131. CBI(
  1132. #ifdef PRR
  1133. PRR
  1134. #elif defined(PRR0)
  1135. PRR0
  1136. #endif
  1137. , PRSPI);
  1138. SPCR = _BV(MSTR) | _BV(SPE) | _BV(SPR0);
  1139. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1140. // ensure 100ns delay - a bit extra is fine
  1141. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1142. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1143. // Read a big-endian temperature value
  1144. max6675_temp = 0;
  1145. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1146. SPDR = 0;
  1147. for (;!TEST(SPSR, SPIF););
  1148. max6675_temp |= SPDR;
  1149. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1150. }
  1151. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1152. if (max6675_temp & MAX6675_ERROR_MASK)
  1153. max6675_temp = 4000; // thermocouple open
  1154. else
  1155. max6675_temp >>= MAX6675_DISCARD_BITS;
  1156. return (int)max6675_temp;
  1157. }
  1158. #endif //HEATER_0_USES_MAX6675
  1159. /**
  1160. * Stages in the ISR loop
  1161. */
  1162. enum TempState {
  1163. PrepareTemp_0,
  1164. MeasureTemp_0,
  1165. PrepareTemp_BED,
  1166. MeasureTemp_BED,
  1167. PrepareTemp_1,
  1168. MeasureTemp_1,
  1169. PrepareTemp_2,
  1170. MeasureTemp_2,
  1171. PrepareTemp_3,
  1172. MeasureTemp_3,
  1173. Prepare_FILWIDTH,
  1174. Measure_FILWIDTH,
  1175. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1176. };
  1177. static unsigned long raw_temp_value[4] = { 0 };
  1178. static unsigned long raw_temp_bed_value = 0;
  1179. static void set_current_temp_raw() {
  1180. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1181. current_temperature_raw[0] = raw_temp_value[0];
  1182. #endif
  1183. #if HAS_TEMP_1
  1184. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1185. redundant_temperature_raw = raw_temp_value[1];
  1186. #else
  1187. current_temperature_raw[1] = raw_temp_value[1];
  1188. #endif
  1189. #if HAS_TEMP_2
  1190. current_temperature_raw[2] = raw_temp_value[2];
  1191. #if HAS_TEMP_3
  1192. current_temperature_raw[3] = raw_temp_value[3];
  1193. #endif
  1194. #endif
  1195. #endif
  1196. current_temperature_bed_raw = raw_temp_bed_value;
  1197. temp_meas_ready = true;
  1198. }
  1199. /**
  1200. * Timer 0 is shared with millies
  1201. * - Manage PWM to all the heaters and fan
  1202. * - Update the raw temperature values
  1203. * - Check new temperature values for MIN/MAX errors
  1204. * - Step the babysteps value for each axis towards 0
  1205. */
  1206. ISR(TIMER0_COMPB_vect) {
  1207. static unsigned char temp_count = 0;
  1208. static TempState temp_state = StartupDelay;
  1209. static unsigned char pwm_count = _BV(SOFT_PWM_SCALE);
  1210. // Static members for each heater
  1211. #if ENABLED(SLOW_PWM_HEATERS)
  1212. static unsigned char slow_pwm_count = 0;
  1213. #define ISR_STATICS(n) \
  1214. static unsigned char soft_pwm_ ## n; \
  1215. static unsigned char state_heater_ ## n = 0; \
  1216. static unsigned char state_timer_heater_ ## n = 0
  1217. #else
  1218. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1219. #endif
  1220. // Statics per heater
  1221. ISR_STATICS(0);
  1222. #if (EXTRUDERS > 1) || ENABLED(HEATERS_PARALLEL)
  1223. ISR_STATICS(1);
  1224. #if EXTRUDERS > 2
  1225. ISR_STATICS(2);
  1226. #if EXTRUDERS > 3
  1227. ISR_STATICS(3);
  1228. #endif
  1229. #endif
  1230. #endif
  1231. #if HAS_HEATER_BED
  1232. ISR_STATICS(BED);
  1233. #endif
  1234. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1235. static unsigned long raw_filwidth_value = 0;
  1236. #endif
  1237. #if DISABLED(SLOW_PWM_HEATERS)
  1238. /**
  1239. * standard PWM modulation
  1240. */
  1241. if (pwm_count == 0) {
  1242. soft_pwm_0 = soft_pwm[0];
  1243. if (soft_pwm_0 > 0) {
  1244. WRITE_HEATER_0(1);
  1245. }
  1246. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1247. #if EXTRUDERS > 1
  1248. soft_pwm_1 = soft_pwm[1];
  1249. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1250. #if EXTRUDERS > 2
  1251. soft_pwm_2 = soft_pwm[2];
  1252. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1253. #if EXTRUDERS > 3
  1254. soft_pwm_3 = soft_pwm[3];
  1255. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1256. #endif
  1257. #endif
  1258. #endif
  1259. #if HAS_HEATER_BED
  1260. soft_pwm_BED = soft_pwm_bed;
  1261. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1262. #endif
  1263. #if ENABLED(FAN_SOFT_PWM)
  1264. #if HAS_FAN0
  1265. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1266. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1267. #endif
  1268. #if HAS_FAN1
  1269. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1270. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1271. #endif
  1272. #if HAS_FAN2
  1273. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1274. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1275. #endif
  1276. #endif
  1277. }
  1278. if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0);
  1279. #if EXTRUDERS > 1
  1280. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1281. #if EXTRUDERS > 2
  1282. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1283. #if EXTRUDERS > 3
  1284. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1285. #endif
  1286. #endif
  1287. #endif
  1288. #if HAS_HEATER_BED
  1289. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1290. #endif
  1291. #if ENABLED(FAN_SOFT_PWM)
  1292. #if HAS_FAN0
  1293. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1294. #endif
  1295. #if HAS_FAN1
  1296. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1297. #endif
  1298. #if HAS_FAN2
  1299. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1300. #endif
  1301. #endif
  1302. pwm_count += _BV(SOFT_PWM_SCALE);
  1303. pwm_count &= 0x7f;
  1304. #else // SLOW_PWM_HEATERS
  1305. /**
  1306. * SLOW PWM HEATERS
  1307. *
  1308. * for heaters drived by relay
  1309. */
  1310. #ifndef MIN_STATE_TIME
  1311. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1312. #endif
  1313. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1314. #define _SLOW_PWM_ROUTINE(NR, src) \
  1315. soft_pwm_ ## NR = src; \
  1316. if (soft_pwm_ ## NR > 0) { \
  1317. if (state_timer_heater_ ## NR == 0) { \
  1318. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1319. state_heater_ ## NR = 1; \
  1320. WRITE_HEATER_ ## NR(1); \
  1321. } \
  1322. } \
  1323. else { \
  1324. if (state_timer_heater_ ## NR == 0) { \
  1325. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1326. state_heater_ ## NR = 0; \
  1327. WRITE_HEATER_ ## NR(0); \
  1328. } \
  1329. }
  1330. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1331. #define PWM_OFF_ROUTINE(NR) \
  1332. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1333. if (state_timer_heater_ ## NR == 0) { \
  1334. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1335. state_heater_ ## NR = 0; \
  1336. WRITE_HEATER_ ## NR (0); \
  1337. } \
  1338. }
  1339. if (slow_pwm_count == 0) {
  1340. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1341. #if EXTRUDERS > 1
  1342. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1343. #if EXTRUDERS > 2
  1344. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1345. #if EXTRUDERS > 3
  1346. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1347. #endif
  1348. #endif
  1349. #endif
  1350. #if HAS_HEATER_BED
  1351. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1352. #endif
  1353. } // slow_pwm_count == 0
  1354. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1355. #if EXTRUDERS > 1
  1356. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1357. #if EXTRUDERS > 2
  1358. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1359. #if EXTRUDERS > 3
  1360. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1361. #endif
  1362. #endif
  1363. #endif
  1364. #if HAS_HEATER_BED
  1365. PWM_OFF_ROUTINE(BED); // BED
  1366. #endif
  1367. #if ENABLED(FAN_SOFT_PWM)
  1368. if (pwm_count == 0) {
  1369. #if HAS_FAN0
  1370. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1371. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1372. #endif
  1373. #if HAS_FAN1
  1374. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1375. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1376. #endif
  1377. #if HAS_FAN2
  1378. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1379. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1380. #endif
  1381. }
  1382. #if HAS_FAN0
  1383. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1384. #endif
  1385. #if HAS_FAN1
  1386. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1387. #endif
  1388. #if HAS_FAN2
  1389. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1390. #endif
  1391. #endif //FAN_SOFT_PWM
  1392. pwm_count += _BV(SOFT_PWM_SCALE);
  1393. pwm_count &= 0x7f;
  1394. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1395. if ((pwm_count % 64) == 0) {
  1396. slow_pwm_count++;
  1397. slow_pwm_count &= 0x7f;
  1398. // EXTRUDER 0
  1399. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1400. #if EXTRUDERS > 1 // EXTRUDER 1
  1401. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1402. #if EXTRUDERS > 2 // EXTRUDER 2
  1403. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1404. #if EXTRUDERS > 3 // EXTRUDER 3
  1405. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1406. #endif
  1407. #endif
  1408. #endif
  1409. #if HAS_HEATER_BED
  1410. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1411. #endif
  1412. } // (pwm_count % 64) == 0
  1413. #endif // SLOW_PWM_HEATERS
  1414. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1415. #ifdef MUX5
  1416. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1417. #else
  1418. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1419. #endif
  1420. // Prepare or measure a sensor, each one every 12th frame
  1421. switch (temp_state) {
  1422. case PrepareTemp_0:
  1423. #if HAS_TEMP_0
  1424. START_ADC(TEMP_0_PIN);
  1425. #endif
  1426. lcd_buttons_update();
  1427. temp_state = MeasureTemp_0;
  1428. break;
  1429. case MeasureTemp_0:
  1430. #if HAS_TEMP_0
  1431. raw_temp_value[0] += ADC;
  1432. #endif
  1433. temp_state = PrepareTemp_BED;
  1434. break;
  1435. case PrepareTemp_BED:
  1436. #if HAS_TEMP_BED
  1437. START_ADC(TEMP_BED_PIN);
  1438. #endif
  1439. lcd_buttons_update();
  1440. temp_state = MeasureTemp_BED;
  1441. break;
  1442. case MeasureTemp_BED:
  1443. #if HAS_TEMP_BED
  1444. raw_temp_bed_value += ADC;
  1445. #endif
  1446. temp_state = PrepareTemp_1;
  1447. break;
  1448. case PrepareTemp_1:
  1449. #if HAS_TEMP_1
  1450. START_ADC(TEMP_1_PIN);
  1451. #endif
  1452. lcd_buttons_update();
  1453. temp_state = MeasureTemp_1;
  1454. break;
  1455. case MeasureTemp_1:
  1456. #if HAS_TEMP_1
  1457. raw_temp_value[1] += ADC;
  1458. #endif
  1459. temp_state = PrepareTemp_2;
  1460. break;
  1461. case PrepareTemp_2:
  1462. #if HAS_TEMP_2
  1463. START_ADC(TEMP_2_PIN);
  1464. #endif
  1465. lcd_buttons_update();
  1466. temp_state = MeasureTemp_2;
  1467. break;
  1468. case MeasureTemp_2:
  1469. #if HAS_TEMP_2
  1470. raw_temp_value[2] += ADC;
  1471. #endif
  1472. temp_state = PrepareTemp_3;
  1473. break;
  1474. case PrepareTemp_3:
  1475. #if HAS_TEMP_3
  1476. START_ADC(TEMP_3_PIN);
  1477. #endif
  1478. lcd_buttons_update();
  1479. temp_state = MeasureTemp_3;
  1480. break;
  1481. case MeasureTemp_3:
  1482. #if HAS_TEMP_3
  1483. raw_temp_value[3] += ADC;
  1484. #endif
  1485. temp_state = Prepare_FILWIDTH;
  1486. break;
  1487. case Prepare_FILWIDTH:
  1488. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1489. START_ADC(FILWIDTH_PIN);
  1490. #endif
  1491. lcd_buttons_update();
  1492. temp_state = Measure_FILWIDTH;
  1493. break;
  1494. case Measure_FILWIDTH:
  1495. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1496. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1497. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1498. raw_filwidth_value -= (raw_filwidth_value >> 7); //multiply raw_filwidth_value by 127/128
  1499. raw_filwidth_value += ((unsigned long)ADC << 7); //add new ADC reading
  1500. }
  1501. #endif
  1502. temp_state = PrepareTemp_0;
  1503. temp_count++;
  1504. break;
  1505. case StartupDelay:
  1506. temp_state = PrepareTemp_0;
  1507. break;
  1508. // default:
  1509. // SERIAL_ERROR_START;
  1510. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1511. // break;
  1512. } // switch(temp_state)
  1513. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1514. // Update the raw values if they've been read. Else we could be updating them during reading.
  1515. if (!temp_meas_ready) set_current_temp_raw();
  1516. // Filament Sensor - can be read any time since IIR filtering is used
  1517. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1518. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1519. #endif
  1520. temp_count = 0;
  1521. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1522. raw_temp_bed_value = 0;
  1523. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1524. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1525. #define GE0 <=
  1526. #else
  1527. #define GE0 >=
  1528. #endif
  1529. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1530. if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
  1531. #endif
  1532. #if HAS_TEMP_1 && EXTRUDERS > 1
  1533. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1534. #define GE1 <=
  1535. #else
  1536. #define GE1 >=
  1537. #endif
  1538. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1539. if (minttemp_raw[1] GE1 current_temperature_raw[1]) min_temp_error(1);
  1540. #endif // TEMP_SENSOR_1
  1541. #if HAS_TEMP_2 && EXTRUDERS > 2
  1542. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1543. #define GE2 <=
  1544. #else
  1545. #define GE2 >=
  1546. #endif
  1547. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1548. if (minttemp_raw[2] GE2 current_temperature_raw[2]) min_temp_error(2);
  1549. #endif // TEMP_SENSOR_2
  1550. #if HAS_TEMP_3 && EXTRUDERS > 3
  1551. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1552. #define GE3 <=
  1553. #else
  1554. #define GE3 >=
  1555. #endif
  1556. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1557. if (minttemp_raw[3] GE3 current_temperature_raw[3]) min_temp_error(3);
  1558. #endif // TEMP_SENSOR_3
  1559. #if HAS_TEMP_BED
  1560. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1561. #define GEBED <=
  1562. #else
  1563. #define GEBED >=
  1564. #endif
  1565. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) _temp_error(-1, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP_BED));
  1566. if (bed_minttemp_raw GEBED current_temperature_bed_raw) _temp_error(-1, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP_BED));
  1567. #endif
  1568. } // temp_count >= OVERSAMPLENR
  1569. #if ENABLED(BABYSTEPPING)
  1570. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1571. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1572. if (curTodo > 0) {
  1573. babystep(axis,/*fwd*/true);
  1574. babystepsTodo[axis]--; //fewer to do next time
  1575. }
  1576. else if (curTodo < 0) {
  1577. babystep(axis,/*fwd*/false);
  1578. babystepsTodo[axis]++; //fewer to do next time
  1579. }
  1580. }
  1581. #endif //BABYSTEPPING
  1582. }
  1583. #if ENABLED(PIDTEMP)
  1584. // Apply the scale factors to the PID values
  1585. float scalePID_i(float i) { return i * PID_dT; }
  1586. float unscalePID_i(float i) { return i / PID_dT; }
  1587. float scalePID_d(float d) { return d / PID_dT; }
  1588. float unscalePID_d(float d) { return d * PID_dT; }
  1589. #endif //PIDTEMP