temperature.cpp 62 KB

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