My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

temperature.cpp 51KB

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