temperature.cpp 57 KB

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