temperature.cpp 58 KB

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