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

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