temperature.cpp 55 KB

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