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

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