My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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 "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. NOLESS(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 NOMORE(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 = _BV(JTD);
  736. MCUCR = _BV(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. #if ENABLED(FAST_PWM_FAN) || ENABLED(FAN_SOFT_PWM)
  771. SET_OUTPUT(FAN_PIN);
  772. #if ENABLED(FAST_PWM_FAN)
  773. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  774. #endif
  775. #if ENABLED(FAN_SOFT_PWM)
  776. soft_pwm_fan = fanSpeedSoftPwm / 2;
  777. #endif
  778. #endif
  779. #endif
  780. #if ENABLED(HEATER_0_USES_MAX6675)
  781. #if DISABLED(SDSUPPORT)
  782. OUT_WRITE(SCK_PIN, LOW);
  783. OUT_WRITE(MOSI_PIN, HIGH);
  784. OUT_WRITE(MISO_PIN, HIGH);
  785. #else
  786. pinMode(SS_PIN, OUTPUT);
  787. digitalWrite(SS_PIN, HIGH);
  788. #endif
  789. OUT_WRITE(MAX6675_SS, HIGH);
  790. #endif //HEATER_0_USES_MAX6675
  791. #ifdef DIDR2
  792. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  793. #else
  794. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  795. #endif
  796. // Set analog inputs
  797. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  798. DIDR0 = 0;
  799. #ifdef DIDR2
  800. DIDR2 = 0;
  801. #endif
  802. #if HAS_TEMP_0
  803. ANALOG_SELECT(TEMP_0_PIN);
  804. #endif
  805. #if HAS_TEMP_1
  806. ANALOG_SELECT(TEMP_1_PIN);
  807. #endif
  808. #if HAS_TEMP_2
  809. ANALOG_SELECT(TEMP_2_PIN);
  810. #endif
  811. #if HAS_TEMP_3
  812. ANALOG_SELECT(TEMP_3_PIN);
  813. #endif
  814. #if HAS_TEMP_BED
  815. ANALOG_SELECT(TEMP_BED_PIN);
  816. #endif
  817. #if HAS_FILAMENT_SENSOR
  818. ANALOG_SELECT(FILWIDTH_PIN);
  819. #endif
  820. #if HAS_AUTO_FAN_0
  821. pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT);
  822. #endif
  823. #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  824. pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT);
  825. #endif
  826. #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)
  827. pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT);
  828. #endif
  829. #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)
  830. pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT);
  831. #endif
  832. // Use timer0 for temperature measurement
  833. // Interleave temperature interrupt with millies interrupt
  834. OCR0B = 128;
  835. SBI(TIMSK0, OCIE0B);
  836. // Wait for temperature measurement to settle
  837. delay(250);
  838. #define TEMP_MIN_ROUTINE(NR) \
  839. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  840. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  841. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  842. minttemp_raw[NR] += OVERSAMPLENR; \
  843. else \
  844. minttemp_raw[NR] -= OVERSAMPLENR; \
  845. }
  846. #define TEMP_MAX_ROUTINE(NR) \
  847. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  848. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  849. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  850. maxttemp_raw[NR] -= OVERSAMPLENR; \
  851. else \
  852. maxttemp_raw[NR] += OVERSAMPLENR; \
  853. }
  854. #ifdef HEATER_0_MINTEMP
  855. TEMP_MIN_ROUTINE(0);
  856. #endif
  857. #ifdef HEATER_0_MAXTEMP
  858. TEMP_MAX_ROUTINE(0);
  859. #endif
  860. #if EXTRUDERS > 1
  861. #ifdef HEATER_1_MINTEMP
  862. TEMP_MIN_ROUTINE(1);
  863. #endif
  864. #ifdef HEATER_1_MAXTEMP
  865. TEMP_MAX_ROUTINE(1);
  866. #endif
  867. #if EXTRUDERS > 2
  868. #ifdef HEATER_2_MINTEMP
  869. TEMP_MIN_ROUTINE(2);
  870. #endif
  871. #ifdef HEATER_2_MAXTEMP
  872. TEMP_MAX_ROUTINE(2);
  873. #endif
  874. #if EXTRUDERS > 3
  875. #ifdef HEATER_3_MINTEMP
  876. TEMP_MIN_ROUTINE(3);
  877. #endif
  878. #ifdef HEATER_3_MAXTEMP
  879. TEMP_MAX_ROUTINE(3);
  880. #endif
  881. #endif // EXTRUDERS > 3
  882. #endif // EXTRUDERS > 2
  883. #endif // EXTRUDERS > 1
  884. #ifdef BED_MINTEMP
  885. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  886. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  887. bed_minttemp_raw += OVERSAMPLENR;
  888. #else
  889. bed_minttemp_raw -= OVERSAMPLENR;
  890. #endif
  891. }
  892. #endif //BED_MINTEMP
  893. #ifdef BED_MAXTEMP
  894. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  895. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  896. bed_maxttemp_raw -= OVERSAMPLENR;
  897. #else
  898. bed_maxttemp_raw += OVERSAMPLENR;
  899. #endif
  900. }
  901. #endif //BED_MAXTEMP
  902. }
  903. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  904. /**
  905. * Start Heating Sanity Check for hotends that are below
  906. * their target temperature by a configurable margin.
  907. * This is called when the temperature is set. (M104, M109)
  908. */
  909. void start_watching_heater(int e) {
  910. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  911. watch_target_temp[e] = degHotend(e) + WATCH_TEMP_INCREASE;
  912. watch_heater_next_ms[e] = millis() + WATCH_TEMP_PERIOD * 1000UL;
  913. }
  914. else
  915. watch_heater_next_ms[e] = 0;
  916. }
  917. #endif
  918. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED)
  919. void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  920. static float tr_target_temperature[EXTRUDERS + 1] = { 0.0 };
  921. /*
  922. SERIAL_ECHO_START;
  923. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  924. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHOPGM(heater_id);
  925. SERIAL_ECHOPGM(" ; State:");
  926. SERIAL_ECHOPGM(*state);
  927. SERIAL_ECHOPGM(" ; Timer:");
  928. SERIAL_ECHOPGM(*timer);
  929. SERIAL_ECHOPGM(" ; Temperature:");
  930. SERIAL_ECHOPGM(temperature);
  931. SERIAL_ECHOPGM(" ; Target Temp:");
  932. SERIAL_ECHOPGM(target_temperature);
  933. SERIAL_EOL;
  934. */
  935. int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
  936. // If the target temperature changes, restart
  937. if (tr_target_temperature[heater_index] != target_temperature)
  938. *state = TRReset;
  939. switch (*state) {
  940. case TRReset:
  941. *timer = 0;
  942. *state = TRInactive;
  943. // Inactive state waits for a target temperature to be set
  944. case TRInactive:
  945. if (target_temperature > 0) {
  946. tr_target_temperature[heater_index] = target_temperature;
  947. *state = TRFirstHeating;
  948. }
  949. break;
  950. // When first heating, wait for the temperature to be reached then go to Stable state
  951. case TRFirstHeating:
  952. if (temperature >= tr_target_temperature[heater_index]) *state = TRStable;
  953. break;
  954. // While the temperature is stable watch for a bad temperature
  955. case TRStable:
  956. // If the temperature is over the target (-hysteresis) restart the timer
  957. if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc)
  958. *timer = millis();
  959. // If the timer goes too long without a reset, trigger shutdown
  960. else if (millis() > *timer + period_seconds * 1000UL)
  961. *state = TRRunaway;
  962. break;
  963. case TRRunaway:
  964. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  965. }
  966. }
  967. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  968. void disable_all_heaters() {
  969. for (int i = 0; i < EXTRUDERS; i++) setTargetHotend(0, i);
  970. setTargetBed(0);
  971. #define DISABLE_HEATER(NR) { \
  972. target_temperature[NR] = 0; \
  973. soft_pwm[NR] = 0; \
  974. WRITE_HEATER_ ## NR (LOW); \
  975. }
  976. #if HAS_TEMP_0 || ENABLED(HEATER_0_USES_MAX6675)
  977. target_temperature[0] = 0;
  978. soft_pwm[0] = 0;
  979. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  980. #endif
  981. #if EXTRUDERS > 1 && HAS_TEMP_1
  982. DISABLE_HEATER(1);
  983. #endif
  984. #if EXTRUDERS > 2 && HAS_TEMP_2
  985. DISABLE_HEATER(2);
  986. #endif
  987. #if EXTRUDERS > 3 && HAS_TEMP_3
  988. DISABLE_HEATER(3);
  989. #endif
  990. #if HAS_TEMP_BED
  991. target_temperature_bed = 0;
  992. soft_pwm_bed = 0;
  993. #if HAS_HEATER_BED
  994. WRITE_HEATER_BED(LOW);
  995. #endif
  996. #endif
  997. }
  998. #if ENABLED(HEATER_0_USES_MAX6675)
  999. #define MAX6675_HEAT_INTERVAL 250u
  1000. static millis_t next_max6675_ms = 0;
  1001. int max6675_temp = 2000;
  1002. static int read_max6675() {
  1003. millis_t ms = millis();
  1004. if (ms < next_max6675_ms)
  1005. return max6675_temp;
  1006. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1007. max6675_temp = 0;
  1008. CBI(
  1009. #ifdef PRR
  1010. PRR
  1011. #elif defined(PRR0)
  1012. PRR0
  1013. #endif
  1014. , PRSPI);
  1015. SPCR = _BV(MSTR) | _BV(SPE) | _BV(SPR0);
  1016. // enable TT_MAX6675
  1017. WRITE(MAX6675_SS, 0);
  1018. // ensure 100ns delay - a bit extra is fine
  1019. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1020. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1021. // read MSB
  1022. SPDR = 0;
  1023. for (; !TEST(SPSR, SPIF););
  1024. max6675_temp = SPDR;
  1025. max6675_temp <<= 8;
  1026. // read LSB
  1027. SPDR = 0;
  1028. for (; !TEST(SPSR, SPIF););
  1029. max6675_temp |= SPDR;
  1030. // disable TT_MAX6675
  1031. WRITE(MAX6675_SS, 1);
  1032. if (max6675_temp & 4) {
  1033. // thermocouple open
  1034. max6675_temp = 4000;
  1035. }
  1036. else {
  1037. max6675_temp = max6675_temp >> 3;
  1038. }
  1039. return max6675_temp;
  1040. }
  1041. #endif //HEATER_0_USES_MAX6675
  1042. /**
  1043. * Stages in the ISR loop
  1044. */
  1045. enum TempState {
  1046. PrepareTemp_0,
  1047. MeasureTemp_0,
  1048. PrepareTemp_BED,
  1049. MeasureTemp_BED,
  1050. PrepareTemp_1,
  1051. MeasureTemp_1,
  1052. PrepareTemp_2,
  1053. MeasureTemp_2,
  1054. PrepareTemp_3,
  1055. MeasureTemp_3,
  1056. Prepare_FILWIDTH,
  1057. Measure_FILWIDTH,
  1058. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1059. };
  1060. static unsigned long raw_temp_value[4] = { 0 };
  1061. static unsigned long raw_temp_bed_value = 0;
  1062. static void set_current_temp_raw() {
  1063. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1064. current_temperature_raw[0] = raw_temp_value[0];
  1065. #endif
  1066. #if HAS_TEMP_1
  1067. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1068. redundant_temperature_raw = raw_temp_value[1];
  1069. #else
  1070. current_temperature_raw[1] = raw_temp_value[1];
  1071. #endif
  1072. #if HAS_TEMP_2
  1073. current_temperature_raw[2] = raw_temp_value[2];
  1074. #if HAS_TEMP_3
  1075. current_temperature_raw[3] = raw_temp_value[3];
  1076. #endif
  1077. #endif
  1078. #endif
  1079. current_temperature_bed_raw = raw_temp_bed_value;
  1080. temp_meas_ready = true;
  1081. }
  1082. /**
  1083. * Timer 0 is shared with millies
  1084. * - Manage PWM to all the heaters and fan
  1085. * - Update the raw temperature values
  1086. * - Check new temperature values for MIN/MAX errors
  1087. * - Step the babysteps value for each axis towards 0
  1088. */
  1089. ISR(TIMER0_COMPB_vect) {
  1090. static unsigned char temp_count = 0;
  1091. static TempState temp_state = StartupDelay;
  1092. static unsigned char pwm_count = _BV(SOFT_PWM_SCALE);
  1093. // Static members for each heater
  1094. #if ENABLED(SLOW_PWM_HEATERS)
  1095. static unsigned char slow_pwm_count = 0;
  1096. #define ISR_STATICS(n) \
  1097. static unsigned char soft_pwm_ ## n; \
  1098. static unsigned char state_heater_ ## n = 0; \
  1099. static unsigned char state_timer_heater_ ## n = 0
  1100. #else
  1101. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1102. #endif
  1103. // Statics per heater
  1104. ISR_STATICS(0);
  1105. #if (EXTRUDERS > 1) || ENABLED(HEATERS_PARALLEL)
  1106. ISR_STATICS(1);
  1107. #if EXTRUDERS > 2
  1108. ISR_STATICS(2);
  1109. #if EXTRUDERS > 3
  1110. ISR_STATICS(3);
  1111. #endif
  1112. #endif
  1113. #endif
  1114. #if HAS_HEATER_BED
  1115. ISR_STATICS(BED);
  1116. #endif
  1117. #if HAS_FILAMENT_SENSOR
  1118. static unsigned long raw_filwidth_value = 0;
  1119. #endif
  1120. #if DISABLED(SLOW_PWM_HEATERS)
  1121. /**
  1122. * standard PWM modulation
  1123. */
  1124. if (pwm_count == 0) {
  1125. soft_pwm_0 = soft_pwm[0];
  1126. if (soft_pwm_0 > 0) {
  1127. WRITE_HEATER_0(1);
  1128. }
  1129. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1130. #if EXTRUDERS > 1
  1131. soft_pwm_1 = soft_pwm[1];
  1132. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1133. #if EXTRUDERS > 2
  1134. soft_pwm_2 = soft_pwm[2];
  1135. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1136. #if EXTRUDERS > 3
  1137. soft_pwm_3 = soft_pwm[3];
  1138. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1139. #endif
  1140. #endif
  1141. #endif
  1142. #if HAS_HEATER_BED
  1143. soft_pwm_BED = soft_pwm_bed;
  1144. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1145. #endif
  1146. #if ENABLED(FAN_SOFT_PWM)
  1147. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1148. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1149. #endif
  1150. }
  1151. if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0);
  1152. #if EXTRUDERS > 1
  1153. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1154. #if EXTRUDERS > 2
  1155. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1156. #if EXTRUDERS > 3
  1157. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1158. #endif
  1159. #endif
  1160. #endif
  1161. #if HAS_HEATER_BED
  1162. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1163. #endif
  1164. #if ENABLED(FAN_SOFT_PWM)
  1165. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1166. #endif
  1167. pwm_count += _BV(SOFT_PWM_SCALE);
  1168. pwm_count &= 0x7f;
  1169. #else // SLOW_PWM_HEATERS
  1170. /*
  1171. * SLOW PWM HEATERS
  1172. *
  1173. * for heaters drived by relay
  1174. */
  1175. #ifndef MIN_STATE_TIME
  1176. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1177. #endif
  1178. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1179. #define _SLOW_PWM_ROUTINE(NR, src) \
  1180. soft_pwm_ ## NR = src; \
  1181. if (soft_pwm_ ## NR > 0) { \
  1182. if (state_timer_heater_ ## NR == 0) { \
  1183. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1184. state_heater_ ## NR = 1; \
  1185. WRITE_HEATER_ ## NR(1); \
  1186. } \
  1187. } \
  1188. else { \
  1189. if (state_timer_heater_ ## NR == 0) { \
  1190. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1191. state_heater_ ## NR = 0; \
  1192. WRITE_HEATER_ ## NR(0); \
  1193. } \
  1194. }
  1195. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1196. #define PWM_OFF_ROUTINE(NR) \
  1197. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1198. if (state_timer_heater_ ## NR == 0) { \
  1199. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1200. state_heater_ ## NR = 0; \
  1201. WRITE_HEATER_ ## NR (0); \
  1202. } \
  1203. }
  1204. if (slow_pwm_count == 0) {
  1205. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1206. #if EXTRUDERS > 1
  1207. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1208. #if EXTRUDERS > 2
  1209. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1210. #if EXTRUDERS > 3
  1211. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1212. #endif
  1213. #endif
  1214. #endif
  1215. #if HAS_HEATER_BED
  1216. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1217. #endif
  1218. } // slow_pwm_count == 0
  1219. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1220. #if EXTRUDERS > 1
  1221. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1222. #if EXTRUDERS > 2
  1223. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1224. #if EXTRUDERS > 3
  1225. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1226. #endif
  1227. #endif
  1228. #endif
  1229. #if HAS_HEATER_BED
  1230. PWM_OFF_ROUTINE(BED); // BED
  1231. #endif
  1232. #if ENABLED(FAN_SOFT_PWM)
  1233. if (pwm_count == 0) {
  1234. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1235. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1236. }
  1237. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1238. #endif //FAN_SOFT_PWM
  1239. pwm_count += _BV(SOFT_PWM_SCALE);
  1240. pwm_count &= 0x7f;
  1241. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1242. if ((pwm_count % 64) == 0) {
  1243. slow_pwm_count++;
  1244. slow_pwm_count &= 0x7f;
  1245. // EXTRUDER 0
  1246. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1247. #if EXTRUDERS > 1 // EXTRUDER 1
  1248. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1249. #if EXTRUDERS > 2 // EXTRUDER 2
  1250. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1251. #if EXTRUDERS > 3 // EXTRUDER 3
  1252. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1253. #endif
  1254. #endif
  1255. #endif
  1256. #if HAS_HEATER_BED
  1257. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1258. #endif
  1259. } // (pwm_count % 64) == 0
  1260. #endif // SLOW_PWM_HEATERS
  1261. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1262. #ifdef MUX5
  1263. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1264. #else
  1265. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1266. #endif
  1267. // Prepare or measure a sensor, each one every 12th frame
  1268. switch (temp_state) {
  1269. case PrepareTemp_0:
  1270. #if HAS_TEMP_0
  1271. START_ADC(TEMP_0_PIN);
  1272. #endif
  1273. lcd_buttons_update();
  1274. temp_state = MeasureTemp_0;
  1275. break;
  1276. case MeasureTemp_0:
  1277. #if HAS_TEMP_0
  1278. raw_temp_value[0] += ADC;
  1279. #endif
  1280. temp_state = PrepareTemp_BED;
  1281. break;
  1282. case PrepareTemp_BED:
  1283. #if HAS_TEMP_BED
  1284. START_ADC(TEMP_BED_PIN);
  1285. #endif
  1286. lcd_buttons_update();
  1287. temp_state = MeasureTemp_BED;
  1288. break;
  1289. case MeasureTemp_BED:
  1290. #if HAS_TEMP_BED
  1291. raw_temp_bed_value += ADC;
  1292. #endif
  1293. temp_state = PrepareTemp_1;
  1294. break;
  1295. case PrepareTemp_1:
  1296. #if HAS_TEMP_1
  1297. START_ADC(TEMP_1_PIN);
  1298. #endif
  1299. lcd_buttons_update();
  1300. temp_state = MeasureTemp_1;
  1301. break;
  1302. case MeasureTemp_1:
  1303. #if HAS_TEMP_1
  1304. raw_temp_value[1] += ADC;
  1305. #endif
  1306. temp_state = PrepareTemp_2;
  1307. break;
  1308. case PrepareTemp_2:
  1309. #if HAS_TEMP_2
  1310. START_ADC(TEMP_2_PIN);
  1311. #endif
  1312. lcd_buttons_update();
  1313. temp_state = MeasureTemp_2;
  1314. break;
  1315. case MeasureTemp_2:
  1316. #if HAS_TEMP_2
  1317. raw_temp_value[2] += ADC;
  1318. #endif
  1319. temp_state = PrepareTemp_3;
  1320. break;
  1321. case PrepareTemp_3:
  1322. #if HAS_TEMP_3
  1323. START_ADC(TEMP_3_PIN);
  1324. #endif
  1325. lcd_buttons_update();
  1326. temp_state = MeasureTemp_3;
  1327. break;
  1328. case MeasureTemp_3:
  1329. #if HAS_TEMP_3
  1330. raw_temp_value[3] += ADC;
  1331. #endif
  1332. temp_state = Prepare_FILWIDTH;
  1333. break;
  1334. case Prepare_FILWIDTH:
  1335. #if HAS_FILAMENT_SENSOR
  1336. START_ADC(FILWIDTH_PIN);
  1337. #endif
  1338. lcd_buttons_update();
  1339. temp_state = Measure_FILWIDTH;
  1340. break;
  1341. case Measure_FILWIDTH:
  1342. #if HAS_FILAMENT_SENSOR
  1343. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1344. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1345. raw_filwidth_value -= (raw_filwidth_value >> 7); //multiply raw_filwidth_value by 127/128
  1346. raw_filwidth_value += ((unsigned long)ADC << 7); //add new ADC reading
  1347. }
  1348. #endif
  1349. temp_state = PrepareTemp_0;
  1350. temp_count++;
  1351. break;
  1352. case StartupDelay:
  1353. temp_state = PrepareTemp_0;
  1354. break;
  1355. // default:
  1356. // SERIAL_ERROR_START;
  1357. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1358. // break;
  1359. } // switch(temp_state)
  1360. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1361. // Update the raw values if they've been read. Else we could be updating them during reading.
  1362. if (!temp_meas_ready) set_current_temp_raw();
  1363. // Filament Sensor - can be read any time since IIR filtering is used
  1364. #if HAS_FILAMENT_SENSOR
  1365. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1366. #endif
  1367. temp_count = 0;
  1368. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1369. raw_temp_bed_value = 0;
  1370. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1371. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1372. #define GE0 <=
  1373. #else
  1374. #define GE0 >=
  1375. #endif
  1376. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1377. if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
  1378. #endif
  1379. #if HAS_TEMP_1 && EXTRUDERS > 1
  1380. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1381. #define GE1 <=
  1382. #else
  1383. #define GE1 >=
  1384. #endif
  1385. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1386. if (minttemp_raw[1] GE1 current_temperature_raw[1]) min_temp_error(1);
  1387. #endif // TEMP_SENSOR_1
  1388. #if HAS_TEMP_2 && EXTRUDERS > 2
  1389. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1390. #define GE2 <=
  1391. #else
  1392. #define GE2 >=
  1393. #endif
  1394. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1395. if (minttemp_raw[2] GE2 current_temperature_raw[2]) min_temp_error(2);
  1396. #endif // TEMP_SENSOR_2
  1397. #if HAS_TEMP_3 && EXTRUDERS > 3
  1398. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1399. #define GE3 <=
  1400. #else
  1401. #define GE3 >=
  1402. #endif
  1403. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1404. if (minttemp_raw[3] GE3 current_temperature_raw[3]) min_temp_error(3);
  1405. #endif // TEMP_SENSOR_3
  1406. #if HAS_TEMP_BED
  1407. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1408. #define GEBED <=
  1409. #else
  1410. #define GEBED >=
  1411. #endif
  1412. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) _temp_error(-1, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP_BED));
  1413. if (bed_minttemp_raw GEBED current_temperature_bed_raw) _temp_error(-1, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP_BED));
  1414. #endif
  1415. } // temp_count >= OVERSAMPLENR
  1416. #if ENABLED(BABYSTEPPING)
  1417. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1418. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1419. if (curTodo > 0) {
  1420. babystep(axis,/*fwd*/true);
  1421. babystepsTodo[axis]--; //fewer to do next time
  1422. }
  1423. else if (curTodo < 0) {
  1424. babystep(axis,/*fwd*/false);
  1425. babystepsTodo[axis]++; //fewer to do next time
  1426. }
  1427. }
  1428. #endif //BABYSTEPPING
  1429. }
  1430. #if ENABLED(PIDTEMP)
  1431. // Apply the scale factors to the PID values
  1432. float scalePID_i(float i) { return i * PID_dT; }
  1433. float unscalePID_i(float i) { return i / PID_dT; }
  1434. float scalePID_d(float d) { return d / PID_dT; }
  1435. float unscalePID_d(float d) { return d * PID_dT; }
  1436. #endif //PIDTEMP