temperature.cpp 56 KB

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