My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

temperature.cpp 50KB

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