temperature.cpp 58 KB

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