My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

temperature.cpp 56KB

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