temperature.cpp 56 KB

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