temperature.cpp 59 KB

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