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

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