temperature.cpp 57 KB

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