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

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