temperature.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1044. SET_OUTPUT(FAN_PIN);
  1045. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1046. analogWrite(FAN_PIN, 255);
  1047. fanSpeed = 255;
  1048. delayMicroseconds(2000);
  1049. }
  1050. else
  1051. {
  1052. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  1053. }
  1054. }
  1055. #endif
  1056. void disable_heater()
  1057. {
  1058. for(int i=0;i<EXTRUDERS;i++)
  1059. setTargetHotend(0,i);
  1060. setTargetBed(0);
  1061. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1062. target_temperature[0]=0;
  1063. soft_pwm[0]=0;
  1064. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  1065. WRITE(HEATER_0_PIN,LOW);
  1066. #endif
  1067. #endif
  1068. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  1069. target_temperature[1]=0;
  1070. soft_pwm[1]=0;
  1071. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1072. WRITE(HEATER_1_PIN,LOW);
  1073. #endif
  1074. #endif
  1075. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  1076. target_temperature[2]=0;
  1077. soft_pwm[2]=0;
  1078. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1079. WRITE(HEATER_2_PIN,LOW);
  1080. #endif
  1081. #endif
  1082. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1083. target_temperature_bed=0;
  1084. soft_pwm_bed=0;
  1085. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1086. WRITE(HEATER_BED_PIN,LOW);
  1087. #endif
  1088. #endif
  1089. }
  1090. void max_temp_error(uint8_t e) {
  1091. disable_heater();
  1092. if(IsStopped() == false) {
  1093. SERIAL_ERROR_START;
  1094. SERIAL_ERRORLN((int)e);
  1095. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  1096. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  1097. }
  1098. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1099. Stop();
  1100. #endif
  1101. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1102. SET_OUTPUT(FAN_PIN);
  1103. SET_OUTPUT(BEEPER);
  1104. WRITE(FAN_PIN, 1);
  1105. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1106. WRITE(BEEPER, 1);
  1107. // fanSpeed will consumed by the check_axes_activity() routine.
  1108. fanSpeed=255;
  1109. }
  1110. void min_temp_error(uint8_t e) {
  1111. disable_heater();
  1112. if(IsStopped() == false) {
  1113. SERIAL_ERROR_START;
  1114. SERIAL_ERRORLN((int)e);
  1115. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1116. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1117. }
  1118. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1119. Stop();
  1120. #endif
  1121. }
  1122. void bed_max_temp_error(void) {
  1123. #if HEATER_BED_PIN > -1
  1124. WRITE(HEATER_BED_PIN, 0);
  1125. #endif
  1126. if(IsStopped() == false) {
  1127. SERIAL_ERROR_START;
  1128. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !");
  1129. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1130. }
  1131. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1132. Stop();
  1133. #endif
  1134. }
  1135. void bed_min_temp_error(void) {
  1136. #if HEATER_BED_PIN > -1
  1137. WRITE(HEATER_BED_PIN, 0);
  1138. #endif
  1139. if(IsStopped() == false) {
  1140. SERIAL_ERROR_START;
  1141. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MINTEMP triggered !");
  1142. LCD_ALERTMESSAGEPGM("Err: MINTEMP BED");
  1143. }
  1144. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1145. Stop();
  1146. #endif
  1147. }
  1148. #ifdef HEATER_0_USES_MAX6675
  1149. #define MAX6675_HEAT_INTERVAL 250
  1150. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1151. int max6675_temp = 2000;
  1152. int read_max6675()
  1153. {
  1154. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1155. return max6675_temp;
  1156. max6675_previous_millis = millis();
  1157. max6675_temp = 0;
  1158. #ifdef PRR
  1159. PRR &= ~(1<<PRSPI);
  1160. #elif defined PRR0
  1161. PRR0 &= ~(1<<PRSPI);
  1162. #endif
  1163. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1164. // enable TT_MAX6675
  1165. WRITE(MAX6675_SS, 0);
  1166. // ensure 100ns delay - a bit extra is fine
  1167. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1168. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1169. // read MSB
  1170. SPDR = 0;
  1171. for (;(SPSR & (1<<SPIF)) == 0;);
  1172. max6675_temp = SPDR;
  1173. max6675_temp <<= 8;
  1174. // read LSB
  1175. SPDR = 0;
  1176. for (;(SPSR & (1<<SPIF)) == 0;);
  1177. max6675_temp |= SPDR;
  1178. // disable TT_MAX6675
  1179. WRITE(MAX6675_SS, 1);
  1180. if (max6675_temp & 4)
  1181. {
  1182. // thermocouple open
  1183. max6675_temp = 2000;
  1184. }
  1185. else
  1186. {
  1187. max6675_temp = max6675_temp >> 3;
  1188. }
  1189. return max6675_temp;
  1190. }
  1191. #endif
  1192. // Timer 0 is shared with millies
  1193. ISR(TIMER0_COMPB_vect)
  1194. {
  1195. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1196. static unsigned char temp_count = 0;
  1197. static unsigned long raw_temp_0_value = 0;
  1198. static unsigned long raw_temp_1_value = 0;
  1199. static unsigned long raw_temp_2_value = 0;
  1200. static unsigned long raw_temp_bed_value = 0;
  1201. static unsigned char temp_state = 10;
  1202. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1203. static unsigned char soft_pwm_0;
  1204. #ifdef SLOW_PWM_HEATERS
  1205. static unsigned char slow_pwm_count = 0;
  1206. static unsigned char state_heater_0 = 0;
  1207. static unsigned char state_timer_heater_0 = 0;
  1208. #endif
  1209. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1210. static unsigned char soft_pwm_1;
  1211. #ifdef SLOW_PWM_HEATERS
  1212. static unsigned char state_heater_1 = 0;
  1213. static unsigned char state_timer_heater_1 = 0;
  1214. #endif
  1215. #endif
  1216. #if EXTRUDERS > 2
  1217. static unsigned char soft_pwm_2;
  1218. #ifdef SLOW_PWM_HEATERS
  1219. static unsigned char state_heater_2 = 0;
  1220. static unsigned char state_timer_heater_2 = 0;
  1221. #endif
  1222. #endif
  1223. #if HEATER_BED_PIN > -1
  1224. static unsigned char soft_pwm_b;
  1225. #ifdef SLOW_PWM_HEATERS
  1226. static unsigned char state_heater_b = 0;
  1227. static unsigned char state_timer_heater_b = 0;
  1228. #endif
  1229. #endif
  1230. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1231. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1232. #endif
  1233. #ifndef SLOW_PWM_HEATERS
  1234. /*
  1235. * standard PWM modulation
  1236. */
  1237. if(pwm_count == 0){
  1238. soft_pwm_0 = soft_pwm[0];
  1239. if(soft_pwm_0 > 0) {
  1240. WRITE(HEATER_0_PIN,1);
  1241. #ifdef HEATERS_PARALLEL
  1242. WRITE(HEATER_1_PIN,1);
  1243. #endif
  1244. } else WRITE(HEATER_0_PIN,0);
  1245. #if EXTRUDERS > 1
  1246. soft_pwm_1 = soft_pwm[1];
  1247. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1248. #endif
  1249. #if EXTRUDERS > 2
  1250. soft_pwm_2 = soft_pwm[2];
  1251. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1252. #endif
  1253. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1254. soft_pwm_b = soft_pwm_bed;
  1255. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1256. #endif
  1257. #ifdef FAN_SOFT_PWM
  1258. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1259. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1260. #endif
  1261. }
  1262. if(soft_pwm_0 < pwm_count) {
  1263. WRITE(HEATER_0_PIN,0);
  1264. #ifdef HEATERS_PARALLEL
  1265. WRITE(HEATER_1_PIN,0);
  1266. #endif
  1267. }
  1268. #if EXTRUDERS > 1
  1269. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1270. #endif
  1271. #if EXTRUDERS > 2
  1272. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1273. #endif
  1274. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1275. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1276. #endif
  1277. #ifdef FAN_SOFT_PWM
  1278. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1279. #endif
  1280. pwm_count += (1 << SOFT_PWM_SCALE);
  1281. pwm_count &= 0x7f;
  1282. #else //ifndef SLOW_PWM_HEATERS
  1283. /*
  1284. * SLOW PWM HEATERS
  1285. *
  1286. * for heaters drived by relay
  1287. */
  1288. #ifndef MIN_STATE_TIME
  1289. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1290. #endif
  1291. if (slow_pwm_count == 0) {
  1292. // EXTRUDER 0
  1293. soft_pwm_0 = soft_pwm[0];
  1294. if (soft_pwm_0 > 0) {
  1295. // turn ON heather only if the minimum time is up
  1296. if (state_timer_heater_0 == 0) {
  1297. // if change state set timer
  1298. if (state_heater_0 == 0) {
  1299. state_timer_heater_0 = MIN_STATE_TIME;
  1300. }
  1301. state_heater_0 = 1;
  1302. WRITE(HEATER_0_PIN, 1);
  1303. #ifdef HEATERS_PARALLEL
  1304. WRITE(HEATER_1_PIN, 1);
  1305. #endif
  1306. }
  1307. } else {
  1308. // turn OFF heather only if the minimum time is up
  1309. if (state_timer_heater_0 == 0) {
  1310. // if change state set timer
  1311. if (state_heater_0 == 1) {
  1312. state_timer_heater_0 = MIN_STATE_TIME;
  1313. }
  1314. state_heater_0 = 0;
  1315. WRITE(HEATER_0_PIN, 0);
  1316. #ifdef HEATERS_PARALLEL
  1317. WRITE(HEATER_1_PIN, 0);
  1318. #endif
  1319. }
  1320. }
  1321. #if EXTRUDERS > 1
  1322. // EXTRUDER 1
  1323. soft_pwm_1 = soft_pwm[1];
  1324. if (soft_pwm_1 > 0) {
  1325. // turn ON heather only if the minimum time is up
  1326. if (state_timer_heater_1 == 0) {
  1327. // if change state set timer
  1328. if (state_heater_1 == 0) {
  1329. state_timer_heater_1 = MIN_STATE_TIME;
  1330. }
  1331. state_heater_1 = 1;
  1332. WRITE(HEATER_1_PIN, 1);
  1333. }
  1334. } else {
  1335. // turn OFF heather only if the minimum time is up
  1336. if (state_timer_heater_1 == 0) {
  1337. // if change state set timer
  1338. if (state_heater_1 == 1) {
  1339. state_timer_heater_1 = MIN_STATE_TIME;
  1340. }
  1341. state_heater_1 = 0;
  1342. WRITE(HEATER_1_PIN, 0);
  1343. }
  1344. }
  1345. #endif
  1346. #if EXTRUDERS > 2
  1347. // EXTRUDER 2
  1348. soft_pwm_2 = soft_pwm[2];
  1349. if (soft_pwm_2 > 0) {
  1350. // turn ON heather only if the minimum time is up
  1351. if (state_timer_heater_2 == 0) {
  1352. // if change state set timer
  1353. if (state_heater_2 == 0) {
  1354. state_timer_heater_2 = MIN_STATE_TIME;
  1355. }
  1356. state_heater_2 = 1;
  1357. WRITE(HEATER_2_PIN, 1);
  1358. }
  1359. } else {
  1360. // turn OFF heather only if the minimum time is up
  1361. if (state_timer_heater_2 == 0) {
  1362. // if change state set timer
  1363. if (state_heater_2 == 1) {
  1364. state_timer_heater_2 = MIN_STATE_TIME;
  1365. }
  1366. state_heater_2 = 0;
  1367. WRITE(HEATER_2_PIN, 0);
  1368. }
  1369. }
  1370. #endif
  1371. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1372. // BED
  1373. soft_pwm_b = soft_pwm_bed;
  1374. if (soft_pwm_b > 0) {
  1375. // turn ON heather only if the minimum time is up
  1376. if (state_timer_heater_b == 0) {
  1377. // if change state set timer
  1378. if (state_heater_b == 0) {
  1379. state_timer_heater_b = MIN_STATE_TIME;
  1380. }
  1381. state_heater_b = 1;
  1382. WRITE(HEATER_BED_PIN, 1);
  1383. }
  1384. } else {
  1385. // turn OFF heather only if the minimum time is up
  1386. if (state_timer_heater_b == 0) {
  1387. // if change state set timer
  1388. if (state_heater_b == 1) {
  1389. state_timer_heater_b = MIN_STATE_TIME;
  1390. }
  1391. state_heater_b = 0;
  1392. WRITE(HEATER_BED_PIN, 0);
  1393. }
  1394. }
  1395. #endif
  1396. } // if (slow_pwm_count == 0)
  1397. // EXTRUDER 0
  1398. if (soft_pwm_0 < slow_pwm_count) {
  1399. // turn OFF heather only if the minimum time is up
  1400. if (state_timer_heater_0 == 0) {
  1401. // if change state set timer
  1402. if (state_heater_0 == 1) {
  1403. state_timer_heater_0 = MIN_STATE_TIME;
  1404. }
  1405. state_heater_0 = 0;
  1406. WRITE(HEATER_0_PIN, 0);
  1407. #ifdef HEATERS_PARALLEL
  1408. WRITE(HEATER_1_PIN, 0);
  1409. #endif
  1410. }
  1411. }
  1412. #if EXTRUDERS > 1
  1413. // EXTRUDER 1
  1414. if (soft_pwm_1 < slow_pwm_count) {
  1415. // turn OFF heather only if the minimum time is up
  1416. if (state_timer_heater_1 == 0) {
  1417. // if change state set timer
  1418. if (state_heater_1 == 1) {
  1419. state_timer_heater_1 = MIN_STATE_TIME;
  1420. }
  1421. state_heater_1 = 0;
  1422. WRITE(HEATER_1_PIN, 0);
  1423. }
  1424. }
  1425. #endif
  1426. #if EXTRUDERS > 2
  1427. // EXTRUDER 2
  1428. if (soft_pwm_2 < slow_pwm_count) {
  1429. // turn OFF heather only if the minimum time is up
  1430. if (state_timer_heater_2 == 0) {
  1431. // if change state set timer
  1432. if (state_heater_2 == 1) {
  1433. state_timer_heater_2 = MIN_STATE_TIME;
  1434. }
  1435. state_heater_2 = 0;
  1436. WRITE(HEATER_2_PIN, 0);
  1437. }
  1438. }
  1439. #endif
  1440. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1441. // BED
  1442. if (soft_pwm_b < slow_pwm_count) {
  1443. // turn OFF heather only if the minimum time is up
  1444. if (state_timer_heater_b == 0) {
  1445. // if change state set timer
  1446. if (state_heater_b == 1) {
  1447. state_timer_heater_b = MIN_STATE_TIME;
  1448. }
  1449. state_heater_b = 0;
  1450. WRITE(HEATER_BED_PIN, 0);
  1451. }
  1452. }
  1453. #endif
  1454. #ifdef FAN_SOFT_PWM
  1455. if (pwm_count == 0){
  1456. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1457. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1458. }
  1459. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1460. #endif
  1461. pwm_count += (1 << SOFT_PWM_SCALE);
  1462. pwm_count &= 0x7f;
  1463. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1464. if ((pwm_count % 64) == 0) {
  1465. slow_pwm_count++;
  1466. slow_pwm_count &= 0x7f;
  1467. // Extruder 0
  1468. if (state_timer_heater_0 > 0) {
  1469. state_timer_heater_0--;
  1470. }
  1471. #if EXTRUDERS > 1
  1472. // Extruder 1
  1473. if (state_timer_heater_1 > 0)
  1474. state_timer_heater_1--;
  1475. #endif
  1476. #if EXTRUDERS > 2
  1477. // Extruder 2
  1478. if (state_timer_heater_2 > 0)
  1479. state_timer_heater_2--;
  1480. #endif
  1481. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1482. // Bed
  1483. if (state_timer_heater_b > 0)
  1484. state_timer_heater_b--;
  1485. #endif
  1486. } //if ((pwm_count % 64) == 0) {
  1487. #endif //ifndef SLOW_PWM_HEATERS
  1488. switch(temp_state) {
  1489. case 0: // Prepare TEMP_0
  1490. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1491. #if TEMP_0_PIN > 7
  1492. ADCSRB = 1<<MUX5;
  1493. #else
  1494. ADCSRB = 0;
  1495. #endif
  1496. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1497. ADCSRA |= 1<<ADSC; // Start conversion
  1498. #endif
  1499. lcd_buttons_update();
  1500. temp_state = 1;
  1501. break;
  1502. case 1: // Measure TEMP_0
  1503. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1504. raw_temp_0_value += ADC;
  1505. #endif
  1506. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1507. raw_temp_0_value = read_max6675();
  1508. #endif
  1509. temp_state = 2;
  1510. break;
  1511. case 2: // Prepare TEMP_BED
  1512. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1513. #if TEMP_BED_PIN > 7
  1514. ADCSRB = 1<<MUX5;
  1515. #else
  1516. ADCSRB = 0;
  1517. #endif
  1518. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1519. ADCSRA |= 1<<ADSC; // Start conversion
  1520. #endif
  1521. lcd_buttons_update();
  1522. temp_state = 3;
  1523. break;
  1524. case 3: // Measure TEMP_BED
  1525. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1526. raw_temp_bed_value += ADC;
  1527. #endif
  1528. temp_state = 4;
  1529. break;
  1530. case 4: // Prepare TEMP_1
  1531. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1532. #if TEMP_1_PIN > 7
  1533. ADCSRB = 1<<MUX5;
  1534. #else
  1535. ADCSRB = 0;
  1536. #endif
  1537. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1538. ADCSRA |= 1<<ADSC; // Start conversion
  1539. #endif
  1540. lcd_buttons_update();
  1541. temp_state = 5;
  1542. break;
  1543. case 5: // Measure TEMP_1
  1544. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1545. raw_temp_1_value += ADC;
  1546. #endif
  1547. temp_state = 6;
  1548. break;
  1549. case 6: // Prepare TEMP_2
  1550. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1551. #if TEMP_2_PIN > 7
  1552. ADCSRB = 1<<MUX5;
  1553. #else
  1554. ADCSRB = 0;
  1555. #endif
  1556. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1557. ADCSRA |= 1<<ADSC; // Start conversion
  1558. #endif
  1559. lcd_buttons_update();
  1560. temp_state = 7;
  1561. break;
  1562. case 7: // Measure TEMP_2
  1563. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1564. raw_temp_2_value += ADC;
  1565. #endif
  1566. temp_state = 8;//change so that Filament Width is also measured
  1567. break;
  1568. case 8: //Prepare FILWIDTH
  1569. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1570. #if FILWIDTH_PIN>7
  1571. ADCSRB = 1<<MUX5;
  1572. #else
  1573. ADCSRB = 0;
  1574. #endif
  1575. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1576. ADCSRA |= 1<<ADSC; // Start conversion
  1577. #endif
  1578. lcd_buttons_update();
  1579. temp_state = 9;
  1580. break;
  1581. case 9: //Measure FILWIDTH
  1582. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1583. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1584. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1585. {
  1586. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1587. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1588. }
  1589. #endif
  1590. temp_state = 0;
  1591. temp_count++;
  1592. break;
  1593. case 10: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1594. temp_state = 0;
  1595. break;
  1596. // default:
  1597. // SERIAL_ERROR_START;
  1598. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1599. // break;
  1600. }
  1601. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1602. {
  1603. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1604. {
  1605. current_temperature_raw[0] = raw_temp_0_value;
  1606. #if EXTRUDERS > 1
  1607. current_temperature_raw[1] = raw_temp_1_value;
  1608. #endif
  1609. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1610. redundant_temperature_raw = raw_temp_1_value;
  1611. #endif
  1612. #if EXTRUDERS > 2
  1613. current_temperature_raw[2] = raw_temp_2_value;
  1614. #endif
  1615. current_temperature_bed_raw = raw_temp_bed_value;
  1616. }
  1617. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1618. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1619. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1620. #endif
  1621. temp_meas_ready = true;
  1622. temp_count = 0;
  1623. raw_temp_0_value = 0;
  1624. raw_temp_1_value = 0;
  1625. raw_temp_2_value = 0;
  1626. raw_temp_bed_value = 0;
  1627. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1628. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1629. #else
  1630. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1631. #endif
  1632. max_temp_error(0);
  1633. }
  1634. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1635. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1636. #else
  1637. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1638. #endif
  1639. min_temp_error(0);
  1640. }
  1641. #if EXTRUDERS > 1
  1642. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1643. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1644. #else
  1645. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1646. #endif
  1647. max_temp_error(1);
  1648. }
  1649. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1650. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1651. #else
  1652. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1653. #endif
  1654. min_temp_error(1);
  1655. }
  1656. #endif
  1657. #if EXTRUDERS > 2
  1658. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1659. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1660. #else
  1661. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1662. #endif
  1663. max_temp_error(2);
  1664. }
  1665. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1666. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1667. #else
  1668. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1669. #endif
  1670. min_temp_error(2);
  1671. }
  1672. #endif
  1673. /* No bed MINTEMP error? */
  1674. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1675. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1676. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1677. #else
  1678. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1679. #endif
  1680. target_temperature_bed = 0;
  1681. bed_max_temp_error();
  1682. }
  1683. }
  1684. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1685. if(current_temperature_bed_raw >= bed_minttemp_raw) {
  1686. #else
  1687. if(current_temperature_bed_raw <= bed_minttemp_raw) {
  1688. #endif
  1689. bed_min_temp_error();
  1690. }
  1691. #endif
  1692. #ifdef BABYSTEPPING
  1693. for(uint8_t axis=0;axis<3;axis++)
  1694. {
  1695. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1696. if(curTodo>0)
  1697. {
  1698. babystep(axis,/*fwd*/true);
  1699. babystepsTodo[axis]--; //less to do next time
  1700. }
  1701. else
  1702. if(curTodo<0)
  1703. {
  1704. babystep(axis,/*fwd*/false);
  1705. babystepsTodo[axis]++; //less to do next time
  1706. }
  1707. }
  1708. #endif //BABYSTEPPING
  1709. }
  1710. #ifdef PIDTEMP
  1711. // Apply the scale factors to the PID values
  1712. float scalePID_i(float i)
  1713. {
  1714. return i*PID_dT;
  1715. }
  1716. float unscalePID_i(float i)
  1717. {
  1718. return i/PID_dT;
  1719. }
  1720. float scalePID_d(float d)
  1721. {
  1722. return d/PID_dT;
  1723. }
  1724. float unscalePID_d(float d)
  1725. {
  1726. return d*PID_dT;
  1727. }
  1728. #endif //PIDTEMP