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

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