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

temperature.cpp 50KB

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