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

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