temperature.cpp 58 KB

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