My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 57KB

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