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

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