temperature.cpp 63 KB

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