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

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