temperature.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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_BED_PERIOD && THERMAL_RUNAWAY_PROTECTION_BED_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) || (defined (THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_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_ERRORPGM("Thermal Runaway, system stopped! Heater_ID: ");
  935. if (heater_id == 9)
  936. SERIAL_ERRORLNPGM("bed");
  937. else
  938. SERIAL_ERRORLN((int)heater_id);
  939. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  940. thermal_runaway = true;
  941. while(1)
  942. {
  943. disable_heater();
  944. disable_x();
  945. disable_y();
  946. disable_z();
  947. disable_e0();
  948. disable_e1();
  949. disable_e2();
  950. manage_heater();
  951. lcd_update();
  952. }
  953. }
  954. break;
  955. }
  956. }
  957. #endif
  958. void disable_heater()
  959. {
  960. for(int i=0;i<EXTRUDERS;i++)
  961. setTargetHotend(0,i);
  962. setTargetBed(0);
  963. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  964. target_temperature[0]=0;
  965. soft_pwm[0]=0;
  966. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  967. WRITE(HEATER_0_PIN,LOW);
  968. #endif
  969. #endif
  970. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  971. target_temperature[1]=0;
  972. soft_pwm[1]=0;
  973. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  974. WRITE(HEATER_1_PIN,LOW);
  975. #endif
  976. #endif
  977. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  978. target_temperature[2]=0;
  979. soft_pwm[2]=0;
  980. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  981. WRITE(HEATER_2_PIN,LOW);
  982. #endif
  983. #endif
  984. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  985. target_temperature_bed=0;
  986. soft_pwm_bed=0;
  987. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  988. WRITE(HEATER_BED_PIN,LOW);
  989. #endif
  990. #endif
  991. }
  992. void max_temp_error(uint8_t e) {
  993. disable_heater();
  994. if(IsStopped() == false) {
  995. SERIAL_ERROR_START;
  996. SERIAL_ERRORLN((int)e);
  997. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  998. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  999. }
  1000. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1001. Stop();
  1002. #endif
  1003. }
  1004. void min_temp_error(uint8_t e) {
  1005. disable_heater();
  1006. if(IsStopped() == false) {
  1007. SERIAL_ERROR_START;
  1008. SERIAL_ERRORLN((int)e);
  1009. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1010. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1011. }
  1012. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1013. Stop();
  1014. #endif
  1015. }
  1016. void bed_max_temp_error(void) {
  1017. #if HEATER_BED_PIN > -1
  1018. WRITE(HEATER_BED_PIN, 0);
  1019. #endif
  1020. if(IsStopped() == false) {
  1021. SERIAL_ERROR_START;
  1022. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  1023. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1024. }
  1025. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1026. Stop();
  1027. #endif
  1028. }
  1029. #ifdef HEATER_0_USES_MAX6675
  1030. #define MAX6675_HEAT_INTERVAL 250
  1031. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1032. int max6675_temp = 2000;
  1033. int read_max6675()
  1034. {
  1035. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1036. return max6675_temp;
  1037. max6675_previous_millis = millis();
  1038. max6675_temp = 0;
  1039. #ifdef PRR
  1040. PRR &= ~(1<<PRSPI);
  1041. #elif defined PRR0
  1042. PRR0 &= ~(1<<PRSPI);
  1043. #endif
  1044. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1045. // enable TT_MAX6675
  1046. WRITE(MAX6675_SS, 0);
  1047. // ensure 100ns delay - a bit extra is fine
  1048. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1049. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1050. // read MSB
  1051. SPDR = 0;
  1052. for (;(SPSR & (1<<SPIF)) == 0;);
  1053. max6675_temp = SPDR;
  1054. max6675_temp <<= 8;
  1055. // read LSB
  1056. SPDR = 0;
  1057. for (;(SPSR & (1<<SPIF)) == 0;);
  1058. max6675_temp |= SPDR;
  1059. // disable TT_MAX6675
  1060. WRITE(MAX6675_SS, 1);
  1061. if (max6675_temp & 4)
  1062. {
  1063. // thermocouple open
  1064. max6675_temp = 2000;
  1065. }
  1066. else
  1067. {
  1068. max6675_temp = max6675_temp >> 3;
  1069. }
  1070. return max6675_temp;
  1071. }
  1072. #endif
  1073. // Timer 0 is shared with millies
  1074. ISR(TIMER0_COMPB_vect)
  1075. {
  1076. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1077. static unsigned char temp_count = 0;
  1078. static unsigned long raw_temp_0_value = 0;
  1079. static unsigned long raw_temp_1_value = 0;
  1080. static unsigned long raw_temp_2_value = 0;
  1081. static unsigned long raw_temp_bed_value = 0;
  1082. static unsigned char temp_state = 10;
  1083. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1084. static unsigned char soft_pwm_0;
  1085. #ifdef SLOW_PWM_HEATERS
  1086. static unsigned char slow_pwm_count = 0;
  1087. static unsigned char state_heater_0 = 0;
  1088. static unsigned char state_timer_heater_0 = 0;
  1089. #endif
  1090. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1091. static unsigned char soft_pwm_1;
  1092. #ifdef SLOW_PWM_HEATERS
  1093. static unsigned char state_heater_1 = 0;
  1094. static unsigned char state_timer_heater_1 = 0;
  1095. #endif
  1096. #endif
  1097. #if EXTRUDERS > 2
  1098. static unsigned char soft_pwm_2;
  1099. #ifdef SLOW_PWM_HEATERS
  1100. static unsigned char state_heater_2 = 0;
  1101. static unsigned char state_timer_heater_2 = 0;
  1102. #endif
  1103. #endif
  1104. #if HEATER_BED_PIN > -1
  1105. static unsigned char soft_pwm_b;
  1106. #ifdef SLOW_PWM_HEATERS
  1107. static unsigned char state_heater_b = 0;
  1108. static unsigned char state_timer_heater_b = 0;
  1109. #endif
  1110. #endif
  1111. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1112. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1113. #endif
  1114. #ifndef SLOW_PWM_HEATERS
  1115. /*
  1116. * standard PWM modulation
  1117. */
  1118. if(pwm_count == 0){
  1119. soft_pwm_0 = soft_pwm[0];
  1120. if(soft_pwm_0 > 0) {
  1121. WRITE(HEATER_0_PIN,1);
  1122. #ifdef HEATERS_PARALLEL
  1123. WRITE(HEATER_1_PIN,1);
  1124. #endif
  1125. } else WRITE(HEATER_0_PIN,0);
  1126. #if EXTRUDERS > 1
  1127. soft_pwm_1 = soft_pwm[1];
  1128. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1129. #endif
  1130. #if EXTRUDERS > 2
  1131. soft_pwm_2 = soft_pwm[2];
  1132. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1133. #endif
  1134. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1135. soft_pwm_b = soft_pwm_bed;
  1136. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1137. #endif
  1138. #ifdef FAN_SOFT_PWM
  1139. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1140. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1141. #endif
  1142. }
  1143. if(soft_pwm_0 < pwm_count) {
  1144. WRITE(HEATER_0_PIN,0);
  1145. #ifdef HEATERS_PARALLEL
  1146. WRITE(HEATER_1_PIN,0);
  1147. #endif
  1148. }
  1149. #if EXTRUDERS > 1
  1150. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1151. #endif
  1152. #if EXTRUDERS > 2
  1153. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1154. #endif
  1155. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1156. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1157. #endif
  1158. #ifdef FAN_SOFT_PWM
  1159. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1160. #endif
  1161. pwm_count += (1 << SOFT_PWM_SCALE);
  1162. pwm_count &= 0x7f;
  1163. #else //ifndef SLOW_PWM_HEATERS
  1164. /*
  1165. * SLOW PWM HEATERS
  1166. *
  1167. * for heaters drived by relay
  1168. */
  1169. #ifndef MIN_STATE_TIME
  1170. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1171. #endif
  1172. if (slow_pwm_count == 0) {
  1173. // EXTRUDER 0
  1174. soft_pwm_0 = soft_pwm[0];
  1175. if (soft_pwm_0 > 0) {
  1176. // turn ON heather only if the minimum time is up
  1177. if (state_timer_heater_0 == 0) {
  1178. // if change state set timer
  1179. if (state_heater_0 == 0) {
  1180. state_timer_heater_0 = MIN_STATE_TIME;
  1181. }
  1182. state_heater_0 = 1;
  1183. WRITE(HEATER_0_PIN, 1);
  1184. #ifdef HEATERS_PARALLEL
  1185. WRITE(HEATER_1_PIN, 1);
  1186. #endif
  1187. }
  1188. } else {
  1189. // turn OFF heather only if the minimum time is up
  1190. if (state_timer_heater_0 == 0) {
  1191. // if change state set timer
  1192. if (state_heater_0 == 1) {
  1193. state_timer_heater_0 = MIN_STATE_TIME;
  1194. }
  1195. state_heater_0 = 0;
  1196. WRITE(HEATER_0_PIN, 0);
  1197. #ifdef HEATERS_PARALLEL
  1198. WRITE(HEATER_1_PIN, 0);
  1199. #endif
  1200. }
  1201. }
  1202. #if EXTRUDERS > 1
  1203. // EXTRUDER 1
  1204. soft_pwm_1 = soft_pwm[1];
  1205. if (soft_pwm_1 > 0) {
  1206. // turn ON heather only if the minimum time is up
  1207. if (state_timer_heater_1 == 0) {
  1208. // if change state set timer
  1209. if (state_heater_1 == 0) {
  1210. state_timer_heater_1 = MIN_STATE_TIME;
  1211. }
  1212. state_heater_1 = 1;
  1213. WRITE(HEATER_1_PIN, 1);
  1214. }
  1215. } else {
  1216. // turn OFF heather only if the minimum time is up
  1217. if (state_timer_heater_1 == 0) {
  1218. // if change state set timer
  1219. if (state_heater_1 == 1) {
  1220. state_timer_heater_1 = MIN_STATE_TIME;
  1221. }
  1222. state_heater_1 = 0;
  1223. WRITE(HEATER_1_PIN, 0);
  1224. }
  1225. }
  1226. #endif
  1227. #if EXTRUDERS > 2
  1228. // EXTRUDER 2
  1229. soft_pwm_2 = soft_pwm[2];
  1230. if (soft_pwm_2 > 0) {
  1231. // turn ON heather only if the minimum time is up
  1232. if (state_timer_heater_2 == 0) {
  1233. // if change state set timer
  1234. if (state_heater_2 == 0) {
  1235. state_timer_heater_2 = MIN_STATE_TIME;
  1236. }
  1237. state_heater_2 = 1;
  1238. WRITE(HEATER_2_PIN, 1);
  1239. }
  1240. } else {
  1241. // turn OFF heather only if the minimum time is up
  1242. if (state_timer_heater_2 == 0) {
  1243. // if change state set timer
  1244. if (state_heater_2 == 1) {
  1245. state_timer_heater_2 = MIN_STATE_TIME;
  1246. }
  1247. state_heater_2 = 0;
  1248. WRITE(HEATER_2_PIN, 0);
  1249. }
  1250. }
  1251. #endif
  1252. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1253. // BED
  1254. soft_pwm_b = soft_pwm_bed;
  1255. if (soft_pwm_b > 0) {
  1256. // turn ON heather only if the minimum time is up
  1257. if (state_timer_heater_b == 0) {
  1258. // if change state set timer
  1259. if (state_heater_b == 0) {
  1260. state_timer_heater_b = MIN_STATE_TIME;
  1261. }
  1262. state_heater_b = 1;
  1263. WRITE(HEATER_BED_PIN, 1);
  1264. }
  1265. } else {
  1266. // turn OFF heather only if the minimum time is up
  1267. if (state_timer_heater_b == 0) {
  1268. // if change state set timer
  1269. if (state_heater_b == 1) {
  1270. state_timer_heater_b = MIN_STATE_TIME;
  1271. }
  1272. state_heater_b = 0;
  1273. WRITE(HEATER_BED_PIN, 0);
  1274. }
  1275. }
  1276. #endif
  1277. } // if (slow_pwm_count == 0)
  1278. // EXTRUDER 0
  1279. if (soft_pwm_0 < slow_pwm_count) {
  1280. // turn OFF heather only if the minimum time is up
  1281. if (state_timer_heater_0 == 0) {
  1282. // if change state set timer
  1283. if (state_heater_0 == 1) {
  1284. state_timer_heater_0 = MIN_STATE_TIME;
  1285. }
  1286. state_heater_0 = 0;
  1287. WRITE(HEATER_0_PIN, 0);
  1288. #ifdef HEATERS_PARALLEL
  1289. WRITE(HEATER_1_PIN, 0);
  1290. #endif
  1291. }
  1292. }
  1293. #if EXTRUDERS > 1
  1294. // EXTRUDER 1
  1295. if (soft_pwm_1 < slow_pwm_count) {
  1296. // turn OFF heather only if the minimum time is up
  1297. if (state_timer_heater_1 == 0) {
  1298. // if change state set timer
  1299. if (state_heater_1 == 1) {
  1300. state_timer_heater_1 = MIN_STATE_TIME;
  1301. }
  1302. state_heater_1 = 0;
  1303. WRITE(HEATER_1_PIN, 0);
  1304. }
  1305. }
  1306. #endif
  1307. #if EXTRUDERS > 2
  1308. // EXTRUDER 2
  1309. if (soft_pwm_2 < slow_pwm_count) {
  1310. // turn OFF heather only if the minimum time is up
  1311. if (state_timer_heater_2 == 0) {
  1312. // if change state set timer
  1313. if (state_heater_2 == 1) {
  1314. state_timer_heater_2 = MIN_STATE_TIME;
  1315. }
  1316. state_heater_2 = 0;
  1317. WRITE(HEATER_2_PIN, 0);
  1318. }
  1319. }
  1320. #endif
  1321. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1322. // BED
  1323. if (soft_pwm_b < slow_pwm_count) {
  1324. // turn OFF heather only if the minimum time is up
  1325. if (state_timer_heater_b == 0) {
  1326. // if change state set timer
  1327. if (state_heater_b == 1) {
  1328. state_timer_heater_b = MIN_STATE_TIME;
  1329. }
  1330. state_heater_b = 0;
  1331. WRITE(HEATER_BED_PIN, 0);
  1332. }
  1333. }
  1334. #endif
  1335. #ifdef FAN_SOFT_PWM
  1336. if (pwm_count == 0){
  1337. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1338. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1339. }
  1340. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1341. #endif
  1342. pwm_count += (1 << SOFT_PWM_SCALE);
  1343. pwm_count &= 0x7f;
  1344. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1345. if ((pwm_count % 64) == 0) {
  1346. slow_pwm_count++;
  1347. slow_pwm_count &= 0x7f;
  1348. // Extruder 0
  1349. if (state_timer_heater_0 > 0) {
  1350. state_timer_heater_0--;
  1351. }
  1352. #if EXTRUDERS > 1
  1353. // Extruder 1
  1354. if (state_timer_heater_1 > 0)
  1355. state_timer_heater_1--;
  1356. #endif
  1357. #if EXTRUDERS > 2
  1358. // Extruder 2
  1359. if (state_timer_heater_2 > 0)
  1360. state_timer_heater_2--;
  1361. #endif
  1362. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1363. // Bed
  1364. if (state_timer_heater_b > 0)
  1365. state_timer_heater_b--;
  1366. #endif
  1367. } //if ((pwm_count % 64) == 0) {
  1368. #endif //ifndef SLOW_PWM_HEATERS
  1369. switch(temp_state) {
  1370. case 0: // Prepare TEMP_0
  1371. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1372. #if TEMP_0_PIN > 7
  1373. ADCSRB = 1<<MUX5;
  1374. #else
  1375. ADCSRB = 0;
  1376. #endif
  1377. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1378. ADCSRA |= 1<<ADSC; // Start conversion
  1379. #endif
  1380. lcd_buttons_update();
  1381. temp_state = 1;
  1382. break;
  1383. case 1: // Measure TEMP_0
  1384. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1385. raw_temp_0_value += ADC;
  1386. #endif
  1387. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1388. raw_temp_0_value = read_max6675();
  1389. #endif
  1390. temp_state = 2;
  1391. break;
  1392. case 2: // Prepare TEMP_BED
  1393. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1394. #if TEMP_BED_PIN > 7
  1395. ADCSRB = 1<<MUX5;
  1396. #else
  1397. ADCSRB = 0;
  1398. #endif
  1399. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1400. ADCSRA |= 1<<ADSC; // Start conversion
  1401. #endif
  1402. lcd_buttons_update();
  1403. temp_state = 3;
  1404. break;
  1405. case 3: // Measure TEMP_BED
  1406. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1407. raw_temp_bed_value += ADC;
  1408. #endif
  1409. temp_state = 4;
  1410. break;
  1411. case 4: // Prepare TEMP_1
  1412. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1413. #if TEMP_1_PIN > 7
  1414. ADCSRB = 1<<MUX5;
  1415. #else
  1416. ADCSRB = 0;
  1417. #endif
  1418. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1419. ADCSRA |= 1<<ADSC; // Start conversion
  1420. #endif
  1421. lcd_buttons_update();
  1422. temp_state = 5;
  1423. break;
  1424. case 5: // Measure TEMP_1
  1425. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1426. raw_temp_1_value += ADC;
  1427. #endif
  1428. temp_state = 6;
  1429. break;
  1430. case 6: // Prepare TEMP_2
  1431. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1432. #if TEMP_2_PIN > 7
  1433. ADCSRB = 1<<MUX5;
  1434. #else
  1435. ADCSRB = 0;
  1436. #endif
  1437. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1438. ADCSRA |= 1<<ADSC; // Start conversion
  1439. #endif
  1440. lcd_buttons_update();
  1441. temp_state = 7;
  1442. break;
  1443. case 7: // Measure TEMP_2
  1444. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1445. raw_temp_2_value += ADC;
  1446. #endif
  1447. temp_state = 8;//change so that Filament Width is also measured
  1448. break;
  1449. case 8: //Prepare FILWIDTH
  1450. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1451. #if FILWIDTH_PIN>7
  1452. ADCSRB = 1<<MUX5;
  1453. #else
  1454. ADCSRB = 0;
  1455. #endif
  1456. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1457. ADCSRA |= 1<<ADSC; // Start conversion
  1458. #endif
  1459. lcd_buttons_update();
  1460. temp_state = 9;
  1461. break;
  1462. case 9: //Measure FILWIDTH
  1463. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1464. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1465. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1466. {
  1467. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1468. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1469. }
  1470. #endif
  1471. temp_state = 0;
  1472. temp_count++;
  1473. break;
  1474. case 10: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1475. temp_state = 0;
  1476. break;
  1477. // default:
  1478. // SERIAL_ERROR_START;
  1479. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1480. // break;
  1481. }
  1482. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1483. {
  1484. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1485. {
  1486. current_temperature_raw[0] = raw_temp_0_value;
  1487. #if EXTRUDERS > 1
  1488. current_temperature_raw[1] = raw_temp_1_value;
  1489. #endif
  1490. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1491. redundant_temperature_raw = raw_temp_1_value;
  1492. #endif
  1493. #if EXTRUDERS > 2
  1494. current_temperature_raw[2] = raw_temp_2_value;
  1495. #endif
  1496. current_temperature_bed_raw = raw_temp_bed_value;
  1497. }
  1498. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1499. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1500. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1501. #endif
  1502. temp_meas_ready = true;
  1503. temp_count = 0;
  1504. raw_temp_0_value = 0;
  1505. raw_temp_1_value = 0;
  1506. raw_temp_2_value = 0;
  1507. raw_temp_bed_value = 0;
  1508. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1509. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1510. #else
  1511. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1512. #endif
  1513. max_temp_error(0);
  1514. }
  1515. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1516. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1517. #else
  1518. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1519. #endif
  1520. min_temp_error(0);
  1521. }
  1522. #if EXTRUDERS > 1
  1523. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1524. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1525. #else
  1526. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1527. #endif
  1528. max_temp_error(1);
  1529. }
  1530. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1531. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1532. #else
  1533. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1534. #endif
  1535. min_temp_error(1);
  1536. }
  1537. #endif
  1538. #if EXTRUDERS > 2
  1539. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1540. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1541. #else
  1542. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1543. #endif
  1544. max_temp_error(2);
  1545. }
  1546. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1547. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1548. #else
  1549. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1550. #endif
  1551. min_temp_error(2);
  1552. }
  1553. #endif
  1554. /* No bed MINTEMP error? */
  1555. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1556. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1557. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1558. #else
  1559. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1560. #endif
  1561. target_temperature_bed = 0;
  1562. bed_max_temp_error();
  1563. }
  1564. #endif
  1565. }
  1566. #ifdef BABYSTEPPING
  1567. for(uint8_t axis=0;axis<3;axis++)
  1568. {
  1569. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1570. if(curTodo>0)
  1571. {
  1572. babystep(axis,/*fwd*/true);
  1573. babystepsTodo[axis]--; //less to do next time
  1574. }
  1575. else
  1576. if(curTodo<0)
  1577. {
  1578. babystep(axis,/*fwd*/false);
  1579. babystepsTodo[axis]++; //less to do next time
  1580. }
  1581. }
  1582. #endif //BABYSTEPPING
  1583. }
  1584. #ifdef PIDTEMP
  1585. // Apply the scale factors to the PID values
  1586. float scalePID_i(float i)
  1587. {
  1588. return i*PID_dT;
  1589. }
  1590. float unscalePID_i(float i)
  1591. {
  1592. return i/PID_dT;
  1593. }
  1594. float scalePID_d(float d)
  1595. {
  1596. return d/PID_dT;
  1597. }
  1598. float unscalePID_d(float d)
  1599. {
  1600. return d*PID_dT;
  1601. }
  1602. #endif //PIDTEMP