temperature.cpp 55 KB

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