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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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 "Sd2PinMap.h"
  28. //===========================================================================
  29. //=============================public variables============================
  30. //===========================================================================
  31. int target_temperature[EXTRUDERS] = { 0 };
  32. int target_temperature_bed = 0;
  33. int current_temperature_raw[EXTRUDERS] = { 0 };
  34. float current_temperature[EXTRUDERS] = { 0.0 };
  35. int current_temperature_bed_raw = 0;
  36. float current_temperature_bed = 0.0;
  37. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  38. int redundant_temperature_raw = 0;
  39. float redundant_temperature = 0.0;
  40. #endif
  41. #ifdef PIDTEMPBED
  42. float bedKp=DEFAULT_bedKp;
  43. float bedKi=(DEFAULT_bedKi*PID_dT);
  44. float bedKd=(DEFAULT_bedKd/PID_dT);
  45. #endif //PIDTEMPBED
  46. #ifdef FAN_SOFT_PWM
  47. unsigned char fanSpeedSoftPwm;
  48. #endif
  49. unsigned char soft_pwm_bed;
  50. #ifdef BABYSTEPPING
  51. volatile int babystepsTodo[3]={0,0,0};
  52. #endif
  53. #ifdef FILAMENT_SENSOR
  54. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  55. #endif
  56. //===========================================================================
  57. //=============================private variables============================
  58. //===========================================================================
  59. static volatile bool temp_meas_ready = false;
  60. #ifdef PIDTEMP
  61. //static cannot be external:
  62. static float temp_iState[EXTRUDERS] = { 0 };
  63. static float temp_dState[EXTRUDERS] = { 0 };
  64. static float pTerm[EXTRUDERS];
  65. static float iTerm[EXTRUDERS];
  66. static float dTerm[EXTRUDERS];
  67. //int output;
  68. static float pid_error[EXTRUDERS];
  69. static float temp_iState_min[EXTRUDERS];
  70. static float temp_iState_max[EXTRUDERS];
  71. // static float pid_input[EXTRUDERS];
  72. // static float pid_output[EXTRUDERS];
  73. static bool pid_reset[EXTRUDERS];
  74. #endif //PIDTEMP
  75. #ifdef PIDTEMPBED
  76. //static cannot be external:
  77. static float temp_iState_bed = { 0 };
  78. static float temp_dState_bed = { 0 };
  79. static float pTerm_bed;
  80. static float iTerm_bed;
  81. static float dTerm_bed;
  82. //int output;
  83. static float pid_error_bed;
  84. static float temp_iState_min_bed;
  85. static float temp_iState_max_bed;
  86. #else //PIDTEMPBED
  87. static unsigned long previous_millis_bed_heater;
  88. #endif //PIDTEMPBED
  89. static unsigned char soft_pwm[EXTRUDERS];
  90. #ifdef FAN_SOFT_PWM
  91. static unsigned char soft_pwm_fan;
  92. #endif
  93. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  94. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  95. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  96. static unsigned long extruder_autofan_last_check;
  97. #endif
  98. #if EXTRUDERS > 4
  99. # error Unsupported number of extruders
  100. #elif EXTRUDERS > 3
  101. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3, v4 }
  102. #elif EXTRUDERS > 2
  103. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3 }
  104. #elif EXTRUDERS > 1
  105. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2 }
  106. #else
  107. # define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1 }
  108. #endif
  109. #ifdef PIDTEMP
  110. #ifdef PID_PARAMS_PER_EXTRUDER
  111. float Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kp, DEFAULT_Kp, DEFAULT_Kp);
  112. float Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT);
  113. float Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT);
  114. #ifdef PID_ADD_EXTRUSION_RATE
  115. float Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kc, DEFAULT_Kc, DEFAULT_Kc);
  116. #endif // PID_ADD_EXTRUSION_RATE
  117. #else //PID_PARAMS_PER_EXTRUDER
  118. float Kp = DEFAULT_Kp;
  119. float Ki = DEFAULT_Ki * PID_dT;
  120. float Kd = DEFAULT_Kd / PID_dT;
  121. #ifdef PID_ADD_EXTRUSION_RATE
  122. float Kc = DEFAULT_Kc;
  123. #endif // PID_ADD_EXTRUSION_RATE
  124. #endif // PID_PARAMS_PER_EXTRUDER
  125. #endif //PIDTEMP
  126. // Init min and max temp with extreme values to prevent false errors during startup
  127. 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);
  128. 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);
  129. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0, 0 );
  130. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383, 16383 );
  131. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  132. #ifdef BED_MAXTEMP
  133. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  134. #endif
  135. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  136. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  137. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  138. #else
  139. 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 );
  140. 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 );
  141. #endif
  142. static float analog2temp(int raw, uint8_t e);
  143. static float analog2tempBed(int raw);
  144. static void updateTemperaturesFromRawValues();
  145. #ifdef WATCH_TEMP_PERIOD
  146. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
  147. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
  148. #endif //WATCH_TEMP_PERIOD
  149. #ifndef SOFT_PWM_SCALE
  150. #define SOFT_PWM_SCALE 0
  151. #endif
  152. #ifdef FILAMENT_SENSOR
  153. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  154. #endif
  155. #ifdef HEATER_0_USES_MAX6675
  156. static int read_max6675();
  157. #endif
  158. //===========================================================================
  159. //============================= functions ============================
  160. //===========================================================================
  161. void PID_autotune(float temp, int extruder, int ncycles)
  162. {
  163. float input = 0.0;
  164. int cycles=0;
  165. bool heating = true;
  166. unsigned long temp_millis = millis();
  167. unsigned long t1=temp_millis;
  168. unsigned long t2=temp_millis;
  169. long t_high = 0;
  170. long t_low = 0;
  171. long bias, d;
  172. float Ku, Tu;
  173. float Kp, Ki, Kd;
  174. float max = 0, min = 10000;
  175. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  176. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  177. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
  178. (defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
  179. unsigned long extruder_autofan_last_check = millis();
  180. #endif
  181. if ((extruder >= EXTRUDERS)
  182. #if (TEMP_BED_PIN <= -1)
  183. ||(extruder < 0)
  184. #endif
  185. ){
  186. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  187. return;
  188. }
  189. SERIAL_ECHOLN("PID Autotune start");
  190. disable_heater(); // switch off all heaters.
  191. if (extruder<0)
  192. {
  193. soft_pwm_bed = (MAX_BED_POWER)/2;
  194. bias = d = (MAX_BED_POWER)/2;
  195. }
  196. else
  197. {
  198. soft_pwm[extruder] = (PID_MAX)/2;
  199. bias = d = (PID_MAX)/2;
  200. }
  201. for(;;) {
  202. if(temp_meas_ready == true) { // temp sample ready
  203. updateTemperaturesFromRawValues();
  204. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  205. max=max(max,input);
  206. min=min(min,input);
  207. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  208. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  209. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
  210. (defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
  211. if(millis() - extruder_autofan_last_check > 2500) {
  212. checkExtruderAutoFans();
  213. extruder_autofan_last_check = millis();
  214. }
  215. #endif
  216. if(heating == true && input > temp) {
  217. if(millis() - t2 > 5000) {
  218. heating=false;
  219. if (extruder<0)
  220. soft_pwm_bed = (bias - d) >> 1;
  221. else
  222. soft_pwm[extruder] = (bias - d) >> 1;
  223. t1=millis();
  224. t_high=t1 - t2;
  225. max=temp;
  226. }
  227. }
  228. if(heating == false && input < temp) {
  229. if(millis() - t1 > 5000) {
  230. heating=true;
  231. t2=millis();
  232. t_low=t2 - t1;
  233. if(cycles > 0) {
  234. bias += (d*(t_high - t_low))/(t_low + t_high);
  235. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  236. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  237. else d = bias;
  238. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  239. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  240. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  241. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  242. if(cycles > 2) {
  243. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  244. Tu = ((float)(t_low + t_high)/1000.0);
  245. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  246. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  247. Kp = 0.6*Ku;
  248. Ki = 2*Kp/Tu;
  249. Kd = Kp*Tu/8;
  250. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  251. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  252. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  253. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  254. /*
  255. Kp = 0.33*Ku;
  256. Ki = Kp/Tu;
  257. Kd = Kp*Tu/3;
  258. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  259. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  260. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  261. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  262. Kp = 0.2*Ku;
  263. Ki = 2*Kp/Tu;
  264. Kd = Kp*Tu/3;
  265. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  266. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  267. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  268. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  269. */
  270. }
  271. }
  272. if (extruder<0)
  273. soft_pwm_bed = (bias + d) >> 1;
  274. else
  275. soft_pwm[extruder] = (bias + d) >> 1;
  276. cycles++;
  277. min=temp;
  278. }
  279. }
  280. }
  281. if(input > (temp + 20)) {
  282. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  283. return;
  284. }
  285. if(millis() - temp_millis > 2000) {
  286. int p;
  287. if (extruder<0){
  288. p=soft_pwm_bed;
  289. SERIAL_PROTOCOLPGM("ok B:");
  290. }else{
  291. p=soft_pwm[extruder];
  292. SERIAL_PROTOCOLPGM("ok T:");
  293. }
  294. SERIAL_PROTOCOL(input);
  295. SERIAL_PROTOCOLPGM(" @:");
  296. SERIAL_PROTOCOLLN(p);
  297. temp_millis = millis();
  298. }
  299. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  300. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  301. return;
  302. }
  303. if(cycles > ncycles) {
  304. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  305. return;
  306. }
  307. lcd_update();
  308. }
  309. }
  310. void updatePID()
  311. {
  312. #ifdef PIDTEMP
  313. for(int e = 0; e < EXTRUDERS; e++) {
  314. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  315. }
  316. #endif
  317. #ifdef PIDTEMPBED
  318. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  319. #endif
  320. }
  321. int getHeaterPower(int heater) {
  322. if (heater<0)
  323. return soft_pwm_bed;
  324. return soft_pwm[heater];
  325. }
  326. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  327. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  328. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  329. #if defined(FAN_PIN) && FAN_PIN > -1
  330. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  331. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  332. #endif
  333. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  334. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  335. #endif
  336. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  337. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  338. #endif
  339. #endif
  340. void setExtruderAutoFanState(int pin, bool state)
  341. {
  342. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  343. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  344. pinMode(pin, OUTPUT);
  345. digitalWrite(pin, newFanSpeed);
  346. analogWrite(pin, newFanSpeed);
  347. }
  348. void checkExtruderAutoFans()
  349. {
  350. uint8_t fanState = 0;
  351. // which fan pins need to be turned on?
  352. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  353. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  354. fanState |= 1;
  355. #endif
  356. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  357. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  358. {
  359. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  360. fanState |= 1;
  361. else
  362. fanState |= 2;
  363. }
  364. #endif
  365. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  366. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  367. {
  368. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  369. fanState |= 1;
  370. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  371. fanState |= 2;
  372. else
  373. fanState |= 4;
  374. }
  375. #endif
  376. #if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
  377. if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  378. {
  379. if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  380. fanState |= 1;
  381. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  382. fanState |= 2;
  383. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN)
  384. fanState |= 4;
  385. else
  386. fanState |= 8;
  387. }
  388. #endif
  389. // update extruder auto fan states
  390. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  391. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  392. #endif
  393. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  394. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  395. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  396. #endif
  397. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  398. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  399. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  400. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  401. #endif
  402. #if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
  403. if (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  404. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  405. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  406. setExtruderAutoFanState(EXTRUDER_3_AUTO_FAN_PIN, (fanState & 8) != 0);
  407. #endif
  408. }
  409. #endif // any extruder auto fan pins set
  410. void manage_heater()
  411. {
  412. float pid_input;
  413. float pid_output;
  414. if(temp_meas_ready != true) //better readability
  415. return;
  416. updateTemperaturesFromRawValues();
  417. #ifdef HEATER_0_USES_MAX6675
  418. if (current_temperature[0] > 1023 || current_temperature[0] > HEATER_0_MAXTEMP) {
  419. max_temp_error(0);
  420. }
  421. if (current_temperature[0] == 0 || current_temperature[0] < HEATER_0_MINTEMP) {
  422. min_temp_error(0);
  423. }
  424. #endif //HEATER_0_USES_MAX6675
  425. for(int e = 0; e < EXTRUDERS; e++)
  426. {
  427. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  428. 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);
  429. #endif
  430. #ifdef PIDTEMP
  431. pid_input = current_temperature[e];
  432. #ifndef PID_OPENLOOP
  433. pid_error[e] = target_temperature[e] - pid_input;
  434. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  435. pid_output = BANG_MAX;
  436. pid_reset[e] = true;
  437. }
  438. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  439. pid_output = 0;
  440. pid_reset[e] = true;
  441. }
  442. else {
  443. if(pid_reset[e] == true) {
  444. temp_iState[e] = 0.0;
  445. pid_reset[e] = false;
  446. }
  447. pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
  448. temp_iState[e] += pid_error[e];
  449. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  450. iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
  451. //K1 defined in Configuration.h in the PID settings
  452. #define K2 (1.0-K1)
  453. dTerm[e] = (PID_PARAM(Kd,e) * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  454. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  455. if (pid_output > PID_MAX) {
  456. if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  457. pid_output=PID_MAX;
  458. } else if (pid_output < 0){
  459. if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  460. pid_output=0;
  461. }
  462. }
  463. temp_dState[e] = pid_input;
  464. #else
  465. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  466. #endif //PID_OPENLOOP
  467. #ifdef PID_DEBUG
  468. SERIAL_ECHO_START;
  469. SERIAL_ECHO(" PID_DEBUG ");
  470. SERIAL_ECHO(e);
  471. SERIAL_ECHO(": Input ");
  472. SERIAL_ECHO(pid_input);
  473. SERIAL_ECHO(" Output ");
  474. SERIAL_ECHO(pid_output);
  475. SERIAL_ECHO(" pTerm ");
  476. SERIAL_ECHO(pTerm[e]);
  477. SERIAL_ECHO(" iTerm ");
  478. SERIAL_ECHO(iTerm[e]);
  479. SERIAL_ECHO(" dTerm ");
  480. SERIAL_ECHOLN(dTerm[e]);
  481. #endif //PID_DEBUG
  482. #else /* PID off */
  483. pid_output = 0;
  484. if(current_temperature[e] < target_temperature[e]) {
  485. pid_output = PID_MAX;
  486. }
  487. #endif
  488. // Check if temperature is within the correct range
  489. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  490. {
  491. soft_pwm[e] = (int)pid_output >> 1;
  492. }
  493. else {
  494. soft_pwm[e] = 0;
  495. }
  496. #ifdef WATCH_TEMP_PERIOD
  497. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  498. {
  499. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  500. {
  501. setTargetHotend(0, e);
  502. LCD_MESSAGEPGM("Heating failed");
  503. SERIAL_ECHO_START;
  504. SERIAL_ECHOLN("Heating failed");
  505. }else{
  506. watchmillis[e] = 0;
  507. }
  508. }
  509. #endif
  510. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  511. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  512. disable_heater();
  513. if(IsStopped() == false) {
  514. SERIAL_ERROR_START;
  515. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  516. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  517. }
  518. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  519. Stop();
  520. #endif
  521. }
  522. #endif
  523. } // End extruder for loop
  524. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  525. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  526. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  527. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  528. {
  529. checkExtruderAutoFans();
  530. extruder_autofan_last_check = millis();
  531. }
  532. #endif
  533. #ifndef PIDTEMPBED
  534. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  535. return;
  536. previous_millis_bed_heater = millis();
  537. #endif
  538. #if TEMP_SENSOR_BED != 0
  539. #if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0
  540. 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);
  541. #endif
  542. #ifdef PIDTEMPBED
  543. pid_input = current_temperature_bed;
  544. #ifndef PID_OPENLOOP
  545. pid_error_bed = target_temperature_bed - pid_input;
  546. pTerm_bed = bedKp * pid_error_bed;
  547. temp_iState_bed += pid_error_bed;
  548. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  549. iTerm_bed = bedKi * temp_iState_bed;
  550. //K1 defined in Configuration.h in the PID settings
  551. #define K2 (1.0-K1)
  552. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  553. temp_dState_bed = pid_input;
  554. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  555. if (pid_output > MAX_BED_POWER) {
  556. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  557. pid_output=MAX_BED_POWER;
  558. } else if (pid_output < 0){
  559. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  560. pid_output=0;
  561. }
  562. #else
  563. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  564. #endif //PID_OPENLOOP
  565. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  566. {
  567. soft_pwm_bed = (int)pid_output >> 1;
  568. }
  569. else {
  570. soft_pwm_bed = 0;
  571. }
  572. #elif !defined(BED_LIMIT_SWITCHING)
  573. // Check if temperature is within the correct range
  574. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  575. {
  576. if(current_temperature_bed >= target_temperature_bed)
  577. {
  578. soft_pwm_bed = 0;
  579. }
  580. else
  581. {
  582. soft_pwm_bed = MAX_BED_POWER>>1;
  583. }
  584. }
  585. else
  586. {
  587. soft_pwm_bed = 0;
  588. WRITE(HEATER_BED_PIN,LOW);
  589. }
  590. #else //#ifdef BED_LIMIT_SWITCHING
  591. // Check if temperature is within the correct band
  592. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  593. {
  594. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  595. {
  596. soft_pwm_bed = 0;
  597. }
  598. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  599. {
  600. soft_pwm_bed = MAX_BED_POWER>>1;
  601. }
  602. }
  603. else
  604. {
  605. soft_pwm_bed = 0;
  606. WRITE(HEATER_BED_PIN,LOW);
  607. }
  608. #endif
  609. #endif
  610. //code for controlling the extruder rate based on the width sensor
  611. #ifdef FILAMENT_SENSOR
  612. if(filament_sensor)
  613. {
  614. meas_shift_index=delay_index1-meas_delay_cm;
  615. if(meas_shift_index<0)
  616. meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed
  617. //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
  618. //then square it to get an area
  619. if(meas_shift_index<0)
  620. meas_shift_index=0;
  621. else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
  622. meas_shift_index=MAX_MEASUREMENT_DELAY;
  623. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
  624. if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
  625. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
  626. }
  627. #endif
  628. }
  629. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  630. // Derived from RepRap FiveD extruder::getTemperature()
  631. // For hot end temperature measurement.
  632. static float analog2temp(int raw, uint8_t e) {
  633. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  634. if(e > EXTRUDERS)
  635. #else
  636. if(e >= EXTRUDERS)
  637. #endif
  638. {
  639. SERIAL_ERROR_START;
  640. SERIAL_ERROR((int)e);
  641. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  642. kill();
  643. return 0.0;
  644. }
  645. #ifdef HEATER_0_USES_MAX6675
  646. if (e == 0)
  647. {
  648. return 0.25 * raw;
  649. }
  650. #endif
  651. if(heater_ttbl_map[e] != NULL)
  652. {
  653. float celsius = 0;
  654. uint8_t i;
  655. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  656. for (i=1; i<heater_ttbllen_map[e]; i++)
  657. {
  658. if (PGM_RD_W((*tt)[i][0]) > raw)
  659. {
  660. celsius = PGM_RD_W((*tt)[i-1][1]) +
  661. (raw - PGM_RD_W((*tt)[i-1][0])) *
  662. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  663. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  664. break;
  665. }
  666. }
  667. // Overflow: Set to last value in the table
  668. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  669. return celsius;
  670. }
  671. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  672. }
  673. // Derived from RepRap FiveD extruder::getTemperature()
  674. // For bed temperature measurement.
  675. static float analog2tempBed(int raw) {
  676. #ifdef BED_USES_THERMISTOR
  677. float celsius = 0;
  678. byte i;
  679. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  680. {
  681. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  682. {
  683. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  684. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  685. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  686. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  687. break;
  688. }
  689. }
  690. // Overflow: Set to last value in the table
  691. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  692. return celsius;
  693. #elif defined BED_USES_AD595
  694. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  695. #else
  696. return 0;
  697. #endif
  698. }
  699. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  700. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  701. static void updateTemperaturesFromRawValues()
  702. {
  703. #ifdef HEATER_0_USES_MAX6675
  704. current_temperature_raw[0] = read_max6675();
  705. #endif
  706. for(uint8_t e=0;e<EXTRUDERS;e++)
  707. {
  708. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  709. }
  710. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  711. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  712. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  713. #endif
  714. #if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
  715. filament_width_meas = analog2widthFil();
  716. #endif
  717. //Reset the watchdog after we know we have a temperature measurement.
  718. watchdog_reset();
  719. CRITICAL_SECTION_START;
  720. temp_meas_ready = false;
  721. CRITICAL_SECTION_END;
  722. }
  723. // For converting raw Filament Width to milimeters
  724. #ifdef FILAMENT_SENSOR
  725. float analog2widthFil() {
  726. return current_raw_filwidth/16383.0*5.0;
  727. //return current_raw_filwidth;
  728. }
  729. // For converting raw Filament Width to a ratio
  730. int widthFil_to_size_ratio() {
  731. float temp;
  732. temp=filament_width_meas;
  733. if(filament_width_meas<MEASURED_LOWER_LIMIT)
  734. temp=filament_width_nominal; //assume sensor cut out
  735. else if (filament_width_meas>MEASURED_UPPER_LIMIT)
  736. temp= MEASURED_UPPER_LIMIT;
  737. return(filament_width_nominal/temp*100);
  738. }
  739. #endif
  740. void tp_init()
  741. {
  742. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  743. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  744. MCUCR=(1<<JTD);
  745. MCUCR=(1<<JTD);
  746. #endif
  747. // Finish init of mult extruder arrays
  748. for(int e = 0; e < EXTRUDERS; e++) {
  749. // populate with the first value
  750. maxttemp[e] = maxttemp[0];
  751. #ifdef PIDTEMP
  752. temp_iState_min[e] = 0.0;
  753. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  754. #endif //PIDTEMP
  755. #ifdef PIDTEMPBED
  756. temp_iState_min_bed = 0.0;
  757. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  758. #endif //PIDTEMPBED
  759. }
  760. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  761. SET_OUTPUT(HEATER_0_PIN);
  762. #endif
  763. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  764. SET_OUTPUT(HEATER_1_PIN);
  765. #endif
  766. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  767. SET_OUTPUT(HEATER_2_PIN);
  768. #endif
  769. #if defined(HEATER_3_PIN) && (HEATER_3_PIN > -1)
  770. SET_OUTPUT(HEATER_3_PIN);
  771. #endif
  772. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  773. SET_OUTPUT(HEATER_BED_PIN);
  774. #endif
  775. #if defined(FAN_PIN) && (FAN_PIN > -1)
  776. SET_OUTPUT(FAN_PIN);
  777. #ifdef FAST_PWM_FAN
  778. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  779. #endif
  780. #ifdef FAN_SOFT_PWM
  781. soft_pwm_fan = fanSpeedSoftPwm / 2;
  782. #endif
  783. #endif
  784. #ifdef HEATER_0_USES_MAX6675
  785. #ifndef SDSUPPORT
  786. SET_OUTPUT(SCK_PIN);
  787. WRITE(SCK_PIN,0);
  788. SET_OUTPUT(MOSI_PIN);
  789. WRITE(MOSI_PIN,1);
  790. SET_INPUT(MISO_PIN);
  791. WRITE(MISO_PIN,1);
  792. #else
  793. pinMode(SS_PIN, OUTPUT);
  794. digitalWrite(SS_PIN, HIGH);
  795. #endif
  796. SET_OUTPUT(MAX6675_SS);
  797. WRITE(MAX6675_SS,1);
  798. #endif //HEATER_0_USES_MAX6675
  799. // Set analog inputs
  800. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  801. DIDR0 = 0;
  802. #ifdef DIDR2
  803. DIDR2 = 0;
  804. #endif
  805. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  806. #if TEMP_0_PIN < 8
  807. DIDR0 |= 1 << TEMP_0_PIN;
  808. #else
  809. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  810. #endif
  811. #endif
  812. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  813. #if TEMP_1_PIN < 8
  814. DIDR0 |= 1<<TEMP_1_PIN;
  815. #else
  816. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  817. #endif
  818. #endif
  819. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  820. #if TEMP_2_PIN < 8
  821. DIDR0 |= 1 << TEMP_2_PIN;
  822. #else
  823. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  824. #endif
  825. #endif
  826. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  827. #if TEMP_3_PIN < 8
  828. DIDR0 |= 1 << TEMP_3_PIN;
  829. #else
  830. DIDR2 |= 1<<(TEMP_3_PIN - 8);
  831. #endif
  832. #endif
  833. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  834. #if TEMP_BED_PIN < 8
  835. DIDR0 |= 1<<TEMP_BED_PIN;
  836. #else
  837. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  838. #endif
  839. #endif
  840. //Added for Filament Sensor
  841. #ifdef FILAMENT_SENSOR
  842. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
  843. #if FILWIDTH_PIN < 8
  844. DIDR0 |= 1<<FILWIDTH_PIN;
  845. #else
  846. DIDR2 |= 1<<(FILWIDTH_PIN - 8);
  847. #endif
  848. #endif
  849. #endif
  850. // Use timer0 for temperature measurement
  851. // Interleave temperature interrupt with millies interrupt
  852. OCR0B = 128;
  853. TIMSK0 |= (1<<OCIE0B);
  854. // Wait for temperature measurement to settle
  855. delay(250);
  856. #ifdef HEATER_0_MINTEMP
  857. minttemp[0] = HEATER_0_MINTEMP;
  858. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  859. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  860. minttemp_raw[0] += OVERSAMPLENR;
  861. #else
  862. minttemp_raw[0] -= OVERSAMPLENR;
  863. #endif
  864. }
  865. #endif //MINTEMP
  866. #ifdef HEATER_0_MAXTEMP
  867. maxttemp[0] = HEATER_0_MAXTEMP;
  868. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  869. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  870. maxttemp_raw[0] -= OVERSAMPLENR;
  871. #else
  872. maxttemp_raw[0] += OVERSAMPLENR;
  873. #endif
  874. }
  875. #endif //MAXTEMP
  876. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  877. minttemp[1] = HEATER_1_MINTEMP;
  878. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  879. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  880. minttemp_raw[1] += OVERSAMPLENR;
  881. #else
  882. minttemp_raw[1] -= OVERSAMPLENR;
  883. #endif
  884. }
  885. #endif // MINTEMP 1
  886. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  887. maxttemp[1] = HEATER_1_MAXTEMP;
  888. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  889. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  890. maxttemp_raw[1] -= OVERSAMPLENR;
  891. #else
  892. maxttemp_raw[1] += OVERSAMPLENR;
  893. #endif
  894. }
  895. #endif //MAXTEMP 1
  896. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  897. minttemp[2] = HEATER_2_MINTEMP;
  898. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  899. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  900. minttemp_raw[2] += OVERSAMPLENR;
  901. #else
  902. minttemp_raw[2] -= OVERSAMPLENR;
  903. #endif
  904. }
  905. #endif //MINTEMP 2
  906. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  907. maxttemp[2] = HEATER_2_MAXTEMP;
  908. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  909. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  910. maxttemp_raw[2] -= OVERSAMPLENR;
  911. #else
  912. maxttemp_raw[2] += OVERSAMPLENR;
  913. #endif
  914. }
  915. #endif //MAXTEMP 2
  916. #if (EXTRUDERS > 3) && defined(HEATER_3_MINTEMP)
  917. minttemp[3] = HEATER_3_MINTEMP;
  918. while(analog2temp(minttemp_raw[3], 3) < HEATER_3_MINTEMP) {
  919. #if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
  920. minttemp_raw[3] += OVERSAMPLENR;
  921. #else
  922. minttemp_raw[3] -= OVERSAMPLENR;
  923. #endif
  924. }
  925. #endif //MINTEMP 3
  926. #if (EXTRUDERS > 3) && defined(HEATER_3_MAXTEMP)
  927. maxttemp[3] = HEATER_3_MAXTEMP;
  928. while(analog2temp(maxttemp_raw[3], 3) > HEATER_3_MAXTEMP) {
  929. #if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
  930. maxttemp_raw[3] -= OVERSAMPLENR;
  931. #else
  932. maxttemp_raw[3] += OVERSAMPLENR;
  933. #endif
  934. }
  935. #endif // MAXTEMP 3
  936. #ifdef BED_MINTEMP
  937. /* No bed MINTEMP error implemented?!? */ /*
  938. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  939. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  940. bed_minttemp_raw += OVERSAMPLENR;
  941. #else
  942. bed_minttemp_raw -= OVERSAMPLENR;
  943. #endif
  944. }
  945. */
  946. #endif //BED_MINTEMP
  947. #ifdef BED_MAXTEMP
  948. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  949. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  950. bed_maxttemp_raw -= OVERSAMPLENR;
  951. #else
  952. bed_maxttemp_raw += OVERSAMPLENR;
  953. #endif
  954. }
  955. #endif //BED_MAXTEMP
  956. }
  957. void setWatch()
  958. {
  959. #ifdef WATCH_TEMP_PERIOD
  960. for (int e = 0; e < EXTRUDERS; e++)
  961. {
  962. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  963. {
  964. watch_start_temp[e] = degHotend(e);
  965. watchmillis[e] = millis();
  966. }
  967. }
  968. #endif
  969. }
  970. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  971. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  972. {
  973. /*
  974. SERIAL_ECHO_START;
  975. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  976. SERIAL_ECHO(heater_id);
  977. SERIAL_ECHO(" ; State:");
  978. SERIAL_ECHO(*state);
  979. SERIAL_ECHO(" ; Timer:");
  980. SERIAL_ECHO(*timer);
  981. SERIAL_ECHO(" ; Temperature:");
  982. SERIAL_ECHO(temperature);
  983. SERIAL_ECHO(" ; Target Temp:");
  984. SERIAL_ECHO(target_temperature);
  985. SERIAL_ECHOLN("");
  986. */
  987. if ((target_temperature == 0) || thermal_runaway)
  988. {
  989. *state = 0;
  990. *timer = 0;
  991. return;
  992. }
  993. switch (*state)
  994. {
  995. case 0: // "Heater Inactive" state
  996. if (target_temperature > 0) *state = 1;
  997. break;
  998. case 1: // "First Heating" state
  999. if (temperature >= target_temperature) *state = 2;
  1000. break;
  1001. case 2: // "Temperature Stable" state
  1002. if (temperature >= (target_temperature - hysteresis_degc))
  1003. {
  1004. *timer = millis();
  1005. }
  1006. else if ( (millis() - *timer) > ((unsigned long) period_seconds) * 1000)
  1007. {
  1008. SERIAL_ERROR_START;
  1009. SERIAL_ERRORLNPGM("Thermal Runaway, system stopped! Heater_ID: ");
  1010. SERIAL_ERRORLN((int)heater_id);
  1011. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  1012. thermal_runaway = true;
  1013. while(1)
  1014. {
  1015. disable_heater();
  1016. disable_x();
  1017. disable_y();
  1018. disable_z();
  1019. disable_e0();
  1020. disable_e1();
  1021. disable_e2();
  1022. disable_e3();
  1023. manage_heater();
  1024. lcd_update();
  1025. }
  1026. }
  1027. break;
  1028. }
  1029. }
  1030. #endif
  1031. void disable_heater()
  1032. {
  1033. for(int i=0;i<EXTRUDERS;i++)
  1034. setTargetHotend(0,i);
  1035. setTargetBed(0);
  1036. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1037. target_temperature[0]=0;
  1038. soft_pwm[0]=0;
  1039. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  1040. WRITE(HEATER_0_PIN,LOW);
  1041. #endif
  1042. #endif
  1043. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  1044. target_temperature[1]=0;
  1045. soft_pwm[1]=0;
  1046. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1047. WRITE(HEATER_1_PIN,LOW);
  1048. #endif
  1049. #endif
  1050. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  1051. target_temperature[2]=0;
  1052. soft_pwm[2]=0;
  1053. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1054. WRITE(HEATER_2_PIN,LOW);
  1055. #endif
  1056. #endif
  1057. #if defined(TEMP_3_PIN) && TEMP_3_PIN > -1 && EXTRUDERS > 3
  1058. target_temperature[3]=0;
  1059. soft_pwm[3]=0;
  1060. #if defined(HEATER_3_PIN) && HEATER_3_PIN > -1
  1061. WRITE(HEATER_3_PIN,LOW);
  1062. #endif
  1063. #endif
  1064. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1065. target_temperature_bed=0;
  1066. soft_pwm_bed=0;
  1067. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1068. WRITE(HEATER_BED_PIN,LOW);
  1069. #endif
  1070. #endif
  1071. }
  1072. void max_temp_error(uint8_t e) {
  1073. disable_heater();
  1074. if(IsStopped() == false) {
  1075. SERIAL_ERROR_START;
  1076. SERIAL_ERRORLN((int)e);
  1077. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  1078. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  1079. }
  1080. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1081. Stop();
  1082. #endif
  1083. }
  1084. void min_temp_error(uint8_t e) {
  1085. disable_heater();
  1086. if(IsStopped() == false) {
  1087. SERIAL_ERROR_START;
  1088. SERIAL_ERRORLN((int)e);
  1089. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1090. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1091. }
  1092. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1093. Stop();
  1094. #endif
  1095. }
  1096. void bed_max_temp_error(void) {
  1097. #if HEATER_BED_PIN > -1
  1098. WRITE(HEATER_BED_PIN, 0);
  1099. #endif
  1100. if(IsStopped() == false) {
  1101. SERIAL_ERROR_START;
  1102. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  1103. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1104. }
  1105. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1106. Stop();
  1107. #endif
  1108. }
  1109. #ifdef HEATER_0_USES_MAX6675
  1110. #define MAX6675_HEAT_INTERVAL 250
  1111. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1112. int max6675_temp = 2000;
  1113. static int read_max6675()
  1114. {
  1115. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1116. return max6675_temp;
  1117. max6675_previous_millis = millis();
  1118. max6675_temp = 0;
  1119. #ifdef PRR
  1120. PRR &= ~(1<<PRSPI);
  1121. #elif defined(PRR0)
  1122. PRR0 &= ~(1<<PRSPI);
  1123. #endif
  1124. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1125. // enable TT_MAX6675
  1126. WRITE(MAX6675_SS, 0);
  1127. // ensure 100ns delay - a bit extra is fine
  1128. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1129. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1130. // read MSB
  1131. SPDR = 0;
  1132. for (;(SPSR & (1<<SPIF)) == 0;);
  1133. max6675_temp = SPDR;
  1134. max6675_temp <<= 8;
  1135. // read LSB
  1136. SPDR = 0;
  1137. for (;(SPSR & (1<<SPIF)) == 0;);
  1138. max6675_temp |= SPDR;
  1139. // disable TT_MAX6675
  1140. WRITE(MAX6675_SS, 1);
  1141. if (max6675_temp & 4)
  1142. {
  1143. // thermocouple open
  1144. max6675_temp = 4000;
  1145. }
  1146. else
  1147. {
  1148. max6675_temp = max6675_temp >> 3;
  1149. }
  1150. return max6675_temp;
  1151. }
  1152. #endif //HEATER_0_USES_MAX6675
  1153. // Timer 0 is shared with millies
  1154. ISR(TIMER0_COMPB_vect)
  1155. {
  1156. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1157. static unsigned char temp_count = 0;
  1158. static unsigned long raw_temp_0_value = 0;
  1159. static unsigned long raw_temp_1_value = 0;
  1160. static unsigned long raw_temp_2_value = 0;
  1161. static unsigned long raw_temp_3_value = 0;
  1162. static unsigned long raw_temp_bed_value = 0;
  1163. static unsigned char temp_state = 12;
  1164. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1165. static unsigned char soft_pwm_0;
  1166. #ifdef SLOW_PWM_HEATERS
  1167. static unsigned char slow_pwm_count = 0;
  1168. static unsigned char state_heater_0 = 0;
  1169. static unsigned char state_timer_heater_0 = 0;
  1170. #endif
  1171. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1172. static unsigned char soft_pwm_1;
  1173. #ifdef SLOW_PWM_HEATERS
  1174. static unsigned char state_heater_1 = 0;
  1175. static unsigned char state_timer_heater_1 = 0;
  1176. #endif
  1177. #endif
  1178. #if EXTRUDERS > 2
  1179. static unsigned char soft_pwm_2;
  1180. #ifdef SLOW_PWM_HEATERS
  1181. static unsigned char state_heater_2 = 0;
  1182. static unsigned char state_timer_heater_2 = 0;
  1183. #endif
  1184. #endif
  1185. #if EXTRUDERS > 3
  1186. static unsigned char soft_pwm_3;
  1187. #ifdef SLOW_PWM_HEATERS
  1188. static unsigned char state_heater_3 = 0;
  1189. static unsigned char state_timer_heater_3 = 0;
  1190. #endif
  1191. #endif
  1192. #if HEATER_BED_PIN > -1
  1193. static unsigned char soft_pwm_b;
  1194. #ifdef SLOW_PWM_HEATERS
  1195. static unsigned char state_heater_b = 0;
  1196. static unsigned char state_timer_heater_b = 0;
  1197. #endif
  1198. #endif
  1199. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1200. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1201. #endif
  1202. #ifndef SLOW_PWM_HEATERS
  1203. /*
  1204. * standard PWM modulation
  1205. */
  1206. if(pwm_count == 0){
  1207. soft_pwm_0 = soft_pwm[0];
  1208. if(soft_pwm_0 > 0) {
  1209. WRITE(HEATER_0_PIN,1);
  1210. #ifdef HEATERS_PARALLEL
  1211. WRITE(HEATER_1_PIN,1);
  1212. #endif
  1213. } else WRITE(HEATER_0_PIN,0);
  1214. #if EXTRUDERS > 1
  1215. soft_pwm_1 = soft_pwm[1];
  1216. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1217. #endif
  1218. #if EXTRUDERS > 2
  1219. soft_pwm_2 = soft_pwm[2];
  1220. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1221. #endif
  1222. #if EXTRUDERS > 3
  1223. soft_pwm_3 = soft_pwm[3];
  1224. if(soft_pwm_3 > 0) WRITE(HEATER_3_PIN,1); else WRITE(HEATER_3_PIN,0);
  1225. #endif
  1226. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1227. soft_pwm_b = soft_pwm_bed;
  1228. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1229. #endif
  1230. #ifdef FAN_SOFT_PWM
  1231. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1232. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1233. #endif
  1234. }
  1235. if(soft_pwm_0 < pwm_count) {
  1236. WRITE(HEATER_0_PIN,0);
  1237. #ifdef HEATERS_PARALLEL
  1238. WRITE(HEATER_1_PIN,0);
  1239. #endif
  1240. }
  1241. #if EXTRUDERS > 1
  1242. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1243. #endif
  1244. #if EXTRUDERS > 2
  1245. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1246. #endif
  1247. #if EXTRUDERS > 3
  1248. if(soft_pwm_3 < pwm_count) WRITE(HEATER_3_PIN,0);
  1249. #endif
  1250. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1251. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1252. #endif
  1253. #ifdef FAN_SOFT_PWM
  1254. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1255. #endif
  1256. pwm_count += (1 << SOFT_PWM_SCALE);
  1257. pwm_count &= 0x7f;
  1258. #else //ifndef SLOW_PWM_HEATERS
  1259. /*
  1260. * SLOW PWM HEATERS
  1261. *
  1262. * for heaters drived by relay
  1263. */
  1264. #ifndef MIN_STATE_TIME
  1265. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1266. #endif
  1267. if (slow_pwm_count == 0) {
  1268. // EXTRUDER 0
  1269. soft_pwm_0 = soft_pwm[0];
  1270. if (soft_pwm_0 > 0) {
  1271. // turn ON heather only if the minimum time is up
  1272. if (state_timer_heater_0 == 0) {
  1273. // if change state set timer
  1274. if (state_heater_0 == 0) {
  1275. state_timer_heater_0 = MIN_STATE_TIME;
  1276. }
  1277. state_heater_0 = 1;
  1278. WRITE(HEATER_0_PIN, 1);
  1279. #ifdef HEATERS_PARALLEL
  1280. WRITE(HEATER_1_PIN, 1);
  1281. #endif
  1282. }
  1283. } else {
  1284. // turn OFF heather only if the minimum time is up
  1285. if (state_timer_heater_0 == 0) {
  1286. // if change state set timer
  1287. if (state_heater_0 == 1) {
  1288. state_timer_heater_0 = MIN_STATE_TIME;
  1289. }
  1290. state_heater_0 = 0;
  1291. WRITE(HEATER_0_PIN, 0);
  1292. #ifdef HEATERS_PARALLEL
  1293. WRITE(HEATER_1_PIN, 0);
  1294. #endif
  1295. }
  1296. }
  1297. #if EXTRUDERS > 1
  1298. // EXTRUDER 1
  1299. soft_pwm_1 = soft_pwm[1];
  1300. if (soft_pwm_1 > 0) {
  1301. // turn ON heather only if the minimum time is up
  1302. if (state_timer_heater_1 == 0) {
  1303. // if change state set timer
  1304. if (state_heater_1 == 0) {
  1305. state_timer_heater_1 = MIN_STATE_TIME;
  1306. }
  1307. state_heater_1 = 1;
  1308. WRITE(HEATER_1_PIN, 1);
  1309. }
  1310. } else {
  1311. // turn OFF heather only if the minimum time is up
  1312. if (state_timer_heater_1 == 0) {
  1313. // if change state set timer
  1314. if (state_heater_1 == 1) {
  1315. state_timer_heater_1 = MIN_STATE_TIME;
  1316. }
  1317. state_heater_1 = 0;
  1318. WRITE(HEATER_1_PIN, 0);
  1319. }
  1320. }
  1321. #endif
  1322. #if EXTRUDERS > 2
  1323. // EXTRUDER 2
  1324. soft_pwm_2 = soft_pwm[2];
  1325. if (soft_pwm_2 > 0) {
  1326. // turn ON heather only if the minimum time is up
  1327. if (state_timer_heater_2 == 0) {
  1328. // if change state set timer
  1329. if (state_heater_2 == 0) {
  1330. state_timer_heater_2 = MIN_STATE_TIME;
  1331. }
  1332. state_heater_2 = 1;
  1333. WRITE(HEATER_2_PIN, 1);
  1334. }
  1335. } else {
  1336. // turn OFF heather only if the minimum time is up
  1337. if (state_timer_heater_2 == 0) {
  1338. // if change state set timer
  1339. if (state_heater_2 == 1) {
  1340. state_timer_heater_2 = MIN_STATE_TIME;
  1341. }
  1342. state_heater_2 = 0;
  1343. WRITE(HEATER_2_PIN, 0);
  1344. }
  1345. }
  1346. #endif
  1347. #if EXTRUDERS > 3
  1348. // EXTRUDER 3
  1349. soft_pwm_3 = soft_pwm[3];
  1350. if (soft_pwm_3 > 0) {
  1351. // turn ON heather only if the minimum time is up
  1352. if (state_timer_heater_3 == 0) {
  1353. // if change state set timer
  1354. if (state_heater_3 == 0) {
  1355. state_timer_heater_3 = MIN_STATE_TIME;
  1356. }
  1357. state_heater_3 = 1;
  1358. WRITE(HEATER_3_PIN, 1);
  1359. }
  1360. } else {
  1361. // turn OFF heather only if the minimum time is up
  1362. if (state_timer_heater_3 == 0) {
  1363. // if change state set timer
  1364. if (state_heater_3 == 1) {
  1365. state_timer_heater_3 = MIN_STATE_TIME;
  1366. }
  1367. state_heater_3 = 0;
  1368. WRITE(HEATER_3_PIN, 0);
  1369. }
  1370. }
  1371. #endif
  1372. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1373. // BED
  1374. soft_pwm_b = soft_pwm_bed;
  1375. if (soft_pwm_b > 0) {
  1376. // turn ON heather only if the minimum time is up
  1377. if (state_timer_heater_b == 0) {
  1378. // if change state set timer
  1379. if (state_heater_b == 0) {
  1380. state_timer_heater_b = MIN_STATE_TIME;
  1381. }
  1382. state_heater_b = 1;
  1383. WRITE(HEATER_BED_PIN, 1);
  1384. }
  1385. } else {
  1386. // turn OFF heather only if the minimum time is up
  1387. if (state_timer_heater_b == 0) {
  1388. // if change state set timer
  1389. if (state_heater_b == 1) {
  1390. state_timer_heater_b = MIN_STATE_TIME;
  1391. }
  1392. state_heater_b = 0;
  1393. WRITE(HEATER_BED_PIN, 0);
  1394. }
  1395. }
  1396. #endif
  1397. } // if (slow_pwm_count == 0)
  1398. // EXTRUDER 0
  1399. if (soft_pwm_0 < slow_pwm_count) {
  1400. // turn OFF heather only if the minimum time is up
  1401. if (state_timer_heater_0 == 0) {
  1402. // if change state set timer
  1403. if (state_heater_0 == 1) {
  1404. state_timer_heater_0 = MIN_STATE_TIME;
  1405. }
  1406. state_heater_0 = 0;
  1407. WRITE(HEATER_0_PIN, 0);
  1408. #ifdef HEATERS_PARALLEL
  1409. WRITE(HEATER_1_PIN, 0);
  1410. #endif
  1411. }
  1412. }
  1413. #if EXTRUDERS > 1
  1414. // EXTRUDER 1
  1415. if (soft_pwm_1 < slow_pwm_count) {
  1416. // turn OFF heather only if the minimum time is up
  1417. if (state_timer_heater_1 == 0) {
  1418. // if change state set timer
  1419. if (state_heater_1 == 1) {
  1420. state_timer_heater_1 = MIN_STATE_TIME;
  1421. }
  1422. state_heater_1 = 0;
  1423. WRITE(HEATER_1_PIN, 0);
  1424. }
  1425. }
  1426. #endif
  1427. #if EXTRUDERS > 2
  1428. // EXTRUDER 2
  1429. if (soft_pwm_2 < slow_pwm_count) {
  1430. // turn OFF heather only if the minimum time is up
  1431. if (state_timer_heater_2 == 0) {
  1432. // if change state set timer
  1433. if (state_heater_2 == 1) {
  1434. state_timer_heater_2 = MIN_STATE_TIME;
  1435. }
  1436. state_heater_2 = 0;
  1437. WRITE(HEATER_2_PIN, 0);
  1438. }
  1439. }
  1440. #endif
  1441. #if EXTRUDERS > 3
  1442. // EXTRUDER 3
  1443. if (soft_pwm_3 < slow_pwm_count) {
  1444. // turn OFF heather only if the minimum time is up
  1445. if (state_timer_heater_3 == 0) {
  1446. // if change state set timer
  1447. if (state_heater_3 == 1) {
  1448. state_timer_heater_3 = MIN_STATE_TIME;
  1449. }
  1450. state_heater_3 = 0;
  1451. WRITE(HEATER_3_PIN, 0);
  1452. }
  1453. }
  1454. #endif
  1455. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1456. // BED
  1457. if (soft_pwm_b < slow_pwm_count) {
  1458. // turn OFF heather only if the minimum time is up
  1459. if (state_timer_heater_b == 0) {
  1460. // if change state set timer
  1461. if (state_heater_b == 1) {
  1462. state_timer_heater_b = MIN_STATE_TIME;
  1463. }
  1464. state_heater_b = 0;
  1465. WRITE(HEATER_BED_PIN, 0);
  1466. }
  1467. }
  1468. #endif
  1469. #ifdef FAN_SOFT_PWM
  1470. if (pwm_count == 0){
  1471. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1472. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1473. }
  1474. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1475. #endif
  1476. pwm_count += (1 << SOFT_PWM_SCALE);
  1477. pwm_count &= 0x7f;
  1478. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1479. if ((pwm_count % 64) == 0) {
  1480. slow_pwm_count++;
  1481. slow_pwm_count &= 0x7f;
  1482. // Extruder 0
  1483. if (state_timer_heater_0 > 0) {
  1484. state_timer_heater_0--;
  1485. }
  1486. #if EXTRUDERS > 1
  1487. // Extruder 1
  1488. if (state_timer_heater_1 > 0)
  1489. state_timer_heater_1--;
  1490. #endif
  1491. #if EXTRUDERS > 2
  1492. // Extruder 2
  1493. if (state_timer_heater_2 > 0)
  1494. state_timer_heater_2--;
  1495. #endif
  1496. #if EXTRUDERS > 3
  1497. // Extruder 3
  1498. if (state_timer_heater_3 > 0)
  1499. state_timer_heater_3--;
  1500. #endif
  1501. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1502. // Bed
  1503. if (state_timer_heater_b > 0)
  1504. state_timer_heater_b--;
  1505. #endif
  1506. } //if ((pwm_count % 64) == 0) {
  1507. #endif //ifndef SLOW_PWM_HEATERS
  1508. switch(temp_state) {
  1509. case 0: // Prepare TEMP_0
  1510. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1511. #if TEMP_0_PIN > 7
  1512. ADCSRB = 1<<MUX5;
  1513. #else
  1514. ADCSRB = 0;
  1515. #endif
  1516. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1517. ADCSRA |= 1<<ADSC; // Start conversion
  1518. #endif
  1519. lcd_buttons_update();
  1520. temp_state = 1;
  1521. break;
  1522. case 1: // Measure TEMP_0
  1523. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1524. raw_temp_0_value += ADC;
  1525. #endif
  1526. temp_state = 2;
  1527. break;
  1528. case 2: // Prepare TEMP_BED
  1529. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1530. #if TEMP_BED_PIN > 7
  1531. ADCSRB = 1<<MUX5;
  1532. #else
  1533. ADCSRB = 0;
  1534. #endif
  1535. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1536. ADCSRA |= 1<<ADSC; // Start conversion
  1537. #endif
  1538. lcd_buttons_update();
  1539. temp_state = 3;
  1540. break;
  1541. case 3: // Measure TEMP_BED
  1542. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1543. raw_temp_bed_value += ADC;
  1544. #endif
  1545. temp_state = 4;
  1546. break;
  1547. case 4: // Prepare TEMP_1
  1548. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1549. #if TEMP_1_PIN > 7
  1550. ADCSRB = 1<<MUX5;
  1551. #else
  1552. ADCSRB = 0;
  1553. #endif
  1554. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1555. ADCSRA |= 1<<ADSC; // Start conversion
  1556. #endif
  1557. lcd_buttons_update();
  1558. temp_state = 5;
  1559. break;
  1560. case 5: // Measure TEMP_1
  1561. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1562. raw_temp_1_value += ADC;
  1563. #endif
  1564. temp_state = 6;
  1565. break;
  1566. case 6: // Prepare TEMP_2
  1567. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1568. #if TEMP_2_PIN > 7
  1569. ADCSRB = 1<<MUX5;
  1570. #else
  1571. ADCSRB = 0;
  1572. #endif
  1573. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1574. ADCSRA |= 1<<ADSC; // Start conversion
  1575. #endif
  1576. lcd_buttons_update();
  1577. temp_state = 7;
  1578. break;
  1579. case 7: // Measure TEMP_2
  1580. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1581. raw_temp_2_value += ADC;
  1582. #endif
  1583. temp_state = 8;
  1584. <<<<<<< HEAD
  1585. break;
  1586. case 8: // Prepare TEMP_3
  1587. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  1588. #if TEMP_3_PIN > 7
  1589. ADCSRB = 1<<MUX5;
  1590. #else
  1591. ADCSRB = 0;
  1592. #endif
  1593. ADMUX = ((1 << REFS0) | (TEMP_3_PIN & 0x07));
  1594. ADCSRA |= 1<<ADSC; // Start conversion
  1595. #endif
  1596. lcd_buttons_update();
  1597. temp_state = 9;
  1598. break;
  1599. case 9: // Measure TEMP_3
  1600. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  1601. raw_temp_3_value += ADC;
  1602. #endif
  1603. temp_state = 10; //change so that Filament Width is also measured
  1604. break;
  1605. =======
  1606. break;
  1607. case 8: // Prepare TEMP_3
  1608. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  1609. #if TEMP_3_PIN > 7
  1610. ADCSRB = 1<<MUX5;
  1611. #else
  1612. ADCSRB = 0;
  1613. #endif
  1614. ADMUX = ((1 << REFS0) | (TEMP_3_PIN & 0x07));
  1615. ADCSRA |= 1<<ADSC; // Start conversion
  1616. #endif
  1617. lcd_buttons_update();
  1618. temp_state = 9;
  1619. break;
  1620. case 9: // Measure TEMP_3
  1621. #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
  1622. raw_temp_3_value += ADC;
  1623. #endif
  1624. temp_state = 10; //change so that Filament Width is also measured
  1625. break;
  1626. >>>>>>> 0ebf896070e8364252df3b3f59111218f1918cc8
  1627. case 10: //Prepare FILWIDTH
  1628. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1629. #if FILWIDTH_PIN>7
  1630. ADCSRB = 1<<MUX5;
  1631. #else
  1632. ADCSRB = 0;
  1633. #endif
  1634. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1635. ADCSRA |= 1<<ADSC; // Start conversion
  1636. #endif
  1637. lcd_buttons_update();
  1638. temp_state = 11;
  1639. break;
  1640. case 11: //Measure FILWIDTH
  1641. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1642. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1643. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1644. {
  1645. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1646. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1647. }
  1648. #endif
  1649. temp_state = 0;
  1650. temp_count++;
  1651. break;
  1652. case 12: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1653. temp_state = 0;
  1654. break;
  1655. // default:
  1656. // SERIAL_ERROR_START;
  1657. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1658. // break;
  1659. }
  1660. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1661. {
  1662. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1663. {
  1664. #ifndef HEATER_0_USES_MAX6675
  1665. current_temperature_raw[0] = raw_temp_0_value;
  1666. #endif
  1667. #if EXTRUDERS > 1
  1668. current_temperature_raw[1] = raw_temp_1_value;
  1669. #endif
  1670. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1671. redundant_temperature_raw = raw_temp_1_value;
  1672. #endif
  1673. #if EXTRUDERS > 2
  1674. current_temperature_raw[2] = raw_temp_2_value;
  1675. #endif
  1676. #if EXTRUDERS > 3
  1677. current_temperature_raw[3] = raw_temp_3_value;
  1678. #endif
  1679. current_temperature_bed_raw = raw_temp_bed_value;
  1680. }
  1681. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1682. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1683. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1684. #endif
  1685. temp_meas_ready = true;
  1686. temp_count = 0;
  1687. raw_temp_0_value = 0;
  1688. raw_temp_1_value = 0;
  1689. raw_temp_2_value = 0;
  1690. raw_temp_3_value = 0;
  1691. raw_temp_bed_value = 0;
  1692. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1693. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1694. #else
  1695. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1696. #endif
  1697. #ifndef HEATER_0_USES_MAX6675
  1698. max_temp_error(0);
  1699. #endif
  1700. }
  1701. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1702. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1703. #else
  1704. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1705. #endif
  1706. #ifndef HEATER_0_USES_MAX6675
  1707. min_temp_error(0);
  1708. #endif
  1709. }
  1710. #if EXTRUDERS > 1
  1711. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1712. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1713. #else
  1714. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1715. #endif
  1716. max_temp_error(1);
  1717. }
  1718. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1719. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1720. #else
  1721. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1722. #endif
  1723. min_temp_error(1);
  1724. }
  1725. #endif
  1726. #if EXTRUDERS > 2
  1727. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1728. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1729. #else
  1730. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1731. #endif
  1732. max_temp_error(2);
  1733. }
  1734. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1735. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1736. #else
  1737. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1738. #endif
  1739. min_temp_error(2);
  1740. }
  1741. #endif
  1742. #if EXTRUDERS > 3
  1743. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1744. if(current_temperature_raw[3] <= maxttemp_raw[3]) {
  1745. #else
  1746. if(current_temperature_raw[3] >= maxttemp_raw[3]) {
  1747. #endif
  1748. max_temp_error(3);
  1749. }
  1750. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1751. if(current_temperature_raw[3] >= minttemp_raw[3]) {
  1752. #else
  1753. if(current_temperature_raw[3] <= minttemp_raw[3]) {
  1754. #endif
  1755. min_temp_error(3);
  1756. }
  1757. #endif
  1758. /* No bed MINTEMP error? */
  1759. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1760. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1761. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1762. #else
  1763. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1764. #endif
  1765. target_temperature_bed = 0;
  1766. bed_max_temp_error();
  1767. }
  1768. #endif
  1769. }
  1770. #ifdef BABYSTEPPING
  1771. for(uint8_t axis=0;axis<3;axis++)
  1772. {
  1773. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1774. if(curTodo>0)
  1775. {
  1776. babystep(axis,/*fwd*/true);
  1777. babystepsTodo[axis]--; //less to do next time
  1778. }
  1779. else
  1780. if(curTodo<0)
  1781. {
  1782. babystep(axis,/*fwd*/false);
  1783. babystepsTodo[axis]++; //less to do next time
  1784. }
  1785. }
  1786. #endif //BABYSTEPPING
  1787. }
  1788. #ifdef PIDTEMP
  1789. // Apply the scale factors to the PID values
  1790. float scalePID_i(float i)
  1791. {
  1792. return i*PID_dT;
  1793. }
  1794. float unscalePID_i(float i)
  1795. {
  1796. return i/PID_dT;
  1797. }
  1798. float scalePID_d(float d)
  1799. {
  1800. return d/PID_dT;
  1801. }
  1802. float unscalePID_d(float d)
  1803. {
  1804. return d*PID_dT;
  1805. }
  1806. #endif //PIDTEMP