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 50KB

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