My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

temperature.cpp 50KB

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