My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 48KB

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