temperature.cpp 63 KB

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