My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 48KB

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