temperature.cpp 57 KB

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