temperature.cpp 54 KB

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