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

temperature.cpp 50KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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, PSTR(MSG_MAXTEMP_EXTRUDER_OFF), PSTR(MSG_ERR_MAXTEMP));
  476. }
  477. void min_temp_error(uint8_t e) {
  478. disable_heater();
  479. _temp_error(e, PSTR(MSG_MINTEMP_EXTRUDER_OFF), PSTR(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, PSTR(MSG_MAXTEMP_BED_OFF), PSTR(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. #ifdef PID_BED_DEBUG
  568. SERIAL_ECHO_START;
  569. SERIAL_ECHO(" PID_BED_DEBUG ");
  570. SERIAL_ECHO(": Input ");
  571. SERIAL_ECHO(current_temperature_bed);
  572. SERIAL_ECHO(" Output ");
  573. SERIAL_ECHO(pid_output);
  574. SERIAL_ECHO(" pTerm ");
  575. SERIAL_ECHO(pTerm_bed);
  576. SERIAL_ECHO(" iTerm ");
  577. SERIAL_ECHO(iTerm_bed);
  578. SERIAL_ECHO(" dTerm ");
  579. SERIAL_ECHOLN(dTerm_bed);
  580. #endif //PID_BED_DEBUG
  581. return pid_output;
  582. }
  583. #endif
  584. void manage_heater() {
  585. if (!temp_meas_ready) return;
  586. updateTemperaturesFromRawValues();
  587. #ifdef HEATER_0_USES_MAX6675
  588. float ct = current_temperature[0];
  589. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  590. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  591. #endif //HEATER_0_USES_MAX6675
  592. unsigned long ms = millis();
  593. // Loop through all extruders
  594. for (int e = 0; e < EXTRUDERS; e++) {
  595. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  596. 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);
  597. #endif
  598. float pid_output = get_pid_output(e);
  599. // Check if temperature is within the correct range
  600. soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  601. #ifdef WATCH_TEMP_PERIOD
  602. if (watchmillis[e] && ms > watchmillis[e] + WATCH_TEMP_PERIOD) {
  603. if (degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE) {
  604. setTargetHotend(0, e);
  605. LCD_MESSAGEPGM(MSG_HEATING_FAILED_LCD); // translatable
  606. SERIAL_ECHO_START;
  607. SERIAL_ECHOLNPGM(MSG_HEATING_FAILED);
  608. }
  609. else {
  610. watchmillis[e] = 0;
  611. }
  612. }
  613. #endif //WATCH_TEMP_PERIOD
  614. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  615. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  616. disable_heater();
  617. _temp_error(-1, MSG_EXTRUDER_SWITCHED_OFF, MSG_ERR_REDUNDANT_TEMP);
  618. }
  619. #endif //TEMP_SENSOR_1_AS_REDUNDANT
  620. } // Extruders Loop
  621. #if HAS_AUTO_FAN
  622. if (ms > extruder_autofan_last_check + 2500) { // only need to check fan state very infrequently
  623. checkExtruderAutoFans();
  624. extruder_autofan_last_check = ms;
  625. }
  626. #endif
  627. #ifndef PIDTEMPBED
  628. if (ms < previous_millis_bed_heater + BED_CHECK_INTERVAL) return;
  629. previous_millis_bed_heater = ms;
  630. #endif //PIDTEMPBED
  631. #if TEMP_SENSOR_BED != 0
  632. #if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0
  633. 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);
  634. #endif
  635. #ifdef PIDTEMPBED
  636. float pid_output = get_pid_output_bed();
  637. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  638. #elif !defined(BED_LIMIT_SWITCHING)
  639. // Check if temperature is within the correct range
  640. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  641. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  642. }
  643. else {
  644. soft_pwm_bed = 0;
  645. WRITE_HEATER_BED(LOW);
  646. }
  647. #else //#ifdef BED_LIMIT_SWITCHING
  648. // Check if temperature is within the correct band
  649. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  650. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  651. soft_pwm_bed = 0;
  652. else if (current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  653. soft_pwm_bed = MAX_BED_POWER >> 1;
  654. }
  655. else {
  656. soft_pwm_bed = 0;
  657. WRITE_HEATER_BED(LOW);
  658. }
  659. #endif
  660. #endif //TEMP_SENSOR_BED != 0
  661. // Control the extruder rate based on the width sensor
  662. #ifdef FILAMENT_SENSOR
  663. if (filament_sensor) {
  664. meas_shift_index = delay_index1 - meas_delay_cm;
  665. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  666. // Get the delayed info and add 100 to reconstitute to a percent of
  667. // the nominal filament diameter then square it to get an area
  668. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  669. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  670. if (vm < 0.01) vm = 0.01;
  671. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  672. }
  673. #endif //FILAMENT_SENSOR
  674. }
  675. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  676. // Derived from RepRap FiveD extruder::getTemperature()
  677. // For hot end temperature measurement.
  678. static float analog2temp(int raw, uint8_t e) {
  679. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  680. if (e > EXTRUDERS)
  681. #else
  682. if (e >= EXTRUDERS)
  683. #endif
  684. {
  685. SERIAL_ERROR_START;
  686. SERIAL_ERROR((int)e);
  687. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  688. kill();
  689. return 0.0;
  690. }
  691. #ifdef HEATER_0_USES_MAX6675
  692. if (e == 0)
  693. {
  694. return 0.25 * raw;
  695. }
  696. #endif
  697. if(heater_ttbl_map[e] != NULL)
  698. {
  699. float celsius = 0;
  700. uint8_t i;
  701. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  702. for (i=1; i<heater_ttbllen_map[e]; i++)
  703. {
  704. if (PGM_RD_W((*tt)[i][0]) > raw)
  705. {
  706. celsius = PGM_RD_W((*tt)[i-1][1]) +
  707. (raw - PGM_RD_W((*tt)[i-1][0])) *
  708. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  709. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  710. break;
  711. }
  712. }
  713. // Overflow: Set to last value in the table
  714. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  715. return celsius;
  716. }
  717. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  718. }
  719. // Derived from RepRap FiveD extruder::getTemperature()
  720. // For bed temperature measurement.
  721. static float analog2tempBed(int raw) {
  722. #ifdef BED_USES_THERMISTOR
  723. float celsius = 0;
  724. byte i;
  725. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  726. {
  727. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  728. {
  729. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  730. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  731. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  732. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  733. break;
  734. }
  735. }
  736. // Overflow: Set to last value in the table
  737. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  738. return celsius;
  739. #elif defined BED_USES_AD595
  740. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  741. #else
  742. return 0;
  743. #endif
  744. }
  745. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  746. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  747. static void updateTemperaturesFromRawValues() {
  748. #ifdef HEATER_0_USES_MAX6675
  749. current_temperature_raw[0] = read_max6675();
  750. #endif
  751. for(uint8_t e = 0; e < EXTRUDERS; e++) {
  752. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  753. }
  754. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  755. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  756. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  757. #endif
  758. #if HAS_FILAMENT_SENSOR
  759. filament_width_meas = analog2widthFil();
  760. #endif
  761. //Reset the watchdog after we know we have a temperature measurement.
  762. watchdog_reset();
  763. CRITICAL_SECTION_START;
  764. temp_meas_ready = false;
  765. CRITICAL_SECTION_END;
  766. }
  767. #ifdef FILAMENT_SENSOR
  768. // Convert raw Filament Width to millimeters
  769. float analog2widthFil() {
  770. return current_raw_filwidth / 16383.0 * 5.0;
  771. //return current_raw_filwidth;
  772. }
  773. // Convert raw Filament Width to a ratio
  774. int widthFil_to_size_ratio() {
  775. float temp = filament_width_meas;
  776. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  777. else if (temp > MEASURED_UPPER_LIMIT) temp = MEASURED_UPPER_LIMIT;
  778. return filament_width_nominal / temp * 100;
  779. }
  780. #endif
  781. void tp_init()
  782. {
  783. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  784. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  785. MCUCR=BIT(JTD);
  786. MCUCR=BIT(JTD);
  787. #endif
  788. // Finish init of mult extruder arrays
  789. for (int e = 0; e < EXTRUDERS; e++) {
  790. // populate with the first value
  791. maxttemp[e] = maxttemp[0];
  792. #ifdef PIDTEMP
  793. temp_iState_min[e] = 0.0;
  794. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  795. #endif //PIDTEMP
  796. #ifdef PIDTEMPBED
  797. temp_iState_min_bed = 0.0;
  798. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  799. #endif //PIDTEMPBED
  800. }
  801. #if HAS_HEATER_0
  802. SET_OUTPUT(HEATER_0_PIN);
  803. #endif
  804. #if HAS_HEATER_1
  805. SET_OUTPUT(HEATER_1_PIN);
  806. #endif
  807. #if HAS_HEATER_2
  808. SET_OUTPUT(HEATER_2_PIN);
  809. #endif
  810. #if HAS_HEATER_3
  811. SET_OUTPUT(HEATER_3_PIN);
  812. #endif
  813. #if HAS_HEATER_BED
  814. SET_OUTPUT(HEATER_BED_PIN);
  815. #endif
  816. #if HAS_FAN
  817. SET_OUTPUT(FAN_PIN);
  818. #ifdef FAST_PWM_FAN
  819. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  820. #endif
  821. #ifdef FAN_SOFT_PWM
  822. soft_pwm_fan = fanSpeedSoftPwm / 2;
  823. #endif
  824. #endif
  825. #ifdef HEATER_0_USES_MAX6675
  826. #ifndef SDSUPPORT
  827. OUT_WRITE(SCK_PIN, LOW);
  828. OUT_WRITE(MOSI_PIN, HIGH);
  829. OUT_WRITE(MISO_PIN, HIGH);
  830. #else
  831. pinMode(SS_PIN, OUTPUT);
  832. digitalWrite(SS_PIN, HIGH);
  833. #endif
  834. OUT_WRITE(MAX6675_SS,HIGH);
  835. #endif //HEATER_0_USES_MAX6675
  836. #ifdef DIDR2
  837. #define ANALOG_SELECT(pin) do{ if (pin < 8) DIDR0 |= BIT(pin); else DIDR2 |= BIT(pin - 8); }while(0)
  838. #else
  839. #define ANALOG_SELECT(pin) do{ DIDR0 |= BIT(pin); }while(0)
  840. #endif
  841. // Set analog inputs
  842. ADCSRA = BIT(ADEN) | BIT(ADSC) | BIT(ADIF) | 0x07;
  843. DIDR0 = 0;
  844. #ifdef DIDR2
  845. DIDR2 = 0;
  846. #endif
  847. #if HAS_TEMP_0
  848. ANALOG_SELECT(TEMP_0_PIN);
  849. #endif
  850. #if HAS_TEMP_1
  851. ANALOG_SELECT(TEMP_1_PIN);
  852. #endif
  853. #if HAS_TEMP_2
  854. ANALOG_SELECT(TEMP_2_PIN);
  855. #endif
  856. #if HAS_TEMP_3
  857. ANALOG_SELECT(TEMP_3_PIN);
  858. #endif
  859. #if HAS_TEMP_BED
  860. ANALOG_SELECT(TEMP_BED_PIN);
  861. #endif
  862. #if HAS_FILAMENT_SENSOR
  863. ANALOG_SELECT(FILWIDTH_PIN);
  864. #endif
  865. // Use timer0 for temperature measurement
  866. // Interleave temperature interrupt with millies interrupt
  867. OCR0B = 128;
  868. TIMSK0 |= BIT(OCIE0B);
  869. // Wait for temperature measurement to settle
  870. delay(250);
  871. #define TEMP_MIN_ROUTINE(NR) \
  872. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  873. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  874. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  875. minttemp_raw[NR] += OVERSAMPLENR; \
  876. else \
  877. minttemp_raw[NR] -= OVERSAMPLENR; \
  878. }
  879. #define TEMP_MAX_ROUTINE(NR) \
  880. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  881. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  882. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  883. maxttemp_raw[NR] -= OVERSAMPLENR; \
  884. else \
  885. maxttemp_raw[NR] += OVERSAMPLENR; \
  886. }
  887. #ifdef HEATER_0_MINTEMP
  888. TEMP_MIN_ROUTINE(0);
  889. #endif
  890. #ifdef HEATER_0_MAXTEMP
  891. TEMP_MAX_ROUTINE(0);
  892. #endif
  893. #if EXTRUDERS > 1
  894. #ifdef HEATER_1_MINTEMP
  895. TEMP_MIN_ROUTINE(1);
  896. #endif
  897. #ifdef HEATER_1_MAXTEMP
  898. TEMP_MAX_ROUTINE(1);
  899. #endif
  900. #if EXTRUDERS > 2
  901. #ifdef HEATER_2_MINTEMP
  902. TEMP_MIN_ROUTINE(2);
  903. #endif
  904. #ifdef HEATER_2_MAXTEMP
  905. TEMP_MAX_ROUTINE(2);
  906. #endif
  907. #if EXTRUDERS > 3
  908. #ifdef HEATER_3_MINTEMP
  909. TEMP_MIN_ROUTINE(3);
  910. #endif
  911. #ifdef HEATER_3_MAXTEMP
  912. TEMP_MAX_ROUTINE(3);
  913. #endif
  914. #endif // EXTRUDERS > 3
  915. #endif // EXTRUDERS > 2
  916. #endif // EXTRUDERS > 1
  917. #ifdef BED_MINTEMP
  918. /* No bed MINTEMP error implemented?!? */ /*
  919. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  920. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  921. bed_minttemp_raw += OVERSAMPLENR;
  922. #else
  923. bed_minttemp_raw -= OVERSAMPLENR;
  924. #endif
  925. }
  926. */
  927. #endif //BED_MINTEMP
  928. #ifdef BED_MAXTEMP
  929. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  930. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  931. bed_maxttemp_raw -= OVERSAMPLENR;
  932. #else
  933. bed_maxttemp_raw += OVERSAMPLENR;
  934. #endif
  935. }
  936. #endif //BED_MAXTEMP
  937. }
  938. void setWatch() {
  939. #ifdef WATCH_TEMP_PERIOD
  940. unsigned long ms = millis();
  941. for (int e = 0; e < EXTRUDERS; e++) {
  942. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2)) {
  943. watch_start_temp[e] = degHotend(e);
  944. watchmillis[e] = ms;
  945. }
  946. }
  947. #endif
  948. }
  949. #if defined(THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  950. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  951. {
  952. /*
  953. SERIAL_ECHO_START;
  954. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  955. SERIAL_ECHO(heater_id);
  956. SERIAL_ECHO(" ; State:");
  957. SERIAL_ECHO(*state);
  958. SERIAL_ECHO(" ; Timer:");
  959. SERIAL_ECHO(*timer);
  960. SERIAL_ECHO(" ; Temperature:");
  961. SERIAL_ECHO(temperature);
  962. SERIAL_ECHO(" ; Target Temp:");
  963. SERIAL_ECHO(target_temperature);
  964. SERIAL_ECHOLN("");
  965. */
  966. if ((target_temperature == 0) || thermal_runaway)
  967. {
  968. *state = 0;
  969. *timer = 0;
  970. return;
  971. }
  972. switch (*state)
  973. {
  974. case 0: // "Heater Inactive" state
  975. if (target_temperature > 0) *state = 1;
  976. break;
  977. case 1: // "First Heating" state
  978. if (temperature >= target_temperature) *state = 2;
  979. break;
  980. case 2: // "Temperature Stable" state
  981. {
  982. unsigned long ms = millis();
  983. if (temperature >= (target_temperature - hysteresis_degc))
  984. {
  985. *timer = ms;
  986. }
  987. else if ( (ms - *timer) > ((unsigned long) period_seconds) * 1000)
  988. {
  989. SERIAL_ERROR_START;
  990. SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
  991. SERIAL_ERRORLN((int)heater_id);
  992. LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY); // translatable
  993. thermal_runaway = true;
  994. while(1)
  995. {
  996. disable_heater();
  997. disable_x();
  998. disable_y();
  999. disable_z();
  1000. disable_e0();
  1001. disable_e1();
  1002. disable_e2();
  1003. disable_e3();
  1004. manage_heater();
  1005. lcd_update();
  1006. }
  1007. }
  1008. } break;
  1009. }
  1010. }
  1011. #endif //THERMAL_RUNAWAY_PROTECTION_PERIOD
  1012. void disable_heater() {
  1013. for (int i=0; i<EXTRUDERS; i++) setTargetHotend(0, i);
  1014. setTargetBed(0);
  1015. #if HAS_TEMP_0
  1016. target_temperature[0] = 0;
  1017. soft_pwm[0] = 0;
  1018. WRITE_HEATER_0P(LOW); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1019. #endif
  1020. #if EXTRUDERS > 1 && HAS_TEMP_1
  1021. target_temperature[1] = 0;
  1022. soft_pwm[1] = 0;
  1023. WRITE_HEATER_1(LOW);
  1024. #endif
  1025. #if EXTRUDERS > 2 && HAS_TEMP_2
  1026. target_temperature[2] = 0;
  1027. soft_pwm[2] = 0;
  1028. WRITE_HEATER_2(LOW);
  1029. #endif
  1030. #if EXTRUDERS > 3 && HAS_TEMP_3
  1031. target_temperature[3] = 0;
  1032. soft_pwm[3] = 0;
  1033. WRITE_HEATER_3(LOW);
  1034. #endif
  1035. #if HAS_TEMP_BED
  1036. target_temperature_bed = 0;
  1037. soft_pwm_bed = 0;
  1038. #if HAS_HEATER_BED
  1039. WRITE_HEATER_BED(LOW);
  1040. #endif
  1041. #endif
  1042. }
  1043. #ifdef HEATER_0_USES_MAX6675
  1044. #define MAX6675_HEAT_INTERVAL 250
  1045. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1046. int max6675_temp = 2000;
  1047. static int read_max6675() {
  1048. unsigned long ms = millis();
  1049. if (ms < max6675_previous_millis + MAX6675_HEAT_INTERVAL)
  1050. return max6675_temp;
  1051. max6675_previous_millis = ms;
  1052. max6675_temp = 0;
  1053. #ifdef PRR
  1054. PRR &= ~BIT(PRSPI);
  1055. #elif defined(PRR0)
  1056. PRR0 &= ~BIT(PRSPI);
  1057. #endif
  1058. SPCR = BIT(MSTR) | BIT(SPE) | BIT(SPR0);
  1059. // enable TT_MAX6675
  1060. WRITE(MAX6675_SS, 0);
  1061. // ensure 100ns delay - a bit extra is fine
  1062. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1063. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1064. // read MSB
  1065. SPDR = 0;
  1066. for (;(SPSR & BIT(SPIF)) == 0;);
  1067. max6675_temp = SPDR;
  1068. max6675_temp <<= 8;
  1069. // read LSB
  1070. SPDR = 0;
  1071. for (;(SPSR & BIT(SPIF)) == 0;);
  1072. max6675_temp |= SPDR;
  1073. // disable TT_MAX6675
  1074. WRITE(MAX6675_SS, 1);
  1075. if (max6675_temp & 4) {
  1076. // thermocouple open
  1077. max6675_temp = 4000;
  1078. }
  1079. else {
  1080. max6675_temp = max6675_temp >> 3;
  1081. }
  1082. return max6675_temp;
  1083. }
  1084. #endif //HEATER_0_USES_MAX6675
  1085. /**
  1086. * Stages in the ISR loop
  1087. */
  1088. enum TempState {
  1089. PrepareTemp_0,
  1090. MeasureTemp_0,
  1091. PrepareTemp_BED,
  1092. MeasureTemp_BED,
  1093. PrepareTemp_1,
  1094. MeasureTemp_1,
  1095. PrepareTemp_2,
  1096. MeasureTemp_2,
  1097. PrepareTemp_3,
  1098. MeasureTemp_3,
  1099. Prepare_FILWIDTH,
  1100. Measure_FILWIDTH,
  1101. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1102. };
  1103. //
  1104. // Timer 0 is shared with millies
  1105. //
  1106. ISR(TIMER0_COMPB_vect) {
  1107. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1108. static unsigned char temp_count = 0;
  1109. static unsigned long raw_temp_value[EXTRUDERS] = { 0 };
  1110. static unsigned long raw_temp_bed_value = 0;
  1111. static TempState temp_state = StartupDelay;
  1112. static unsigned char pwm_count = BIT(SOFT_PWM_SCALE);
  1113. // Static members for each heater
  1114. #ifdef SLOW_PWM_HEATERS
  1115. static unsigned char slow_pwm_count = 0;
  1116. #define ISR_STATICS(n) \
  1117. static unsigned char soft_pwm_ ## n; \
  1118. static unsigned char state_heater_ ## n = 0; \
  1119. static unsigned char state_timer_heater_ ## n = 0
  1120. #else
  1121. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1122. #endif
  1123. // Statics per heater
  1124. ISR_STATICS(0);
  1125. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1126. ISR_STATICS(1);
  1127. #if EXTRUDERS > 2
  1128. ISR_STATICS(2);
  1129. #if EXTRUDERS > 3
  1130. ISR_STATICS(3);
  1131. #endif
  1132. #endif
  1133. #endif
  1134. #if HAS_HEATER_BED
  1135. ISR_STATICS(BED);
  1136. #endif
  1137. #if HAS_FILAMENT_SENSOR
  1138. static unsigned long raw_filwidth_value = 0;
  1139. #endif
  1140. #ifndef SLOW_PWM_HEATERS
  1141. /**
  1142. * standard PWM modulation
  1143. */
  1144. if (pwm_count == 0) {
  1145. soft_pwm_0 = soft_pwm[0];
  1146. if (soft_pwm_0 > 0) {
  1147. WRITE_HEATER_0(1);
  1148. }
  1149. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1150. #if EXTRUDERS > 1
  1151. soft_pwm_1 = soft_pwm[1];
  1152. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1153. #if EXTRUDERS > 2
  1154. soft_pwm_2 = soft_pwm[2];
  1155. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1156. #if EXTRUDERS > 3
  1157. soft_pwm_3 = soft_pwm[3];
  1158. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1159. #endif
  1160. #endif
  1161. #endif
  1162. #if HAS_HEATER_BED
  1163. soft_pwm_BED = soft_pwm_bed;
  1164. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1165. #endif
  1166. #ifdef FAN_SOFT_PWM
  1167. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1168. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1169. #endif
  1170. }
  1171. if (soft_pwm_0 < pwm_count) { WRITE_HEATER_0(0); }
  1172. #if EXTRUDERS > 1
  1173. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1174. #if EXTRUDERS > 2
  1175. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1176. #if EXTRUDERS > 3
  1177. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1178. #endif
  1179. #endif
  1180. #endif
  1181. #if HAS_HEATER_BED
  1182. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1183. #endif
  1184. #ifdef FAN_SOFT_PWM
  1185. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1186. #endif
  1187. pwm_count += BIT(SOFT_PWM_SCALE);
  1188. pwm_count &= 0x7f;
  1189. #else // SLOW_PWM_HEATERS
  1190. /*
  1191. * SLOW PWM HEATERS
  1192. *
  1193. * for heaters drived by relay
  1194. */
  1195. #ifndef MIN_STATE_TIME
  1196. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1197. #endif
  1198. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1199. #define _SLOW_PWM_ROUTINE(NR, src) \
  1200. soft_pwm_ ## NR = src; \
  1201. if (soft_pwm_ ## NR > 0) { \
  1202. if (state_timer_heater_ ## NR == 0) { \
  1203. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1204. state_heater_ ## NR = 1; \
  1205. WRITE_HEATER_ ## NR(1); \
  1206. } \
  1207. } \
  1208. else { \
  1209. if (state_timer_heater_ ## NR == 0) { \
  1210. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1211. state_heater_ ## NR = 0; \
  1212. WRITE_HEATER_ ## NR(0); \
  1213. } \
  1214. }
  1215. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1216. #define PWM_OFF_ROUTINE(NR) \
  1217. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1218. if (state_timer_heater_ ## NR == 0) { \
  1219. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1220. state_heater_ ## NR = 0; \
  1221. WRITE_HEATER_ ## NR (0); \
  1222. } \
  1223. }
  1224. if (slow_pwm_count == 0) {
  1225. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1226. #if EXTRUDERS > 1
  1227. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1228. #if EXTRUDERS > 2
  1229. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1230. #if EXTRUDERS > 3
  1231. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1232. #endif
  1233. #endif
  1234. #endif
  1235. #if HAS_HEATER_BED
  1236. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1237. #endif
  1238. } // slow_pwm_count == 0
  1239. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1240. #if EXTRUDERS > 1
  1241. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1242. #if EXTRUDERS > 2
  1243. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1244. #if EXTRUDERS > 3
  1245. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1246. #endif
  1247. #endif
  1248. #endif
  1249. #if HAS_HEATER_BED
  1250. PWM_OFF_ROUTINE(BED); // BED
  1251. #endif
  1252. #ifdef FAN_SOFT_PWM
  1253. if (pwm_count == 0) {
  1254. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1255. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1256. }
  1257. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1258. #endif //FAN_SOFT_PWM
  1259. pwm_count += BIT(SOFT_PWM_SCALE);
  1260. pwm_count &= 0x7f;
  1261. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1262. if ((pwm_count % 64) == 0) {
  1263. slow_pwm_count++;
  1264. slow_pwm_count &= 0x7f;
  1265. // EXTRUDER 0
  1266. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1267. #if EXTRUDERS > 1 // EXTRUDER 1
  1268. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1269. #if EXTRUDERS > 2 // EXTRUDER 2
  1270. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1271. #if EXTRUDERS > 3 // EXTRUDER 3
  1272. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1273. #endif
  1274. #endif
  1275. #endif
  1276. #if HAS_HEATER_BED
  1277. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1278. #endif
  1279. } // (pwm_count % 64) == 0
  1280. #endif // SLOW_PWM_HEATERS
  1281. #define SET_ADMUX_ADCSRA(pin) ADMUX = BIT(REFS0) | (pin & 0x07); ADCSRA |= BIT(ADSC)
  1282. #ifdef MUX5
  1283. #define START_ADC(pin) if (pin > 7) ADCSRB = BIT(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1284. #else
  1285. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1286. #endif
  1287. switch(temp_state) {
  1288. case PrepareTemp_0:
  1289. #if HAS_TEMP_0
  1290. START_ADC(TEMP_0_PIN);
  1291. #endif
  1292. lcd_buttons_update();
  1293. temp_state = MeasureTemp_0;
  1294. break;
  1295. case MeasureTemp_0:
  1296. #if HAS_TEMP_0
  1297. raw_temp_value[0] += ADC;
  1298. #endif
  1299. temp_state = PrepareTemp_BED;
  1300. break;
  1301. case PrepareTemp_BED:
  1302. #if HAS_TEMP_BED
  1303. START_ADC(TEMP_BED_PIN);
  1304. #endif
  1305. lcd_buttons_update();
  1306. temp_state = MeasureTemp_BED;
  1307. break;
  1308. case MeasureTemp_BED:
  1309. #if HAS_TEMP_BED
  1310. raw_temp_bed_value += ADC;
  1311. #endif
  1312. temp_state = PrepareTemp_1;
  1313. break;
  1314. case PrepareTemp_1:
  1315. #if HAS_TEMP_1
  1316. START_ADC(TEMP_1_PIN);
  1317. #endif
  1318. lcd_buttons_update();
  1319. temp_state = MeasureTemp_1;
  1320. break;
  1321. case MeasureTemp_1:
  1322. #if HAS_TEMP_1
  1323. raw_temp_value[1] += ADC;
  1324. #endif
  1325. temp_state = PrepareTemp_2;
  1326. break;
  1327. case PrepareTemp_2:
  1328. #if HAS_TEMP_2
  1329. START_ADC(TEMP_2_PIN);
  1330. #endif
  1331. lcd_buttons_update();
  1332. temp_state = MeasureTemp_2;
  1333. break;
  1334. case MeasureTemp_2:
  1335. #if HAS_TEMP_2
  1336. raw_temp_value[2] += ADC;
  1337. #endif
  1338. temp_state = PrepareTemp_3;
  1339. break;
  1340. case PrepareTemp_3:
  1341. #if HAS_TEMP_3
  1342. START_ADC(TEMP_3_PIN);
  1343. #endif
  1344. lcd_buttons_update();
  1345. temp_state = MeasureTemp_3;
  1346. break;
  1347. case MeasureTemp_3:
  1348. #if HAS_TEMP_3
  1349. raw_temp_value[3] += ADC;
  1350. #endif
  1351. temp_state = Prepare_FILWIDTH;
  1352. break;
  1353. case Prepare_FILWIDTH:
  1354. #if HAS_FILAMENT_SENSOR
  1355. START_ADC(FILWIDTH_PIN);
  1356. #endif
  1357. lcd_buttons_update();
  1358. temp_state = Measure_FILWIDTH;
  1359. break;
  1360. case Measure_FILWIDTH:
  1361. #if HAS_FILAMENT_SENSOR
  1362. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1363. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1364. raw_filwidth_value -= (raw_filwidth_value>>7); //multiply raw_filwidth_value by 127/128
  1365. raw_filwidth_value += ((unsigned long)ADC<<7); //add new ADC reading
  1366. }
  1367. #endif
  1368. temp_state = PrepareTemp_0;
  1369. temp_count++;
  1370. break;
  1371. case StartupDelay:
  1372. temp_state = PrepareTemp_0;
  1373. break;
  1374. // default:
  1375. // SERIAL_ERROR_START;
  1376. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1377. // break;
  1378. } // switch(temp_state)
  1379. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1380. if (!temp_meas_ready) { //Only update the raw values if they have been read. Else we could be updating them during reading.
  1381. #ifndef HEATER_0_USES_MAX6675
  1382. current_temperature_raw[0] = raw_temp_value[0];
  1383. #endif
  1384. #if EXTRUDERS > 1
  1385. current_temperature_raw[1] = raw_temp_value[1];
  1386. #if EXTRUDERS > 2
  1387. current_temperature_raw[2] = raw_temp_value[2];
  1388. #if EXTRUDERS > 3
  1389. current_temperature_raw[3] = raw_temp_value[3];
  1390. #endif
  1391. #endif
  1392. #endif
  1393. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1394. redundant_temperature_raw = raw_temp_value[1];
  1395. #endif
  1396. current_temperature_bed_raw = raw_temp_bed_value;
  1397. } //!temp_meas_ready
  1398. // Filament Sensor - can be read any time since IIR filtering is used
  1399. #if HAS_FILAMENT_SENSOR
  1400. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1401. #endif
  1402. temp_meas_ready = true;
  1403. temp_count = 0;
  1404. for (int i = 0; i < EXTRUDERS; i++) raw_temp_value[i] = 0;
  1405. raw_temp_bed_value = 0;
  1406. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1407. #define GE0 <=
  1408. #define LE0 >=
  1409. #else
  1410. #define GE0 >=
  1411. #define LE0 <=
  1412. #endif
  1413. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1414. if (current_temperature_raw[0] LE0 minttemp_raw[0]) min_temp_error(0);
  1415. #if EXTRUDERS > 1
  1416. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1417. #define GE1 <=
  1418. #define LE1 >=
  1419. #else
  1420. #define GE1 >=
  1421. #define LE1 <=
  1422. #endif
  1423. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1424. if (current_temperature_raw[1] LE1 minttemp_raw[1]) min_temp_error(1);
  1425. #if EXTRUDERS > 2
  1426. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1427. #define GE2 <=
  1428. #define LE2 >=
  1429. #else
  1430. #define GE2 >=
  1431. #define LE2 <=
  1432. #endif
  1433. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1434. if (current_temperature_raw[2] LE2 minttemp_raw[2]) min_temp_error(2);
  1435. #if EXTRUDERS > 3
  1436. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1437. #define GE3 <=
  1438. #define LE3 >=
  1439. #else
  1440. #define GE3 >=
  1441. #define LE3 <=
  1442. #endif
  1443. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1444. if (current_temperature_raw[3] LE3 minttemp_raw[3]) min_temp_error(3);
  1445. #endif // EXTRUDERS > 3
  1446. #endif // EXTRUDERS > 2
  1447. #endif // EXTRUDERS > 1
  1448. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1449. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1450. #define GEBED <=
  1451. #define LEBED >=
  1452. #else
  1453. #define GEBED >=
  1454. #define LEBED <=
  1455. #endif
  1456. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) {
  1457. target_temperature_bed = 0;
  1458. bed_max_temp_error();
  1459. }
  1460. #endif
  1461. } // temp_count >= OVERSAMPLENR
  1462. #ifdef BABYSTEPPING
  1463. for (uint8_t axis=X_AXIS; axis<=Z_AXIS; axis++) {
  1464. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1465. if (curTodo > 0) {
  1466. babystep(axis,/*fwd*/true);
  1467. babystepsTodo[axis]--; //less to do next time
  1468. }
  1469. else if(curTodo < 0) {
  1470. babystep(axis,/*fwd*/false);
  1471. babystepsTodo[axis]++; //less to do next time
  1472. }
  1473. }
  1474. #endif //BABYSTEPPING
  1475. }
  1476. #ifdef PIDTEMP
  1477. // Apply the scale factors to the PID values
  1478. float scalePID_i(float i) { return i * PID_dT; }
  1479. float unscalePID_i(float i) { return i / PID_dT; }
  1480. float scalePID_d(float d) { return d / PID_dT; }
  1481. float unscalePID_d(float d) { return d * PID_dT; }
  1482. #endif //PIDTEMP