temperature.cpp 56 KB

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