My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 56KB

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