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

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