temperature.cpp 56 KB

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