temperature.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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. #ifdef HOST_KEEPALIVE_FEATURE
  691. host_keepalive();
  692. #endif
  693. }
  694. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  695. // Derived from RepRap FiveD extruder::getTemperature()
  696. // For hot end temperature measurement.
  697. static float analog2temp(int raw, uint8_t e) {
  698. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  699. if(e > EXTRUDERS)
  700. #else
  701. if(e >= EXTRUDERS)
  702. #endif
  703. {
  704. SERIAL_ERROR_START;
  705. SERIAL_ERROR((int)e);
  706. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  707. kill("", 6);
  708. return 0.0;
  709. }
  710. #ifdef HEATER_0_USES_MAX6675
  711. if (e == 0)
  712. {
  713. return 0.25 * raw;
  714. }
  715. #endif
  716. if(heater_ttbl_map[e] != NULL)
  717. {
  718. float celsius = 0;
  719. uint8_t i;
  720. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  721. for (i=1; i<heater_ttbllen_map[e]; i++)
  722. {
  723. if (PGM_RD_W((*tt)[i][0]) > raw)
  724. {
  725. celsius = PGM_RD_W((*tt)[i-1][1]) +
  726. (raw - PGM_RD_W((*tt)[i-1][0])) *
  727. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  728. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  729. break;
  730. }
  731. }
  732. // Overflow: Set to last value in the table
  733. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  734. return celsius;
  735. }
  736. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  737. }
  738. // Derived from RepRap FiveD extruder::getTemperature()
  739. // For bed temperature measurement.
  740. static float analog2tempBed(int raw) {
  741. #ifdef BED_USES_THERMISTOR
  742. float celsius = 0;
  743. byte i;
  744. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  745. {
  746. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  747. {
  748. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  749. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  750. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  751. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  752. break;
  753. }
  754. }
  755. // Overflow: Set to last value in the table
  756. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  757. // temperature offset adjustment
  758. #ifdef BED_OFFSET
  759. float _offset = BED_OFFSET;
  760. float _offset_center = BED_OFFSET_CENTER;
  761. float _offset_start = BED_OFFSET_START;
  762. float _first_koef = (_offset / 2) / (_offset_center - _offset_start);
  763. float _second_koef = (_offset / 2) / (100 - _offset_center);
  764. if (celsius >= _offset_start && celsius <= _offset_center)
  765. {
  766. celsius = celsius + (_first_koef * (celsius - _offset_start));
  767. }
  768. else if (celsius > _offset_center && celsius <= 100)
  769. {
  770. celsius = celsius + (_first_koef * (_offset_center - _offset_start)) + ( _second_koef * ( celsius - ( 100 - _offset_center ) )) ;
  771. }
  772. else if (celsius > 100)
  773. {
  774. celsius = celsius + _offset;
  775. }
  776. #endif
  777. return celsius;
  778. #elif defined BED_USES_AD595
  779. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  780. #else
  781. return 0;
  782. #endif
  783. }
  784. static float analog2tempAmbient(int raw)
  785. {
  786. float celsius = 0;
  787. byte i;
  788. for (i=1; i<AMBIENTTEMPTABLE_LEN; i++)
  789. {
  790. if (PGM_RD_W(AMBIENTTEMPTABLE[i][0]) > raw)
  791. {
  792. celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]) +
  793. (raw - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0])) *
  794. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][1]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][1])) /
  795. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][0]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0]));
  796. break;
  797. }
  798. }
  799. // Overflow: Set to last value in the table
  800. if (i == AMBIENTTEMPTABLE_LEN) celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]);
  801. return celsius;
  802. }
  803. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  804. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  805. static void updateTemperaturesFromRawValues()
  806. {
  807. for(uint8_t e=0;e<EXTRUDERS;e++)
  808. {
  809. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  810. }
  811. #ifdef PINDA_THERMISTOR
  812. current_temperature_pinda = analog2tempBed(current_temperature_raw_pinda); //thermistor for pinda is the same as for bed
  813. #endif
  814. #ifdef AMBIENT_THERMISTOR
  815. current_temperature_ambient = analog2tempAmbient(current_temperature_raw_ambient); //thermistor for ambient is NTCG104LH104JT1 (2000)
  816. #endif
  817. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  818. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  819. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  820. #endif
  821. #if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
  822. filament_width_meas = analog2widthFil();
  823. #endif
  824. //Reset the watchdog after we know we have a temperature measurement.
  825. watchdog_reset();
  826. CRITICAL_SECTION_START;
  827. temp_meas_ready = false;
  828. CRITICAL_SECTION_END;
  829. }
  830. // For converting raw Filament Width to milimeters
  831. #ifdef FILAMENT_SENSOR
  832. float analog2widthFil() {
  833. return current_raw_filwidth/16383.0*5.0;
  834. //return current_raw_filwidth;
  835. }
  836. // For converting raw Filament Width to a ratio
  837. int widthFil_to_size_ratio() {
  838. float temp;
  839. temp=filament_width_meas;
  840. if(filament_width_meas<MEASURED_LOWER_LIMIT)
  841. temp=filament_width_nominal; //assume sensor cut out
  842. else if (filament_width_meas>MEASURED_UPPER_LIMIT)
  843. temp= MEASURED_UPPER_LIMIT;
  844. return(filament_width_nominal/temp*100);
  845. }
  846. #endif
  847. void tp_init()
  848. {
  849. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  850. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  851. MCUCR=(1<<JTD);
  852. MCUCR=(1<<JTD);
  853. #endif
  854. // Finish init of mult extruder arrays
  855. for(int e = 0; e < EXTRUDERS; e++) {
  856. // populate with the first value
  857. maxttemp[e] = maxttemp[0];
  858. #ifdef PIDTEMP
  859. temp_iState_min[e] = 0.0;
  860. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  861. #endif //PIDTEMP
  862. #ifdef PIDTEMPBED
  863. temp_iState_min_bed = 0.0;
  864. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  865. #endif //PIDTEMPBED
  866. }
  867. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  868. SET_OUTPUT(HEATER_0_PIN);
  869. #endif
  870. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  871. SET_OUTPUT(HEATER_1_PIN);
  872. #endif
  873. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  874. SET_OUTPUT(HEATER_2_PIN);
  875. #endif
  876. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  877. SET_OUTPUT(HEATER_BED_PIN);
  878. #endif
  879. #if defined(FAN_PIN) && (FAN_PIN > -1)
  880. SET_OUTPUT(FAN_PIN);
  881. #ifdef FAST_PWM_FAN
  882. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  883. #endif
  884. #ifdef FAN_SOFT_PWM
  885. soft_pwm_fan = fanSpeedSoftPwm / 2;
  886. #endif
  887. #if defined(LCD_PWM_PIN) && (LCD_PWM_PIN > -1)
  888. soft_pwm_lcd = lcdSoftPwm / 2;
  889. lcd_blink_delay = lcdBlinkDelay;
  890. lcd_blink_on = true;
  891. #endif
  892. #endif
  893. #ifdef HEATER_0_USES_MAX6675
  894. #ifndef SDSUPPORT
  895. SET_OUTPUT(SCK_PIN);
  896. WRITE(SCK_PIN,0);
  897. SET_OUTPUT(MOSI_PIN);
  898. WRITE(MOSI_PIN,1);
  899. SET_INPUT(MISO_PIN);
  900. WRITE(MISO_PIN,1);
  901. #endif
  902. /* Using pinMode and digitalWrite, as that was the only way I could get it to compile */
  903. //Have to toggle SD card CS pin to low first, to enable firmware to talk with SD card
  904. pinMode(SS_PIN, OUTPUT);
  905. digitalWrite(SS_PIN,0);
  906. pinMode(MAX6675_SS, OUTPUT);
  907. digitalWrite(MAX6675_SS,1);
  908. #endif
  909. // Set analog inputs
  910. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  911. DIDR0 = 0;
  912. #ifdef DIDR2
  913. DIDR2 = 0;
  914. #endif
  915. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  916. #if TEMP_0_PIN < 8
  917. DIDR0 |= 1 << TEMP_0_PIN;
  918. #else
  919. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  920. #endif
  921. #endif
  922. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  923. #if TEMP_1_PIN < 8
  924. DIDR0 |= 1<<TEMP_1_PIN;
  925. #else
  926. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  927. #endif
  928. #endif
  929. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  930. #if TEMP_2_PIN < 8
  931. DIDR0 |= 1 << TEMP_2_PIN;
  932. #else
  933. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  934. #endif
  935. #endif
  936. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  937. #if TEMP_BED_PIN < 8
  938. DIDR0 |= 1<<TEMP_BED_PIN;
  939. #else
  940. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  941. #endif
  942. #endif
  943. //Added for Filament Sensor
  944. #ifdef FILAMENT_SENSOR
  945. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
  946. #if FILWIDTH_PIN < 8
  947. DIDR0 |= 1<<FILWIDTH_PIN;
  948. #else
  949. DIDR2 |= 1<<(FILWIDTH_PIN - 8);
  950. #endif
  951. #endif
  952. #endif
  953. // Use timer0 for temperature measurement
  954. // Interleave temperature interrupt with millies interrupt
  955. OCR0B = 128;
  956. TIMSK0 |= (1<<OCIE0B);
  957. // Wait for temperature measurement to settle
  958. delay(250);
  959. #ifdef HEATER_0_MINTEMP
  960. minttemp[0] = HEATER_0_MINTEMP;
  961. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  962. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  963. minttemp_raw[0] += OVERSAMPLENR;
  964. #else
  965. minttemp_raw[0] -= OVERSAMPLENR;
  966. #endif
  967. }
  968. #endif //MINTEMP
  969. #ifdef HEATER_0_MAXTEMP
  970. maxttemp[0] = HEATER_0_MAXTEMP;
  971. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  972. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  973. maxttemp_raw[0] -= OVERSAMPLENR;
  974. #else
  975. maxttemp_raw[0] += OVERSAMPLENR;
  976. #endif
  977. }
  978. #endif //MAXTEMP
  979. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  980. minttemp[1] = HEATER_1_MINTEMP;
  981. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  982. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  983. minttemp_raw[1] += OVERSAMPLENR;
  984. #else
  985. minttemp_raw[1] -= OVERSAMPLENR;
  986. #endif
  987. }
  988. #endif // MINTEMP 1
  989. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  990. maxttemp[1] = HEATER_1_MAXTEMP;
  991. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  992. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  993. maxttemp_raw[1] -= OVERSAMPLENR;
  994. #else
  995. maxttemp_raw[1] += OVERSAMPLENR;
  996. #endif
  997. }
  998. #endif //MAXTEMP 1
  999. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  1000. minttemp[2] = HEATER_2_MINTEMP;
  1001. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  1002. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  1003. minttemp_raw[2] += OVERSAMPLENR;
  1004. #else
  1005. minttemp_raw[2] -= OVERSAMPLENR;
  1006. #endif
  1007. }
  1008. #endif //MINTEMP 2
  1009. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  1010. maxttemp[2] = HEATER_2_MAXTEMP;
  1011. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  1012. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  1013. maxttemp_raw[2] -= OVERSAMPLENR;
  1014. #else
  1015. maxttemp_raw[2] += OVERSAMPLENR;
  1016. #endif
  1017. }
  1018. #endif //MAXTEMP 2
  1019. #ifdef BED_MINTEMP
  1020. /* No bed MINTEMP error implemented?!? */
  1021. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1022. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1023. bed_minttemp_raw += OVERSAMPLENR;
  1024. #else
  1025. bed_minttemp_raw -= OVERSAMPLENR;
  1026. #endif
  1027. }
  1028. #endif //BED_MINTEMP
  1029. #ifdef BED_MAXTEMP
  1030. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1031. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1032. bed_maxttemp_raw -= OVERSAMPLENR;
  1033. #else
  1034. bed_maxttemp_raw += OVERSAMPLENR;
  1035. #endif
  1036. }
  1037. #endif //BED_MAXTEMP
  1038. }
  1039. void setWatch()
  1040. {
  1041. #ifdef WATCH_TEMP_PERIOD
  1042. for (int e = 0; e < EXTRUDERS; e++)
  1043. {
  1044. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  1045. {
  1046. watch_start_temp[e] = degHotend(e);
  1047. watchmillis[e] = millis();
  1048. }
  1049. }
  1050. #endif
  1051. }
  1052. #if (defined (TEMP_RUNAWAY_BED_HYSTERESIS) && TEMP_RUNAWAY_BED_TIMEOUT > 0) || (defined (TEMP_RUNAWAY_EXTRUDER_HYSTERESIS) && TEMP_RUNAWAY_EXTRUDER_TIMEOUT > 0)
  1053. void temp_runaway_check(int _heater_id, float _target_temperature, float _current_temperature, float _output, bool _isbed)
  1054. {
  1055. float __hysteresis = 0;
  1056. int __timeout = 0;
  1057. bool temp_runaway_check_active = false;
  1058. static float __preheat_start[2] = { 0,0}; //currently just bed and one extruder
  1059. static int __preheat_counter[2] = { 0,0};
  1060. static int __preheat_errors[2] = { 0,0};
  1061. #ifdef TEMP_RUNAWAY_BED_TIMEOUT
  1062. if (_isbed)
  1063. {
  1064. __hysteresis = TEMP_RUNAWAY_BED_HYSTERESIS;
  1065. __timeout = TEMP_RUNAWAY_BED_TIMEOUT;
  1066. }
  1067. #endif
  1068. #ifdef TEMP_RUNAWAY_EXTRUDER_TIMEOUT
  1069. if (!_isbed)
  1070. {
  1071. __hysteresis = TEMP_RUNAWAY_EXTRUDER_HYSTERESIS;
  1072. __timeout = TEMP_RUNAWAY_EXTRUDER_TIMEOUT;
  1073. }
  1074. #endif
  1075. if (millis() - temp_runaway_timer[_heater_id] > 2000)
  1076. {
  1077. temp_runaway_timer[_heater_id] = millis();
  1078. if (_output == 0)
  1079. {
  1080. temp_runaway_check_active = false;
  1081. temp_runaway_error_counter[_heater_id] = 0;
  1082. }
  1083. if (temp_runaway_target[_heater_id] != _target_temperature)
  1084. {
  1085. if (_target_temperature > 0)
  1086. {
  1087. temp_runaway_status[_heater_id] = TempRunaway_PREHEAT;
  1088. temp_runaway_target[_heater_id] = _target_temperature;
  1089. __preheat_start[_heater_id] = _current_temperature;
  1090. __preheat_counter[_heater_id] = 0;
  1091. }
  1092. else
  1093. {
  1094. temp_runaway_status[_heater_id] = TempRunaway_INACTIVE;
  1095. temp_runaway_target[_heater_id] = _target_temperature;
  1096. }
  1097. }
  1098. if (temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  1099. {
  1100. 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
  1101. {
  1102. __preheat_counter[_heater_id]++;
  1103. if (__preheat_counter[_heater_id] > ((_isbed) ? 16 : 8)) // periodicaly check if current temperature changes
  1104. {
  1105. /*SERIAL_ECHOPGM("Heater:");
  1106. MYSERIAL.print(_heater_id);
  1107. SERIAL_ECHOPGM(" T:");
  1108. MYSERIAL.print(_current_temperature);
  1109. SERIAL_ECHOPGM(" Tstart:");
  1110. MYSERIAL.print(__preheat_start[_heater_id]);*/
  1111. if (_current_temperature - __preheat_start[_heater_id] < 2) {
  1112. __preheat_errors[_heater_id]++;
  1113. /*SERIAL_ECHOPGM(" Preheat errors:");
  1114. MYSERIAL.println(__preheat_errors[_heater_id]);*/
  1115. }
  1116. else {
  1117. //SERIAL_ECHOLNPGM("");
  1118. __preheat_errors[_heater_id] = 0;
  1119. }
  1120. if (__preheat_errors[_heater_id] > ((_isbed) ? 2 : 5))
  1121. {
  1122. if (farm_mode) { prusa_statistics(0); }
  1123. temp_runaway_stop(true, _isbed);
  1124. if (farm_mode) { prusa_statistics(91); }
  1125. }
  1126. __preheat_start[_heater_id] = _current_temperature;
  1127. __preheat_counter[_heater_id] = 0;
  1128. }
  1129. }
  1130. }
  1131. if (_current_temperature >= _target_temperature && temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  1132. {
  1133. temp_runaway_status[_heater_id] = TempRunaway_ACTIVE;
  1134. temp_runaway_check_active = false;
  1135. }
  1136. if (!temp_runaway_check_active && _output > 0)
  1137. {
  1138. temp_runaway_check_active = true;
  1139. }
  1140. if (temp_runaway_check_active)
  1141. {
  1142. // we are in range
  1143. if (_target_temperature - __hysteresis < _current_temperature && _current_temperature < _target_temperature + __hysteresis)
  1144. {
  1145. temp_runaway_check_active = false;
  1146. temp_runaway_error_counter[_heater_id] = 0;
  1147. }
  1148. else
  1149. {
  1150. if (temp_runaway_status[_heater_id] > TempRunaway_PREHEAT)
  1151. {
  1152. temp_runaway_error_counter[_heater_id]++;
  1153. if (temp_runaway_error_counter[_heater_id] * 2 > __timeout)
  1154. {
  1155. if (farm_mode) { prusa_statistics(0); }
  1156. temp_runaway_stop(false, _isbed);
  1157. if (farm_mode) { prusa_statistics(90); }
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. void temp_runaway_stop(bool isPreheat, bool isBed)
  1165. {
  1166. cancel_heatup = true;
  1167. quickStop();
  1168. if (card.sdprinting)
  1169. {
  1170. card.sdprinting = false;
  1171. card.closefile();
  1172. }
  1173. disable_heater();
  1174. disable_x();
  1175. disable_y();
  1176. disable_e0();
  1177. disable_e1();
  1178. disable_e2();
  1179. manage_heater();
  1180. lcd_update();
  1181. WRITE(BEEPER, HIGH);
  1182. delayMicroseconds(500);
  1183. WRITE(BEEPER, LOW);
  1184. delayMicroseconds(100);
  1185. if (isPreheat)
  1186. {
  1187. Stop();
  1188. isBed ? LCD_ALERTMESSAGEPGM("BED PREHEAT ERROR") : LCD_ALERTMESSAGEPGM("PREHEAT ERROR");
  1189. SERIAL_ERROR_START;
  1190. isBed ? SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HEATBED)") : SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HOTEND)");
  1191. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1192. SET_OUTPUT(FAN_PIN);
  1193. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1194. analogWrite(FAN_PIN, 255);
  1195. fanSpeed = 255;
  1196. delayMicroseconds(2000);
  1197. }
  1198. else
  1199. {
  1200. isBed ? LCD_ALERTMESSAGEPGM("BED THERMAL RUNAWAY") : LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  1201. SERIAL_ERROR_START;
  1202. isBed ? SERIAL_ERRORLNPGM(" HEATBED THERMAL RUNAWAY") : SERIAL_ERRORLNPGM(" HOTEND THERMAL RUNAWAY");
  1203. }
  1204. }
  1205. #endif
  1206. void disable_heater()
  1207. {
  1208. for(int i=0;i<EXTRUDERS;i++)
  1209. setTargetHotend(0,i);
  1210. setTargetBed(0);
  1211. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1212. target_temperature[0]=0;
  1213. soft_pwm[0]=0;
  1214. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  1215. WRITE(HEATER_0_PIN,LOW);
  1216. #endif
  1217. #endif
  1218. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  1219. target_temperature[1]=0;
  1220. soft_pwm[1]=0;
  1221. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1222. WRITE(HEATER_1_PIN,LOW);
  1223. #endif
  1224. #endif
  1225. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  1226. target_temperature[2]=0;
  1227. soft_pwm[2]=0;
  1228. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1229. WRITE(HEATER_2_PIN,LOW);
  1230. #endif
  1231. #endif
  1232. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1233. target_temperature_bed=0;
  1234. soft_pwm_bed=0;
  1235. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1236. WRITE(HEATER_BED_PIN,LOW);
  1237. #endif
  1238. #endif
  1239. }
  1240. void max_temp_error(uint8_t e) {
  1241. disable_heater();
  1242. if(IsStopped() == false) {
  1243. SERIAL_ERROR_START;
  1244. SERIAL_ERRORLN((int)e);
  1245. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  1246. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  1247. }
  1248. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1249. Stop();
  1250. #endif
  1251. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1252. SET_OUTPUT(FAN_PIN);
  1253. SET_OUTPUT(BEEPER);
  1254. WRITE(FAN_PIN, 1);
  1255. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1256. WRITE(BEEPER, 1);
  1257. // fanSpeed will consumed by the check_axes_activity() routine.
  1258. fanSpeed=255;
  1259. if (farm_mode) { prusa_statistics(93); }
  1260. }
  1261. void min_temp_error(uint8_t e) {
  1262. #ifdef DEBUG_DISABLE_MINTEMP
  1263. return;
  1264. #endif
  1265. disable_heater();
  1266. if(IsStopped() == false) {
  1267. SERIAL_ERROR_START;
  1268. SERIAL_ERRORLN((int)e);
  1269. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1270. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1271. }
  1272. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1273. Stop();
  1274. #endif
  1275. if (farm_mode) { prusa_statistics(92); }
  1276. }
  1277. void bed_max_temp_error(void) {
  1278. #if HEATER_BED_PIN > -1
  1279. WRITE(HEATER_BED_PIN, 0);
  1280. #endif
  1281. if(IsStopped() == false) {
  1282. SERIAL_ERROR_START;
  1283. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !");
  1284. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1285. }
  1286. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1287. Stop();
  1288. #endif
  1289. }
  1290. void bed_min_temp_error(void) {
  1291. #ifdef DEBUG_DISABLE_MINTEMP
  1292. return;
  1293. #endif
  1294. #if HEATER_BED_PIN > -1
  1295. WRITE(HEATER_BED_PIN, 0);
  1296. #endif
  1297. if(IsStopped() == false) {
  1298. SERIAL_ERROR_START;
  1299. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MINTEMP triggered !");
  1300. LCD_ALERTMESSAGEPGM("Err: MINTEMP BED");
  1301. }
  1302. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1303. Stop();
  1304. #endif*/
  1305. }
  1306. #ifdef HEATER_0_USES_MAX6675
  1307. #define MAX6675_HEAT_INTERVAL 250
  1308. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1309. int max6675_temp = 2000;
  1310. int read_max6675()
  1311. {
  1312. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1313. return max6675_temp;
  1314. max6675_previous_millis = millis();
  1315. max6675_temp = 0;
  1316. #ifdef PRR
  1317. PRR &= ~(1<<PRSPI);
  1318. #elif defined PRR0
  1319. PRR0 &= ~(1<<PRSPI);
  1320. #endif
  1321. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1322. // enable TT_MAX6675
  1323. WRITE(MAX6675_SS, 0);
  1324. // ensure 100ns delay - a bit extra is fine
  1325. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1326. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1327. // read MSB
  1328. SPDR = 0;
  1329. for (;(SPSR & (1<<SPIF)) == 0;);
  1330. max6675_temp = SPDR;
  1331. max6675_temp <<= 8;
  1332. // read LSB
  1333. SPDR = 0;
  1334. for (;(SPSR & (1<<SPIF)) == 0;);
  1335. max6675_temp |= SPDR;
  1336. // disable TT_MAX6675
  1337. WRITE(MAX6675_SS, 1);
  1338. if (max6675_temp & 4)
  1339. {
  1340. // thermocouple open
  1341. max6675_temp = 2000;
  1342. }
  1343. else
  1344. {
  1345. max6675_temp = max6675_temp >> 3;
  1346. }
  1347. return max6675_temp;
  1348. }
  1349. #endif
  1350. // Timer 0 is shared with millies
  1351. ISR(TIMER0_COMPB_vect)
  1352. {
  1353. // if (UVLO) uvlo();
  1354. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1355. static unsigned char temp_count = 0;
  1356. static unsigned long raw_temp_0_value = 0;
  1357. static unsigned long raw_temp_1_value = 0;
  1358. static unsigned long raw_temp_2_value = 0;
  1359. static unsigned long raw_temp_bed_value = 0;
  1360. static unsigned long raw_temp_pinda_value = 0;
  1361. static unsigned long raw_temp_ambient_value = 0;
  1362. static unsigned char temp_state = 14;
  1363. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1364. static unsigned char soft_pwm_0;
  1365. #ifdef SLOW_PWM_HEATERS
  1366. static unsigned char slow_pwm_count = 0;
  1367. static unsigned char state_heater_0 = 0;
  1368. static unsigned char state_timer_heater_0 = 0;
  1369. #endif
  1370. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1371. static unsigned char soft_pwm_1;
  1372. #ifdef SLOW_PWM_HEATERS
  1373. static unsigned char state_heater_1 = 0;
  1374. static unsigned char state_timer_heater_1 = 0;
  1375. #endif
  1376. #endif
  1377. #if EXTRUDERS > 2
  1378. static unsigned char soft_pwm_2;
  1379. #ifdef SLOW_PWM_HEATERS
  1380. static unsigned char state_heater_2 = 0;
  1381. static unsigned char state_timer_heater_2 = 0;
  1382. #endif
  1383. #endif
  1384. #if HEATER_BED_PIN > -1
  1385. static unsigned char soft_pwm_b;
  1386. #ifdef SLOW_PWM_HEATERS
  1387. static unsigned char state_heater_b = 0;
  1388. static unsigned char state_timer_heater_b = 0;
  1389. #endif
  1390. #endif
  1391. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1392. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1393. #endif
  1394. #ifndef SLOW_PWM_HEATERS
  1395. /*
  1396. * standard PWM modulation
  1397. */
  1398. if (pwm_count == 0)
  1399. {
  1400. soft_pwm_0 = soft_pwm[0];
  1401. if(soft_pwm_0 > 0)
  1402. {
  1403. WRITE(HEATER_0_PIN,1);
  1404. #ifdef HEATERS_PARALLEL
  1405. WRITE(HEATER_1_PIN,1);
  1406. #endif
  1407. } else WRITE(HEATER_0_PIN,0);
  1408. #if EXTRUDERS > 1
  1409. soft_pwm_1 = soft_pwm[1];
  1410. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1411. #endif
  1412. #if EXTRUDERS > 2
  1413. soft_pwm_2 = soft_pwm[2];
  1414. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1415. #endif
  1416. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1417. soft_pwm_b = soft_pwm_bed;
  1418. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1419. #endif
  1420. #ifdef FAN_SOFT_PWM
  1421. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1422. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1423. #endif
  1424. }
  1425. if(soft_pwm_0 < pwm_count)
  1426. {
  1427. WRITE(HEATER_0_PIN,0);
  1428. #ifdef HEATERS_PARALLEL
  1429. WRITE(HEATER_1_PIN,0);
  1430. #endif
  1431. }
  1432. #if defined(LCD_PWM_PIN) && (LCD_PWM_PIN > -1)
  1433. if ((pwm_count & LCD_PWM_MAX) == 0)
  1434. {
  1435. if (lcd_blink_delay)
  1436. {
  1437. lcd_blink_delay--;
  1438. if (lcd_blink_delay == 0)
  1439. {
  1440. lcd_blink_delay = lcdBlinkDelay;
  1441. lcd_blink_on = !lcd_blink_on;
  1442. }
  1443. }
  1444. else
  1445. {
  1446. lcd_blink_delay = lcdBlinkDelay;
  1447. lcd_blink_on = true;
  1448. }
  1449. soft_pwm_lcd = (lcd_blink_on) ? (lcdSoftPwm / 2) : 0;
  1450. if (soft_pwm_lcd > 0) WRITE(LCD_PWM_PIN,1); else WRITE(LCD_PWM_PIN,0);
  1451. }
  1452. #endif
  1453. #if EXTRUDERS > 1
  1454. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1455. #endif
  1456. #if EXTRUDERS > 2
  1457. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1458. #endif
  1459. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1460. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1461. #endif
  1462. #ifdef FAN_SOFT_PWM
  1463. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1464. #endif
  1465. #if defined(LCD_PWM_PIN) && (LCD_PWM_PIN > -1)
  1466. if (soft_pwm_lcd < (pwm_count & LCD_PWM_MAX)) WRITE(LCD_PWM_PIN,0);
  1467. #endif
  1468. pwm_count += (1 << SOFT_PWM_SCALE);
  1469. pwm_count &= 0x7f;
  1470. #else //ifndef SLOW_PWM_HEATERS
  1471. /*
  1472. * SLOW PWM HEATERS
  1473. *
  1474. * for heaters drived by relay
  1475. */
  1476. #ifndef MIN_STATE_TIME
  1477. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1478. #endif
  1479. if (slow_pwm_count == 0) {
  1480. // EXTRUDER 0
  1481. soft_pwm_0 = soft_pwm[0];
  1482. if (soft_pwm_0 > 0) {
  1483. // turn ON heather only if the minimum time is up
  1484. if (state_timer_heater_0 == 0) {
  1485. // if change state set timer
  1486. if (state_heater_0 == 0) {
  1487. state_timer_heater_0 = MIN_STATE_TIME;
  1488. }
  1489. state_heater_0 = 1;
  1490. WRITE(HEATER_0_PIN, 1);
  1491. #ifdef HEATERS_PARALLEL
  1492. WRITE(HEATER_1_PIN, 1);
  1493. #endif
  1494. }
  1495. } else {
  1496. // turn OFF heather only if the minimum time is up
  1497. if (state_timer_heater_0 == 0) {
  1498. // if change state set timer
  1499. if (state_heater_0 == 1) {
  1500. state_timer_heater_0 = MIN_STATE_TIME;
  1501. }
  1502. state_heater_0 = 0;
  1503. WRITE(HEATER_0_PIN, 0);
  1504. #ifdef HEATERS_PARALLEL
  1505. WRITE(HEATER_1_PIN, 0);
  1506. #endif
  1507. }
  1508. }
  1509. #if EXTRUDERS > 1
  1510. // EXTRUDER 1
  1511. soft_pwm_1 = soft_pwm[1];
  1512. if (soft_pwm_1 > 0) {
  1513. // turn ON heather only if the minimum time is up
  1514. if (state_timer_heater_1 == 0) {
  1515. // if change state set timer
  1516. if (state_heater_1 == 0) {
  1517. state_timer_heater_1 = MIN_STATE_TIME;
  1518. }
  1519. state_heater_1 = 1;
  1520. WRITE(HEATER_1_PIN, 1);
  1521. }
  1522. } else {
  1523. // turn OFF heather only if the minimum time is up
  1524. if (state_timer_heater_1 == 0) {
  1525. // if change state set timer
  1526. if (state_heater_1 == 1) {
  1527. state_timer_heater_1 = MIN_STATE_TIME;
  1528. }
  1529. state_heater_1 = 0;
  1530. WRITE(HEATER_1_PIN, 0);
  1531. }
  1532. }
  1533. #endif
  1534. #if EXTRUDERS > 2
  1535. // EXTRUDER 2
  1536. soft_pwm_2 = soft_pwm[2];
  1537. if (soft_pwm_2 > 0) {
  1538. // turn ON heather only if the minimum time is up
  1539. if (state_timer_heater_2 == 0) {
  1540. // if change state set timer
  1541. if (state_heater_2 == 0) {
  1542. state_timer_heater_2 = MIN_STATE_TIME;
  1543. }
  1544. state_heater_2 = 1;
  1545. WRITE(HEATER_2_PIN, 1);
  1546. }
  1547. } else {
  1548. // turn OFF heather only if the minimum time is up
  1549. if (state_timer_heater_2 == 0) {
  1550. // if change state set timer
  1551. if (state_heater_2 == 1) {
  1552. state_timer_heater_2 = MIN_STATE_TIME;
  1553. }
  1554. state_heater_2 = 0;
  1555. WRITE(HEATER_2_PIN, 0);
  1556. }
  1557. }
  1558. #endif
  1559. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1560. // BED
  1561. soft_pwm_b = soft_pwm_bed;
  1562. if (soft_pwm_b > 0) {
  1563. // turn ON heather only if the minimum time is up
  1564. if (state_timer_heater_b == 0) {
  1565. // if change state set timer
  1566. if (state_heater_b == 0) {
  1567. state_timer_heater_b = MIN_STATE_TIME;
  1568. }
  1569. state_heater_b = 1;
  1570. WRITE(HEATER_BED_PIN, 1);
  1571. }
  1572. } else {
  1573. // turn OFF heather only if the minimum time is up
  1574. if (state_timer_heater_b == 0) {
  1575. // if change state set timer
  1576. if (state_heater_b == 1) {
  1577. state_timer_heater_b = MIN_STATE_TIME;
  1578. }
  1579. state_heater_b = 0;
  1580. WRITE(HEATER_BED_PIN, 0);
  1581. }
  1582. }
  1583. #endif
  1584. } // if (slow_pwm_count == 0)
  1585. // EXTRUDER 0
  1586. if (soft_pwm_0 < slow_pwm_count) {
  1587. // turn OFF heather only if the minimum time is up
  1588. if (state_timer_heater_0 == 0) {
  1589. // if change state set timer
  1590. if (state_heater_0 == 1) {
  1591. state_timer_heater_0 = MIN_STATE_TIME;
  1592. }
  1593. state_heater_0 = 0;
  1594. WRITE(HEATER_0_PIN, 0);
  1595. #ifdef HEATERS_PARALLEL
  1596. WRITE(HEATER_1_PIN, 0);
  1597. #endif
  1598. }
  1599. }
  1600. #if EXTRUDERS > 1
  1601. // EXTRUDER 1
  1602. if (soft_pwm_1 < slow_pwm_count) {
  1603. // turn OFF heather only if the minimum time is up
  1604. if (state_timer_heater_1 == 0) {
  1605. // if change state set timer
  1606. if (state_heater_1 == 1) {
  1607. state_timer_heater_1 = MIN_STATE_TIME;
  1608. }
  1609. state_heater_1 = 0;
  1610. WRITE(HEATER_1_PIN, 0);
  1611. }
  1612. }
  1613. #endif
  1614. #if EXTRUDERS > 2
  1615. // EXTRUDER 2
  1616. if (soft_pwm_2 < slow_pwm_count) {
  1617. // turn OFF heather only if the minimum time is up
  1618. if (state_timer_heater_2 == 0) {
  1619. // if change state set timer
  1620. if (state_heater_2 == 1) {
  1621. state_timer_heater_2 = MIN_STATE_TIME;
  1622. }
  1623. state_heater_2 = 0;
  1624. WRITE(HEATER_2_PIN, 0);
  1625. }
  1626. }
  1627. #endif
  1628. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1629. // BED
  1630. if (soft_pwm_b < slow_pwm_count) {
  1631. // turn OFF heather only if the minimum time is up
  1632. if (state_timer_heater_b == 0) {
  1633. // if change state set timer
  1634. if (state_heater_b == 1) {
  1635. state_timer_heater_b = MIN_STATE_TIME;
  1636. }
  1637. state_heater_b = 0;
  1638. WRITE(HEATER_BED_PIN, 0);
  1639. }
  1640. }
  1641. #endif
  1642. #ifdef FAN_SOFT_PWM
  1643. if (pwm_count == 0){
  1644. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1645. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1646. }
  1647. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1648. #endif
  1649. pwm_count += (1 << SOFT_PWM_SCALE);
  1650. pwm_count &= 0x7f;
  1651. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1652. if ((pwm_count % 64) == 0) {
  1653. slow_pwm_count++;
  1654. slow_pwm_count &= 0x7f;
  1655. // Extruder 0
  1656. if (state_timer_heater_0 > 0) {
  1657. state_timer_heater_0--;
  1658. }
  1659. #if EXTRUDERS > 1
  1660. // Extruder 1
  1661. if (state_timer_heater_1 > 0)
  1662. state_timer_heater_1--;
  1663. #endif
  1664. #if EXTRUDERS > 2
  1665. // Extruder 2
  1666. if (state_timer_heater_2 > 0)
  1667. state_timer_heater_2--;
  1668. #endif
  1669. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1670. // Bed
  1671. if (state_timer_heater_b > 0)
  1672. state_timer_heater_b--;
  1673. #endif
  1674. } //if ((pwm_count % 64) == 0) {
  1675. #endif //ifndef SLOW_PWM_HEATERS
  1676. switch(temp_state) {
  1677. case 0: // Prepare TEMP_0
  1678. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1679. #if TEMP_0_PIN > 7
  1680. ADCSRB = 1<<MUX5;
  1681. #else
  1682. ADCSRB = 0;
  1683. #endif
  1684. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1685. ADCSRA |= 1<<ADSC; // Start conversion
  1686. #endif
  1687. lcd_buttons_update();
  1688. temp_state = 1;
  1689. break;
  1690. case 1: // Measure TEMP_0
  1691. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1692. raw_temp_0_value += ADC;
  1693. #endif
  1694. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1695. raw_temp_0_value = read_max6675();
  1696. #endif
  1697. temp_state = 2;
  1698. break;
  1699. case 2: // Prepare TEMP_BED
  1700. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1701. #if TEMP_BED_PIN > 7
  1702. ADCSRB = 1<<MUX5;
  1703. #else
  1704. ADCSRB = 0;
  1705. #endif
  1706. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1707. ADCSRA |= 1<<ADSC; // Start conversion
  1708. #endif
  1709. lcd_buttons_update();
  1710. temp_state = 3;
  1711. break;
  1712. case 3: // Measure TEMP_BED
  1713. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1714. raw_temp_bed_value += ADC;
  1715. #endif
  1716. temp_state = 4;
  1717. break;
  1718. case 4: // Prepare TEMP_1
  1719. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1720. #if TEMP_1_PIN > 7
  1721. ADCSRB = 1<<MUX5;
  1722. #else
  1723. ADCSRB = 0;
  1724. #endif
  1725. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1726. ADCSRA |= 1<<ADSC; // Start conversion
  1727. #endif
  1728. lcd_buttons_update();
  1729. temp_state = 5;
  1730. break;
  1731. case 5: // Measure TEMP_1
  1732. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1733. raw_temp_1_value += ADC;
  1734. #endif
  1735. temp_state = 6;
  1736. break;
  1737. case 6: // Prepare TEMP_2
  1738. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1739. #if TEMP_2_PIN > 7
  1740. ADCSRB = 1<<MUX5;
  1741. #else
  1742. ADCSRB = 0;
  1743. #endif
  1744. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1745. ADCSRA |= 1<<ADSC; // Start conversion
  1746. #endif
  1747. lcd_buttons_update();
  1748. temp_state = 7;
  1749. break;
  1750. case 7: // Measure TEMP_2
  1751. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1752. raw_temp_2_value += ADC;
  1753. #endif
  1754. temp_state = 8;//change so that Filament Width is also measured
  1755. break;
  1756. case 8: //Prepare FILWIDTH
  1757. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1758. #if FILWIDTH_PIN>7
  1759. ADCSRB = 1<<MUX5;
  1760. #else
  1761. ADCSRB = 0;
  1762. #endif
  1763. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1764. ADCSRA |= 1<<ADSC; // Start conversion
  1765. #endif
  1766. lcd_buttons_update();
  1767. temp_state = 9;
  1768. break;
  1769. case 9: //Measure FILWIDTH
  1770. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1771. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1772. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1773. {
  1774. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1775. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1776. }
  1777. #endif
  1778. temp_state = 10;
  1779. break;
  1780. case 10: // Prepare TEMP_AMBIENT
  1781. #if defined(TEMP_AMBIENT_PIN) && (TEMP_AMBIENT_PIN > -1)
  1782. #if TEMP_AMBIENT_PIN > 7
  1783. ADCSRB = 1<<MUX5;
  1784. #else
  1785. ADCSRB = 0;
  1786. #endif
  1787. ADMUX = ((1 << REFS0) | (TEMP_AMBIENT_PIN & 0x07));
  1788. ADCSRA |= 1<<ADSC; // Start conversion
  1789. #endif
  1790. lcd_buttons_update();
  1791. temp_state = 11;
  1792. break;
  1793. case 11: // Measure TEMP_AMBIENT
  1794. #if defined(TEMP_AMBIENT_PIN) && (TEMP_AMBIENT_PIN > -1)
  1795. raw_temp_ambient_value += ADC;
  1796. #endif
  1797. temp_state = 12;
  1798. break;
  1799. case 12: // Prepare TEMP_PINDA
  1800. #if defined(TEMP_PINDA_PIN) && (TEMP_PINDA_PIN > -1)
  1801. #if TEMP_PINDA_PIN > 7
  1802. ADCSRB = 1<<MUX5;
  1803. #else
  1804. ADCSRB = 0;
  1805. #endif
  1806. ADMUX = ((1 << REFS0) | (TEMP_PINDA_PIN & 0x07));
  1807. ADCSRA |= 1<<ADSC; // Start conversion
  1808. #endif
  1809. lcd_buttons_update();
  1810. temp_state = 13;
  1811. break;
  1812. case 13: // Measure TEMP_PINDA
  1813. #if defined(TEMP_PINDA_PIN) && (TEMP_PINDA_PIN > -1)
  1814. raw_temp_pinda_value += ADC;
  1815. #endif
  1816. temp_state = 0;
  1817. temp_count++;
  1818. break;
  1819. case 14: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1820. temp_state = 0;
  1821. break;
  1822. // default:
  1823. // SERIAL_ERROR_START;
  1824. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1825. // break;
  1826. }
  1827. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1828. {
  1829. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1830. {
  1831. current_temperature_raw[0] = raw_temp_0_value;
  1832. #if EXTRUDERS > 1
  1833. current_temperature_raw[1] = raw_temp_1_value;
  1834. #endif
  1835. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1836. redundant_temperature_raw = raw_temp_1_value;
  1837. #endif
  1838. #if EXTRUDERS > 2
  1839. current_temperature_raw[2] = raw_temp_2_value;
  1840. #endif
  1841. #ifdef PINDA_THERMISTOR
  1842. current_temperature_raw_pinda = raw_temp_pinda_value;
  1843. #endif //PINDA_THERMISTOR
  1844. #ifdef AMBIENT_THERMISTOR
  1845. current_temperature_raw_ambient = raw_temp_ambient_value;
  1846. #endif //AMBIENT_THERMISTOR
  1847. current_temperature_bed_raw = raw_temp_bed_value;
  1848. }
  1849. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1850. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1851. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1852. #endif
  1853. temp_meas_ready = true;
  1854. temp_count = 0;
  1855. raw_temp_0_value = 0;
  1856. raw_temp_1_value = 0;
  1857. raw_temp_2_value = 0;
  1858. raw_temp_bed_value = 0;
  1859. raw_temp_pinda_value = 0;
  1860. raw_temp_ambient_value = 0;
  1861. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1862. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1863. #else
  1864. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1865. #endif
  1866. max_temp_error(0);
  1867. }
  1868. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1869. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1870. #else
  1871. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1872. #endif
  1873. min_temp_error(0);
  1874. }
  1875. #if EXTRUDERS > 1
  1876. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1877. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1878. #else
  1879. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1880. #endif
  1881. max_temp_error(1);
  1882. }
  1883. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1884. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1885. #else
  1886. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1887. #endif
  1888. min_temp_error(1);
  1889. }
  1890. #endif
  1891. #if EXTRUDERS > 2
  1892. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1893. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1894. #else
  1895. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1896. #endif
  1897. max_temp_error(2);
  1898. }
  1899. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1900. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1901. #else
  1902. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1903. #endif
  1904. min_temp_error(2);
  1905. }
  1906. #endif
  1907. /* No bed MINTEMP error? */
  1908. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1909. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1910. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1911. #else
  1912. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1913. #endif
  1914. target_temperature_bed = 0;
  1915. bed_max_temp_error();
  1916. }
  1917. }
  1918. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1919. if(current_temperature_bed_raw >= bed_minttemp_raw) {
  1920. #else
  1921. if(current_temperature_bed_raw <= bed_minttemp_raw) {
  1922. #endif
  1923. bed_min_temp_error();
  1924. }
  1925. #endif
  1926. #ifdef BABYSTEPPING
  1927. for(uint8_t axis=0;axis<3;axis++)
  1928. {
  1929. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1930. if(curTodo>0)
  1931. {
  1932. babystep(axis,/*fwd*/true);
  1933. babystepsTodo[axis]--; //less to do next time
  1934. }
  1935. else
  1936. if(curTodo<0)
  1937. {
  1938. babystep(axis,/*fwd*/false);
  1939. babystepsTodo[axis]++; //less to do next time
  1940. }
  1941. }
  1942. #endif //BABYSTEPPING
  1943. check_fans();
  1944. }
  1945. void check_fans() {
  1946. if (READ(TACH_0) != fan_state[0]) {
  1947. fan_edge_counter[0] ++;
  1948. fan_state[0] = !fan_state[0];
  1949. }
  1950. //if (READ(TACH_1) != fan_state[1]) {
  1951. // fan_edge_counter[1] ++;
  1952. // fan_state[1] = !fan_state[1];
  1953. //}
  1954. }
  1955. #ifdef PIDTEMP
  1956. // Apply the scale factors to the PID values
  1957. float scalePID_i(float i)
  1958. {
  1959. return i*PID_dT;
  1960. }
  1961. float unscalePID_i(float i)
  1962. {
  1963. return i/PID_dT;
  1964. }
  1965. float scalePID_d(float d)
  1966. {
  1967. return d/PID_dT;
  1968. }
  1969. float unscalePID_d(float d)
  1970. {
  1971. return d*PID_dT;
  1972. }
  1973. #endif //PIDTEMP