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

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