My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

temperature.cpp 50KB

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