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.

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