temperature.cpp 57 KB

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