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

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