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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  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 "temperature.h"
  27. #include "thermistortables.h"
  28. #include "ultralcd.h"
  29. #include "planner.h"
  30. #include "language.h"
  31. #if ENABLED(HEATER_0_USES_MAX6675)
  32. #include "spi.h"
  33. #endif
  34. #if ENABLED(BABYSTEPPING)
  35. #include "stepper.h"
  36. #endif
  37. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  38. #include "endstops.h"
  39. #endif
  40. #if ENABLED(USE_WATCHDOG)
  41. #include "watchdog.h"
  42. #endif
  43. #ifdef K1 // Defined in Configuration.h in the PID settings
  44. #define K2 (1.0-K1)
  45. #endif
  46. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  47. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  48. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  49. #else
  50. static void* heater_ttbl_map[HOTENDS] = ARRAY_BY_HOTENDS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE, (void*)HEATER_4_TEMPTABLE);
  51. static uint8_t heater_ttbllen_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN, HEATER_4_TEMPTABLE_LEN);
  52. #endif
  53. Temperature thermalManager;
  54. // public:
  55. float Temperature::current_temperature[HOTENDS] = { 0.0 },
  56. Temperature::current_temperature_bed = 0.0;
  57. int16_t Temperature::current_temperature_raw[HOTENDS] = { 0 },
  58. Temperature::target_temperature[HOTENDS] = { 0 },
  59. Temperature::current_temperature_bed_raw = 0,
  60. Temperature::target_temperature_bed = 0;
  61. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  62. float Temperature::redundant_temperature = 0.0;
  63. #endif
  64. #if ENABLED(PIDTEMP)
  65. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  66. float Temperature::Kp[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kp),
  67. Temperature::Ki[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Ki) * (PID_dT)),
  68. Temperature::Kd[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Kd) / (PID_dT));
  69. #if ENABLED(PID_EXTRUSION_SCALING)
  70. float Temperature::Kc[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kc);
  71. #endif
  72. #else
  73. float Temperature::Kp = DEFAULT_Kp,
  74. Temperature::Ki = (DEFAULT_Ki) * (PID_dT),
  75. Temperature::Kd = (DEFAULT_Kd) / (PID_dT);
  76. #if ENABLED(PID_EXTRUSION_SCALING)
  77. float Temperature::Kc = DEFAULT_Kc;
  78. #endif
  79. #endif
  80. #endif
  81. #if ENABLED(PIDTEMPBED)
  82. float Temperature::bedKp = DEFAULT_bedKp,
  83. Temperature::bedKi = ((DEFAULT_bedKi) * PID_dT),
  84. Temperature::bedKd = ((DEFAULT_bedKd) / PID_dT);
  85. #endif
  86. #if ENABLED(BABYSTEPPING)
  87. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  88. #endif
  89. #if WATCH_HOTENDS
  90. int Temperature::watch_target_temp[HOTENDS] = { 0 };
  91. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  92. #endif
  93. #if WATCH_THE_BED
  94. int Temperature::watch_target_bed_temp = 0;
  95. millis_t Temperature::watch_bed_next_ms = 0;
  96. #endif
  97. #if ENABLED(PREVENT_COLD_EXTRUSION)
  98. bool Temperature::allow_cold_extrude = false;
  99. float Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  100. #endif
  101. // private:
  102. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  103. int Temperature::redundant_temperature_raw = 0;
  104. float Temperature::redundant_temperature = 0.0;
  105. #endif
  106. volatile bool Temperature::temp_meas_ready = false;
  107. #if ENABLED(PIDTEMP)
  108. float Temperature::temp_iState[HOTENDS] = { 0 },
  109. Temperature::temp_dState[HOTENDS] = { 0 },
  110. Temperature::pTerm[HOTENDS],
  111. Temperature::iTerm[HOTENDS],
  112. Temperature::dTerm[HOTENDS];
  113. #if ENABLED(PID_EXTRUSION_SCALING)
  114. float Temperature::cTerm[HOTENDS];
  115. long Temperature::last_e_position;
  116. long Temperature::lpq[LPQ_MAX_LEN];
  117. int Temperature::lpq_ptr = 0;
  118. #endif
  119. float Temperature::pid_error[HOTENDS];
  120. bool Temperature::pid_reset[HOTENDS];
  121. #endif
  122. #if ENABLED(PIDTEMPBED)
  123. float Temperature::temp_iState_bed = { 0 },
  124. Temperature::temp_dState_bed = { 0 },
  125. Temperature::pTerm_bed,
  126. Temperature::iTerm_bed,
  127. Temperature::dTerm_bed,
  128. Temperature::pid_error_bed;
  129. #else
  130. millis_t Temperature::next_bed_check_ms;
  131. #endif
  132. uint16_t Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 },
  133. Temperature::raw_temp_bed_value = 0;
  134. // Init min and max temp with extreme values to prevent false errors during startup
  135. int16_t Temperature::minttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP, HEATER_4_RAW_LO_TEMP),
  136. Temperature::maxttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP, HEATER_4_RAW_HI_TEMP),
  137. Temperature::minttemp[HOTENDS] = { 0 },
  138. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  139. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  140. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  141. #endif
  142. #ifdef MILLISECONDS_PREHEAT_TIME
  143. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  144. #endif
  145. #ifdef BED_MINTEMP
  146. int16_t Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  147. #endif
  148. #ifdef BED_MAXTEMP
  149. int16_t Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  150. #endif
  151. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  152. int16_t Temperature::meas_shift_index; // Index of a delayed sample in buffer
  153. #endif
  154. #if HAS_AUTO_FAN
  155. millis_t Temperature::next_auto_fan_check_ms = 0;
  156. #endif
  157. uint8_t Temperature::soft_pwm_amount[HOTENDS],
  158. Temperature::soft_pwm_amount_bed;
  159. #if ENABLED(FAN_SOFT_PWM)
  160. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  161. Temperature::soft_pwm_count_fan[FAN_COUNT];
  162. #endif
  163. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  164. int Temperature::current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  165. #endif
  166. #if ENABLED(PROBING_HEATERS_OFF)
  167. bool Temperature::paused;
  168. int16_t Temperature::paused_hotend_temp[HOTENDS];
  169. #if HAS_TEMP_BED
  170. int16_t Temperature::paused_bed_temp;
  171. #endif
  172. #endif
  173. #if HAS_PID_HEATING
  174. void Temperature::PID_autotune(float temp, int hotend, int ncycles, bool set_result/*=false*/) {
  175. float input = 0.0;
  176. int cycles = 0;
  177. bool heating = true;
  178. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  179. long t_high = 0, t_low = 0;
  180. long bias, d;
  181. float Ku, Tu;
  182. float workKp = 0, workKi = 0, workKd = 0;
  183. float max = 0, min = 10000;
  184. #if HAS_AUTO_FAN
  185. next_auto_fan_check_ms = temp_ms + 2500UL;
  186. #endif
  187. if (hotend >=
  188. #if ENABLED(PIDTEMP)
  189. HOTENDS
  190. #else
  191. 0
  192. #endif
  193. || hotend <
  194. #if ENABLED(PIDTEMPBED)
  195. -1
  196. #else
  197. 0
  198. #endif
  199. ) {
  200. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  201. return;
  202. }
  203. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  204. disable_all_heaters(); // switch off all heaters.
  205. #if HAS_PID_FOR_BOTH
  206. if (hotend < 0)
  207. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  208. else
  209. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  210. #elif ENABLED(PIDTEMP)
  211. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  212. #else
  213. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  214. #endif
  215. wait_for_heatup = true;
  216. // PID Tuning loop
  217. while (wait_for_heatup) {
  218. millis_t ms = millis();
  219. if (temp_meas_ready) { // temp sample ready
  220. updateTemperaturesFromRawValues();
  221. input =
  222. #if HAS_PID_FOR_BOTH
  223. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  224. #elif ENABLED(PIDTEMP)
  225. current_temperature[hotend]
  226. #else
  227. current_temperature_bed
  228. #endif
  229. ;
  230. NOLESS(max, input);
  231. NOMORE(min, input);
  232. #if HAS_AUTO_FAN
  233. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  234. checkExtruderAutoFans();
  235. next_auto_fan_check_ms = ms + 2500UL;
  236. }
  237. #endif
  238. if (heating && input > temp) {
  239. if (ELAPSED(ms, t2 + 5000UL)) {
  240. heating = false;
  241. #if HAS_PID_FOR_BOTH
  242. if (hotend < 0)
  243. soft_pwm_amount_bed = (bias - d) >> 1;
  244. else
  245. soft_pwm_amount[hotend] = (bias - d) >> 1;
  246. #elif ENABLED(PIDTEMP)
  247. soft_pwm_amount[hotend] = (bias - d) >> 1;
  248. #elif ENABLED(PIDTEMPBED)
  249. soft_pwm_amount_bed = (bias - d) >> 1;
  250. #endif
  251. t1 = ms;
  252. t_high = t1 - t2;
  253. max = temp;
  254. }
  255. }
  256. if (!heating && input < temp) {
  257. if (ELAPSED(ms, t1 + 5000UL)) {
  258. heating = true;
  259. t2 = ms;
  260. t_low = t2 - t1;
  261. if (cycles > 0) {
  262. long max_pow =
  263. #if HAS_PID_FOR_BOTH
  264. hotend < 0 ? MAX_BED_POWER : PID_MAX
  265. #elif ENABLED(PIDTEMP)
  266. PID_MAX
  267. #else
  268. MAX_BED_POWER
  269. #endif
  270. ;
  271. bias += (d * (t_high - t_low)) / (t_low + t_high);
  272. bias = constrain(bias, 20, max_pow - 20);
  273. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  274. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  275. SERIAL_PROTOCOLPAIR(MSG_D, d);
  276. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  277. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  278. if (cycles > 2) {
  279. Ku = (4.0 * d) / (M_PI * (max - min) * 0.5);
  280. Tu = ((float)(t_low + t_high) * 0.001);
  281. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  282. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  283. workKp = 0.6 * Ku;
  284. workKi = 2 * workKp / Tu;
  285. workKd = workKp * Tu * 0.125;
  286. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  287. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  288. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  289. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  290. /**
  291. workKp = 0.33*Ku;
  292. workKi = workKp/Tu;
  293. workKd = workKp*Tu/3;
  294. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  295. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  296. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  297. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  298. workKp = 0.2*Ku;
  299. workKi = 2*workKp/Tu;
  300. workKd = workKp*Tu/3;
  301. SERIAL_PROTOCOLLNPGM(" No overshoot");
  302. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  303. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  304. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  305. */
  306. }
  307. }
  308. #if HAS_PID_FOR_BOTH
  309. if (hotend < 0)
  310. soft_pwm_amount_bed = (bias + d) >> 1;
  311. else
  312. soft_pwm_amount[hotend] = (bias + d) >> 1;
  313. #elif ENABLED(PIDTEMP)
  314. soft_pwm_amount[hotend] = (bias + d) >> 1;
  315. #else
  316. soft_pwm_amount_bed = (bias + d) >> 1;
  317. #endif
  318. cycles++;
  319. min = temp;
  320. }
  321. }
  322. }
  323. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  324. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  325. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  326. return;
  327. }
  328. // Every 2 seconds...
  329. if (ELAPSED(ms, temp_ms + 2000UL)) {
  330. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  331. print_heaterstates();
  332. SERIAL_EOL;
  333. #endif
  334. temp_ms = ms;
  335. } // every 2 seconds
  336. // Over 2 minutes?
  337. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  338. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  339. return;
  340. }
  341. if (cycles > ncycles) {
  342. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  343. #if HAS_PID_FOR_BOTH
  344. const char* estring = hotend < 0 ? "bed" : "";
  345. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL;
  346. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL;
  347. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL;
  348. #elif ENABLED(PIDTEMP)
  349. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL;
  350. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL;
  351. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL;
  352. #else
  353. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL;
  354. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL;
  355. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL;
  356. #endif
  357. #define _SET_BED_PID() do { \
  358. bedKp = workKp; \
  359. bedKi = scalePID_i(workKi); \
  360. bedKd = scalePID_d(workKd); \
  361. updatePID(); } while(0)
  362. #define _SET_EXTRUDER_PID() do { \
  363. PID_PARAM(Kp, hotend) = workKp; \
  364. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  365. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  366. updatePID(); } while(0)
  367. // Use the result? (As with "M303 U1")
  368. if (set_result) {
  369. #if HAS_PID_FOR_BOTH
  370. if (hotend < 0)
  371. _SET_BED_PID();
  372. else
  373. _SET_EXTRUDER_PID();
  374. #elif ENABLED(PIDTEMP)
  375. _SET_EXTRUDER_PID();
  376. #else
  377. _SET_BED_PID();
  378. #endif
  379. }
  380. return;
  381. }
  382. lcd_update();
  383. }
  384. if (!wait_for_heatup) disable_all_heaters();
  385. }
  386. #endif // HAS_PID_HEATING
  387. /**
  388. * Class and Instance Methods
  389. */
  390. Temperature::Temperature() { }
  391. void Temperature::updatePID() {
  392. #if ENABLED(PIDTEMP)
  393. #if ENABLED(PID_EXTRUSION_SCALING)
  394. last_e_position = 0;
  395. #endif
  396. #endif
  397. }
  398. int Temperature::getHeaterPower(int heater) {
  399. return heater < 0 ? soft_pwm_amount_bed : soft_pwm_amount[heater];
  400. }
  401. #if HAS_AUTO_FAN
  402. void Temperature::checkExtruderAutoFans() {
  403. constexpr int8_t fanPin[] = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN };
  404. constexpr int fanBit[] = {
  405. 0,
  406. AUTO_1_IS_0 ? 0 : 1,
  407. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  408. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3,
  409. AUTO_4_IS_0 ? 0 : AUTO_4_IS_1 ? 1 : AUTO_4_IS_2 ? 2 : AUTO_4_IS_3 ? 3 : 4
  410. };
  411. uint8_t fanState = 0;
  412. HOTEND_LOOP() {
  413. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  414. SBI(fanState, fanBit[e]);
  415. }
  416. uint8_t fanDone = 0;
  417. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  418. int8_t pin = fanPin[f];
  419. if (pin >= 0 && !TEST(fanDone, fanBit[f])) {
  420. uint8_t newFanSpeed = TEST(fanState, fanBit[f]) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  421. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  422. digitalWrite(pin, newFanSpeed);
  423. analogWrite(pin, newFanSpeed);
  424. SBI(fanDone, fanBit[f]);
  425. }
  426. }
  427. }
  428. #endif // HAS_AUTO_FAN
  429. //
  430. // Temperature Error Handlers
  431. //
  432. void Temperature::_temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  433. static bool killed = false;
  434. if (IsRunning()) {
  435. SERIAL_ERROR_START;
  436. serialprintPGM(serial_msg);
  437. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  438. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  439. }
  440. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  441. if (!killed) {
  442. Running = false;
  443. killed = true;
  444. kill(lcd_msg);
  445. }
  446. else
  447. disable_all_heaters(); // paranoia
  448. #endif
  449. }
  450. void Temperature::max_temp_error(int8_t e) {
  451. #if HAS_TEMP_BED
  452. _temp_error(e, PSTR(MSG_T_MAXTEMP), e >= 0 ? PSTR(MSG_ERR_MAXTEMP) : PSTR(MSG_ERR_MAXTEMP_BED));
  453. #else
  454. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  455. #if HOTENDS == 1
  456. UNUSED(e);
  457. #endif
  458. #endif
  459. }
  460. void Temperature::min_temp_error(int8_t e) {
  461. #if HAS_TEMP_BED
  462. _temp_error(e, PSTR(MSG_T_MINTEMP), e >= 0 ? PSTR(MSG_ERR_MINTEMP) : PSTR(MSG_ERR_MINTEMP_BED));
  463. #else
  464. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  465. #if HOTENDS == 1
  466. UNUSED(e);
  467. #endif
  468. #endif
  469. }
  470. float Temperature::get_pid_output(int e) {
  471. #if HOTENDS == 1
  472. UNUSED(e);
  473. #define _HOTEND_TEST true
  474. #else
  475. #define _HOTEND_TEST e == active_extruder
  476. #endif
  477. float pid_output;
  478. #if ENABLED(PIDTEMP)
  479. #if DISABLED(PID_OPENLOOP)
  480. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  481. dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
  482. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  483. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  484. pid_output = BANG_MAX;
  485. pid_reset[HOTEND_INDEX] = true;
  486. }
  487. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0) {
  488. pid_output = 0;
  489. pid_reset[HOTEND_INDEX] = true;
  490. }
  491. else {
  492. if (pid_reset[HOTEND_INDEX]) {
  493. temp_iState[HOTEND_INDEX] = 0.0;
  494. pid_reset[HOTEND_INDEX] = false;
  495. }
  496. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  497. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  498. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  499. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  500. #if ENABLED(PID_EXTRUSION_SCALING)
  501. cTerm[HOTEND_INDEX] = 0;
  502. if (_HOTEND_TEST) {
  503. long e_position = stepper.position(E_AXIS);
  504. if (e_position > last_e_position) {
  505. lpq[lpq_ptr] = e_position - last_e_position;
  506. last_e_position = e_position;
  507. }
  508. else {
  509. lpq[lpq_ptr] = 0;
  510. }
  511. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  512. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  513. pid_output += cTerm[HOTEND_INDEX];
  514. }
  515. #endif // PID_EXTRUSION_SCALING
  516. if (pid_output > PID_MAX) {
  517. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  518. pid_output = PID_MAX;
  519. }
  520. else if (pid_output < 0) {
  521. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  522. pid_output = 0;
  523. }
  524. }
  525. #else
  526. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  527. #endif //PID_OPENLOOP
  528. #if ENABLED(PID_DEBUG)
  529. SERIAL_ECHO_START;
  530. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  531. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  532. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  533. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  534. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  535. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  536. #if ENABLED(PID_EXTRUSION_SCALING)
  537. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  538. #endif
  539. SERIAL_EOL;
  540. #endif //PID_DEBUG
  541. #else /* PID off */
  542. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  543. #endif
  544. return pid_output;
  545. }
  546. #if ENABLED(PIDTEMPBED)
  547. float Temperature::get_pid_output_bed() {
  548. float pid_output;
  549. #if DISABLED(PID_OPENLOOP)
  550. pid_error_bed = target_temperature_bed - current_temperature_bed;
  551. pTerm_bed = bedKp * pid_error_bed;
  552. temp_iState_bed += pid_error_bed;
  553. iTerm_bed = bedKi * temp_iState_bed;
  554. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  555. temp_dState_bed = current_temperature_bed;
  556. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  557. if (pid_output > MAX_BED_POWER) {
  558. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  559. pid_output = MAX_BED_POWER;
  560. }
  561. else if (pid_output < 0) {
  562. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  563. pid_output = 0;
  564. }
  565. #else
  566. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  567. #endif // PID_OPENLOOP
  568. #if ENABLED(PID_BED_DEBUG)
  569. SERIAL_ECHO_START;
  570. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  571. SERIAL_ECHOPGM(": Input ");
  572. SERIAL_ECHO(current_temperature_bed);
  573. SERIAL_ECHOPGM(" Output ");
  574. SERIAL_ECHO(pid_output);
  575. SERIAL_ECHOPGM(" pTerm ");
  576. SERIAL_ECHO(pTerm_bed);
  577. SERIAL_ECHOPGM(" iTerm ");
  578. SERIAL_ECHO(iTerm_bed);
  579. SERIAL_ECHOPGM(" dTerm ");
  580. SERIAL_ECHOLN(dTerm_bed);
  581. #endif //PID_BED_DEBUG
  582. return pid_output;
  583. }
  584. #endif //PIDTEMPBED
  585. /**
  586. * Manage heating activities for extruder hot-ends and a heated bed
  587. * - Acquire updated temperature readings
  588. * - Also resets the watchdog timer
  589. * - Invoke thermal runaway protection
  590. * - Manage extruder auto-fan
  591. * - Apply filament width to the extrusion rate (may move)
  592. * - Update the heated bed PID output value
  593. */
  594. /**
  595. * The following line SOMETIMES results in the dreaded "unable to find a register to spill in class 'POINTER_REGS'"
  596. * compile error.
  597. * 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);
  598. *
  599. * This is due to a bug in the C++ compiler used by the Arduino IDE from 1.6.10 to at least 1.8.1.
  600. *
  601. * The work around is to add the compiler flag "__attribute__((__optimize__("O2")))" to the declaration for manage_heater()
  602. */
  603. //void Temperature::manage_heater() __attribute__((__optimize__("O2")));
  604. void Temperature::manage_heater() {
  605. if (!temp_meas_ready) return;
  606. updateTemperaturesFromRawValues(); // also resets the watchdog
  607. #if ENABLED(HEATER_0_USES_MAX6675)
  608. if (current_temperature[0] > min(HEATER_0_MAXTEMP, MAX6675_TMAX - 1)) max_temp_error(0);
  609. if (current_temperature[0] < max(HEATER_0_MINTEMP, MAX6675_TMIN + 0.01)) min_temp_error(0);
  610. #endif
  611. #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN
  612. millis_t ms = millis();
  613. #endif
  614. // Loop through all hotends
  615. HOTEND_LOOP() {
  616. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  617. 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);
  618. #endif
  619. float pid_output = get_pid_output(e);
  620. // Check if temperature is within the correct range
  621. soft_pwm_amount[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  622. // Check if the temperature is failing to increase
  623. #if WATCH_HOTENDS
  624. // Is it time to check this extruder's heater?
  625. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) {
  626. // Has it failed to increase enough?
  627. if (degHotend(e) < watch_target_temp[e]) {
  628. // Stop!
  629. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  630. }
  631. else {
  632. // Start again if the target is still far off
  633. start_watching_heater(e);
  634. }
  635. }
  636. #endif // THERMAL_PROTECTION_HOTENDS
  637. // Check if the temperature is failing to increase
  638. #if WATCH_THE_BED
  639. // Is it time to check the bed?
  640. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) {
  641. // Has it failed to increase enough?
  642. if (degBed() < watch_target_bed_temp) {
  643. // Stop!
  644. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  645. }
  646. else {
  647. // Start again if the target is still far off
  648. start_watching_bed();
  649. }
  650. }
  651. #endif // THERMAL_PROTECTION_HOTENDS
  652. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  653. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  654. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  655. }
  656. #endif
  657. } // HOTEND_LOOP
  658. #if HAS_AUTO_FAN
  659. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  660. checkExtruderAutoFans();
  661. next_auto_fan_check_ms = ms + 2500UL;
  662. }
  663. #endif
  664. // Control the extruder rate based on the width sensor
  665. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  666. if (filament_sensor) {
  667. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  668. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  669. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  670. // Get the delayed info and add 100 to reconstitute to a percent of
  671. // the nominal filament diameter then square it to get an area
  672. const float vmroot = measurement_delay[meas_shift_index] * 0.01 + 1.0;
  673. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vmroot <= 0.1 ? 0.01 : sq(vmroot);
  674. }
  675. #endif // FILAMENT_WIDTH_SENSOR
  676. #if DISABLED(PIDTEMPBED)
  677. if (PENDING(ms, next_bed_check_ms)) return;
  678. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  679. #endif
  680. #if TEMP_SENSOR_BED != 0
  681. #if HAS_THERMALLY_PROTECTED_BED
  682. 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);
  683. #endif
  684. #if ENABLED(PIDTEMPBED)
  685. float pid_output = get_pid_output_bed();
  686. soft_pwm_amount_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)pid_output >> 1 : 0;
  687. #elif ENABLED(BED_LIMIT_SWITCHING)
  688. // Check if temperature is within the correct band
  689. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  690. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  691. soft_pwm_amount_bed = 0;
  692. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  693. soft_pwm_amount_bed = MAX_BED_POWER >> 1;
  694. }
  695. else {
  696. soft_pwm_amount_bed = 0;
  697. WRITE_HEATER_BED(LOW);
  698. }
  699. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  700. // Check if temperature is within the correct range
  701. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  702. soft_pwm_amount_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  703. }
  704. else {
  705. soft_pwm_amount_bed = 0;
  706. WRITE_HEATER_BED(LOW);
  707. }
  708. #endif
  709. #endif //TEMP_SENSOR_BED != 0
  710. }
  711. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  712. // Derived from RepRap FiveD extruder::getTemperature()
  713. // For hot end temperature measurement.
  714. float Temperature::analog2temp(int raw, uint8_t e) {
  715. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  716. if (e > HOTENDS)
  717. #else
  718. if (e >= HOTENDS)
  719. #endif
  720. {
  721. SERIAL_ERROR_START;
  722. SERIAL_ERROR((int)e);
  723. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  724. kill(PSTR(MSG_KILLED));
  725. return 0.0;
  726. }
  727. #if ENABLED(HEATER_0_USES_MAX6675)
  728. if (e == 0) return 0.25 * raw;
  729. #endif
  730. if (heater_ttbl_map[e] != NULL) {
  731. float celsius = 0;
  732. uint8_t i;
  733. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  734. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  735. if (PGM_RD_W((*tt)[i][0]) > raw) {
  736. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  737. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  738. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  739. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  740. break;
  741. }
  742. }
  743. // Overflow: Set to last value in the table
  744. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  745. return celsius;
  746. }
  747. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  748. }
  749. // Derived from RepRap FiveD extruder::getTemperature()
  750. // For bed temperature measurement.
  751. float Temperature::analog2tempBed(int raw) {
  752. #if ENABLED(BED_USES_THERMISTOR)
  753. float celsius = 0;
  754. byte i;
  755. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  756. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  757. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  758. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  759. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  760. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  761. break;
  762. }
  763. }
  764. // Overflow: Set to last value in the table
  765. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  766. return celsius;
  767. #elif defined(BED_USES_AD595)
  768. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  769. #else
  770. UNUSED(raw);
  771. return 0;
  772. #endif
  773. }
  774. /**
  775. * Get the raw values into the actual temperatures.
  776. * The raw values are created in interrupt context,
  777. * and this function is called from normal context
  778. * as it would block the stepper routine.
  779. */
  780. void Temperature::updateTemperaturesFromRawValues() {
  781. #if ENABLED(HEATER_0_USES_MAX6675)
  782. current_temperature_raw[0] = read_max6675();
  783. #endif
  784. HOTEND_LOOP()
  785. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  786. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  787. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  788. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  789. #endif
  790. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  791. filament_width_meas = analog2widthFil();
  792. #endif
  793. #if ENABLED(USE_WATCHDOG)
  794. // Reset the watchdog after we know we have a temperature measurement.
  795. watchdog_reset();
  796. #endif
  797. CRITICAL_SECTION_START;
  798. temp_meas_ready = false;
  799. CRITICAL_SECTION_END;
  800. }
  801. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  802. // Convert raw Filament Width to millimeters
  803. float Temperature::analog2widthFil() {
  804. return current_raw_filwidth / 16383.0 * 5.0;
  805. //return current_raw_filwidth;
  806. }
  807. // Convert raw Filament Width to a ratio
  808. int Temperature::widthFil_to_size_ratio() {
  809. float temp = filament_width_meas;
  810. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  811. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  812. return filament_width_nominal / temp * 100;
  813. }
  814. #endif
  815. #if ENABLED(HEATER_0_USES_MAX6675)
  816. #ifndef MAX6675_SCK_PIN
  817. #define MAX6675_SCK_PIN SCK_PIN
  818. #endif
  819. #ifndef MAX6675_DO_PIN
  820. #define MAX6675_DO_PIN MISO_PIN
  821. #endif
  822. Spi<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  823. #endif
  824. /**
  825. * Initialize the temperature manager
  826. * The manager is implemented by periodic calls to manage_heater()
  827. */
  828. void Temperature::init() {
  829. #if MB(RUMBA) && (TEMP_SENSOR_0 == -1 || TEMP_SENSOR_1 == -1 || TEMP_SENSOR_2 == -1 || TEMP_SENSOR_BED == -1)
  830. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  831. MCUCR = _BV(JTD);
  832. MCUCR = _BV(JTD);
  833. #endif
  834. // Finish init of mult hotend arrays
  835. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  836. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  837. last_e_position = 0;
  838. #endif
  839. #if HAS_HEATER_0
  840. SET_OUTPUT(HEATER_0_PIN);
  841. #endif
  842. #if HAS_HEATER_1
  843. SET_OUTPUT(HEATER_1_PIN);
  844. #endif
  845. #if HAS_HEATER_2
  846. SET_OUTPUT(HEATER_2_PIN);
  847. #endif
  848. #if HAS_HEATER_3
  849. SET_OUTPUT(HEATER_3_PIN);
  850. #endif
  851. #if HAS_HEATER_4
  852. SET_OUTPUT(HEATER_3_PIN);
  853. #endif
  854. #if HAS_HEATER_BED
  855. SET_OUTPUT(HEATER_BED_PIN);
  856. #endif
  857. #if HAS_FAN0
  858. SET_OUTPUT(FAN_PIN);
  859. #if ENABLED(FAST_PWM_FAN)
  860. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  861. #endif
  862. #endif
  863. #if HAS_FAN1
  864. SET_OUTPUT(FAN1_PIN);
  865. #if ENABLED(FAST_PWM_FAN)
  866. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  867. #endif
  868. #endif
  869. #if HAS_FAN2
  870. SET_OUTPUT(FAN2_PIN);
  871. #if ENABLED(FAST_PWM_FAN)
  872. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  873. #endif
  874. #endif
  875. #if ENABLED(HEATER_0_USES_MAX6675)
  876. OUT_WRITE(SCK_PIN, LOW);
  877. OUT_WRITE(MOSI_PIN, HIGH);
  878. SET_INPUT_PULLUP(MISO_PIN);
  879. max6675_spi.init();
  880. OUT_WRITE(SS_PIN, HIGH);
  881. OUT_WRITE(MAX6675_SS, HIGH);
  882. #endif // HEATER_0_USES_MAX6675
  883. #ifdef DIDR2
  884. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  885. #else
  886. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  887. #endif
  888. // Set analog inputs
  889. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  890. DIDR0 = 0;
  891. #ifdef DIDR2
  892. DIDR2 = 0;
  893. #endif
  894. #if HAS_TEMP_0
  895. ANALOG_SELECT(TEMP_0_PIN);
  896. #endif
  897. #if HAS_TEMP_1
  898. ANALOG_SELECT(TEMP_1_PIN);
  899. #endif
  900. #if HAS_TEMP_2
  901. ANALOG_SELECT(TEMP_2_PIN);
  902. #endif
  903. #if HAS_TEMP_3
  904. ANALOG_SELECT(TEMP_3_PIN);
  905. #endif
  906. #if HAS_TEMP_4
  907. ANALOG_SELECT(TEMP_4_PIN);
  908. #endif
  909. #if HAS_TEMP_BED
  910. ANALOG_SELECT(TEMP_BED_PIN);
  911. #endif
  912. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  913. ANALOG_SELECT(FILWIDTH_PIN);
  914. #endif
  915. #if HAS_AUTO_FAN_0
  916. #if E0_AUTO_FAN_PIN == FAN1_PIN
  917. SET_OUTPUT(E0_AUTO_FAN_PIN);
  918. #if ENABLED(FAST_PWM_FAN)
  919. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  920. #endif
  921. #else
  922. SET_OUTPUT(E0_AUTO_FAN_PIN);
  923. #endif
  924. #endif
  925. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  926. #if E1_AUTO_FAN_PIN == FAN1_PIN
  927. SET_OUTPUT(E1_AUTO_FAN_PIN);
  928. #if ENABLED(FAST_PWM_FAN)
  929. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  930. #endif
  931. #else
  932. SET_OUTPUT(E1_AUTO_FAN_PIN);
  933. #endif
  934. #endif
  935. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  936. #if E2_AUTO_FAN_PIN == FAN1_PIN
  937. SET_OUTPUT(E2_AUTO_FAN_PIN);
  938. #if ENABLED(FAST_PWM_FAN)
  939. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  940. #endif
  941. #else
  942. SET_OUTPUT(E2_AUTO_FAN_PIN);
  943. #endif
  944. #endif
  945. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  946. #if E3_AUTO_FAN_PIN == FAN1_PIN
  947. SET_OUTPUT(E3_AUTO_FAN_PIN);
  948. #if ENABLED(FAST_PWM_FAN)
  949. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  950. #endif
  951. #else
  952. SET_OUTPUT(E3_AUTO_FAN_PIN);
  953. #endif
  954. #endif
  955. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  956. #if E4_AUTO_FAN_PIN == FAN1_PIN
  957. SET_OUTPUT(E4_AUTO_FAN_PIN);
  958. #if ENABLED(FAST_PWM_FAN)
  959. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  960. #endif
  961. #else
  962. SET_OUTPUT(E4_AUTO_FAN_PIN);
  963. #endif
  964. #endif
  965. // Use timer0 for temperature measurement
  966. // Interleave temperature interrupt with millies interrupt
  967. OCR0B = 128;
  968. SBI(TIMSK0, OCIE0B);
  969. // Wait for temperature measurement to settle
  970. delay(250);
  971. #define TEMP_MIN_ROUTINE(NR) \
  972. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  973. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  974. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  975. minttemp_raw[NR] += OVERSAMPLENR; \
  976. else \
  977. minttemp_raw[NR] -= OVERSAMPLENR; \
  978. }
  979. #define TEMP_MAX_ROUTINE(NR) \
  980. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  981. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  982. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  983. maxttemp_raw[NR] -= OVERSAMPLENR; \
  984. else \
  985. maxttemp_raw[NR] += OVERSAMPLENR; \
  986. }
  987. #ifdef HEATER_0_MINTEMP
  988. TEMP_MIN_ROUTINE(0);
  989. #endif
  990. #ifdef HEATER_0_MAXTEMP
  991. TEMP_MAX_ROUTINE(0);
  992. #endif
  993. #if HOTENDS > 1
  994. #ifdef HEATER_1_MINTEMP
  995. TEMP_MIN_ROUTINE(1);
  996. #endif
  997. #ifdef HEATER_1_MAXTEMP
  998. TEMP_MAX_ROUTINE(1);
  999. #endif
  1000. #if HOTENDS > 2
  1001. #ifdef HEATER_2_MINTEMP
  1002. TEMP_MIN_ROUTINE(2);
  1003. #endif
  1004. #ifdef HEATER_2_MAXTEMP
  1005. TEMP_MAX_ROUTINE(2);
  1006. #endif
  1007. #if HOTENDS > 3
  1008. #ifdef HEATER_3_MINTEMP
  1009. TEMP_MIN_ROUTINE(3);
  1010. #endif
  1011. #ifdef HEATER_3_MAXTEMP
  1012. TEMP_MAX_ROUTINE(3);
  1013. #endif
  1014. #if HOTENDS > 4
  1015. #ifdef HEATER_4_MINTEMP
  1016. TEMP_MIN_ROUTINE(4);
  1017. #endif
  1018. #ifdef HEATER_4_MAXTEMP
  1019. TEMP_MAX_ROUTINE(4);
  1020. #endif
  1021. #endif // HOTENDS > 4
  1022. #endif // HOTENDS > 3
  1023. #endif // HOTENDS > 2
  1024. #endif // HOTENDS > 1
  1025. #ifdef BED_MINTEMP
  1026. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1027. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1028. bed_minttemp_raw += OVERSAMPLENR;
  1029. #else
  1030. bed_minttemp_raw -= OVERSAMPLENR;
  1031. #endif
  1032. }
  1033. #endif //BED_MINTEMP
  1034. #ifdef BED_MAXTEMP
  1035. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1036. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1037. bed_maxttemp_raw -= OVERSAMPLENR;
  1038. #else
  1039. bed_maxttemp_raw += OVERSAMPLENR;
  1040. #endif
  1041. }
  1042. #endif //BED_MAXTEMP
  1043. #if ENABLED(PROBING_HEATERS_OFF)
  1044. paused = false;
  1045. ZERO(paused_hotend_temp);
  1046. #if HAS_TEMP_BED
  1047. paused_bed_temp = 0;
  1048. #endif
  1049. #endif
  1050. }
  1051. #if WATCH_HOTENDS
  1052. /**
  1053. * Start Heating Sanity Check for hotends that are below
  1054. * their target temperature by a configurable margin.
  1055. * This is called when the temperature is set. (M104, M109)
  1056. */
  1057. void Temperature::start_watching_heater(uint8_t e) {
  1058. #if HOTENDS == 1
  1059. UNUSED(e);
  1060. #endif
  1061. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1062. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1063. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1064. }
  1065. else
  1066. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1067. }
  1068. #endif
  1069. #if WATCH_THE_BED
  1070. /**
  1071. * Start Heating Sanity Check for hotends that are below
  1072. * their target temperature by a configurable margin.
  1073. * This is called when the temperature is set. (M140, M190)
  1074. */
  1075. void Temperature::start_watching_bed() {
  1076. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1077. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1078. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1079. }
  1080. else
  1081. watch_bed_next_ms = 0;
  1082. }
  1083. #endif
  1084. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1085. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1086. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1087. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1088. #endif
  1089. #if HAS_THERMALLY_PROTECTED_BED
  1090. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1091. millis_t Temperature::thermal_runaway_bed_timer;
  1092. #endif
  1093. void Temperature::thermal_runaway_protection(Temperature::TRState* state, millis_t* timer, float current, float target, int heater_id, int period_seconds, int hysteresis_degc) {
  1094. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1095. /**
  1096. SERIAL_ECHO_START;
  1097. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1098. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1099. SERIAL_ECHOPAIR(" ; State:", *state);
  1100. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1101. SERIAL_ECHOPAIR(" ; Temperature:", current);
  1102. SERIAL_ECHOPAIR(" ; Target Temp:", target);
  1103. SERIAL_EOL;
  1104. */
  1105. int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1106. // If the target temperature changes, restart
  1107. if (tr_target_temperature[heater_index] != target) {
  1108. tr_target_temperature[heater_index] = target;
  1109. *state = target > 0 ? TRFirstHeating : TRInactive;
  1110. }
  1111. switch (*state) {
  1112. // Inactive state waits for a target temperature to be set
  1113. case TRInactive: break;
  1114. // When first heating, wait for the temperature to be reached then go to Stable state
  1115. case TRFirstHeating:
  1116. if (current < tr_target_temperature[heater_index]) break;
  1117. *state = TRStable;
  1118. // While the temperature is stable watch for a bad temperature
  1119. case TRStable:
  1120. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1121. *timer = millis() + period_seconds * 1000UL;
  1122. break;
  1123. }
  1124. else if (PENDING(millis(), *timer)) break;
  1125. *state = TRRunaway;
  1126. case TRRunaway:
  1127. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1128. }
  1129. }
  1130. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1131. void Temperature::disable_all_heaters() {
  1132. #if ENABLED(AUTOTEMP)
  1133. planner.autotemp_enabled = false;
  1134. #endif
  1135. HOTEND_LOOP() setTargetHotend(0, e);
  1136. setTargetBed(0);
  1137. // Unpause and reset everything
  1138. #if ENABLED(PROBING_HEATERS_OFF)
  1139. paused = false;
  1140. ZERO(paused_hotend_temp);
  1141. #if HAS_TEMP_BED
  1142. paused_bed_temp = 0;
  1143. #endif
  1144. #endif
  1145. // If all heaters go down then for sure our print job has stopped
  1146. print_job_timer.stop();
  1147. #define DISABLE_HEATER(NR) { \
  1148. setTargetHotend(0, NR); \
  1149. soft_pwm_amount[NR] = 0; \
  1150. WRITE_HEATER_ ##NR (LOW); \
  1151. }
  1152. #if HAS_TEMP_HOTEND
  1153. DISABLE_HEATER(0);
  1154. #if HOTENDS > 1
  1155. DISABLE_HEATER(1);
  1156. #if HOTENDS > 2
  1157. DISABLE_HEATER(2);
  1158. #if HOTENDS > 3
  1159. DISABLE_HEATER(3);
  1160. #if HOTENDS > 4
  1161. DISABLE_HEATER(4);
  1162. #endif // HOTENDS > 4
  1163. #endif // HOTENDS > 3
  1164. #endif // HOTENDS > 2
  1165. #endif // HOTENDS > 1
  1166. #endif
  1167. #if HAS_TEMP_BED
  1168. target_temperature_bed = 0;
  1169. soft_pwm_amount_bed = 0;
  1170. #if HAS_HEATER_BED
  1171. WRITE_HEATER_BED(LOW);
  1172. #endif
  1173. #endif
  1174. }
  1175. #if ENABLED(PROBING_HEATERS_OFF)
  1176. void Temperature::pause(const bool p) {
  1177. if (p != paused) {
  1178. paused = p;
  1179. if (p) {
  1180. HOTEND_LOOP() {
  1181. paused_hotend_temp[e] = degTargetHotend(e);
  1182. setTargetHotend(0, e);
  1183. }
  1184. #if HAS_TEMP_BED
  1185. paused_bed_temp = degTargetBed();
  1186. setTargetBed(0);
  1187. #endif
  1188. }
  1189. else {
  1190. HOTEND_LOOP() setTargetHotend(paused_hotend_temp[e], e);
  1191. #if HAS_TEMP_BED
  1192. setTargetBed(paused_bed_temp);
  1193. #endif
  1194. }
  1195. }
  1196. }
  1197. #endif // PROBING_HEATERS_OFF
  1198. #if ENABLED(HEATER_0_USES_MAX6675)
  1199. #define MAX6675_HEAT_INTERVAL 250u
  1200. #if ENABLED(MAX6675_IS_MAX31855)
  1201. uint32_t max6675_temp = 2000;
  1202. #define MAX6675_ERROR_MASK 7
  1203. #define MAX6675_DISCARD_BITS 18
  1204. #define MAX6675_SPEED_BITS (_BV(SPR1)) // clock ÷ 64
  1205. #else
  1206. uint16_t max6675_temp = 2000;
  1207. #define MAX6675_ERROR_MASK 4
  1208. #define MAX6675_DISCARD_BITS 3
  1209. #define MAX6675_SPEED_BITS (_BV(SPR0)) // clock ÷ 16
  1210. #endif
  1211. int Temperature::read_max6675() {
  1212. static millis_t next_max6675_ms = 0;
  1213. millis_t ms = millis();
  1214. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1215. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1216. CBI(
  1217. #ifdef PRR
  1218. PRR
  1219. #elif defined(PRR0)
  1220. PRR0
  1221. #endif
  1222. , PRSPI);
  1223. SPCR = _BV(MSTR) | _BV(SPE) | MAX6675_SPEED_BITS;
  1224. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1225. // ensure 100ns delay - a bit extra is fine
  1226. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1227. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1228. // Read a big-endian temperature value
  1229. max6675_temp = 0;
  1230. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1231. max6675_temp |= max6675_spi.receive();
  1232. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1233. }
  1234. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1235. if (max6675_temp & MAX6675_ERROR_MASK) {
  1236. SERIAL_ERROR_START;
  1237. SERIAL_ERRORPGM("Temp measurement error! ");
  1238. #if MAX6675_ERROR_MASK == 7
  1239. SERIAL_ERRORPGM("MAX31855 ");
  1240. if (max6675_temp & 1)
  1241. SERIAL_ERRORLNPGM("Open Circuit");
  1242. else if (max6675_temp & 2)
  1243. SERIAL_ERRORLNPGM("Short to GND");
  1244. else if (max6675_temp & 4)
  1245. SERIAL_ERRORLNPGM("Short to VCC");
  1246. #else
  1247. SERIAL_ERRORLNPGM("MAX6675");
  1248. #endif
  1249. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1250. }
  1251. else
  1252. max6675_temp >>= MAX6675_DISCARD_BITS;
  1253. #if ENABLED(MAX6675_IS_MAX31855)
  1254. // Support negative temperature
  1255. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000;
  1256. #endif
  1257. return (int)max6675_temp;
  1258. }
  1259. #endif //HEATER_0_USES_MAX6675
  1260. /**
  1261. * Get raw temperatures
  1262. */
  1263. void Temperature::set_current_temp_raw() {
  1264. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1265. current_temperature_raw[0] = raw_temp_value[0];
  1266. #endif
  1267. #if HAS_TEMP_1
  1268. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1269. redundant_temperature_raw = raw_temp_value[1];
  1270. #else
  1271. current_temperature_raw[1] = raw_temp_value[1];
  1272. #endif
  1273. #if HAS_TEMP_2
  1274. current_temperature_raw[2] = raw_temp_value[2];
  1275. #if HAS_TEMP_3
  1276. current_temperature_raw[3] = raw_temp_value[3];
  1277. #if HAS_TEMP_4
  1278. current_temperature_raw[4] = raw_temp_value[4];
  1279. #endif
  1280. #endif
  1281. #endif
  1282. #endif
  1283. current_temperature_bed_raw = raw_temp_bed_value;
  1284. temp_meas_ready = true;
  1285. }
  1286. #if ENABLED(PINS_DEBUGGING)
  1287. /**
  1288. * monitors endstops & Z probe for changes
  1289. *
  1290. * If a change is detected then the LED is toggled and
  1291. * a message is sent out the serial port
  1292. *
  1293. * Yes, we could miss a rapid back & forth change but
  1294. * that won't matter because this is all manual.
  1295. *
  1296. */
  1297. void endstop_monitor() {
  1298. static uint16_t old_endstop_bits_local = 0;
  1299. static uint8_t local_LED_status = 0;
  1300. uint16_t current_endstop_bits_local = 0;
  1301. #if HAS_X_MIN
  1302. if (READ(X_MIN_PIN)) SBI(current_endstop_bits_local, X_MIN);
  1303. #endif
  1304. #if HAS_X_MAX
  1305. if (READ(X_MAX_PIN)) SBI(current_endstop_bits_local, X_MAX);
  1306. #endif
  1307. #if HAS_Y_MIN
  1308. if (READ(Y_MIN_PIN)) SBI(current_endstop_bits_local, Y_MIN);
  1309. #endif
  1310. #if HAS_Y_MAX
  1311. if (READ(Y_MAX_PIN)) SBI(current_endstop_bits_local, Y_MAX);
  1312. #endif
  1313. #if HAS_Z_MIN
  1314. if (READ(Z_MIN_PIN)) SBI(current_endstop_bits_local, Z_MIN);
  1315. #endif
  1316. #if HAS_Z_MAX
  1317. if (READ(Z_MAX_PIN)) SBI(current_endstop_bits_local, Z_MAX);
  1318. #endif
  1319. #if HAS_Z_MIN_PROBE_PIN
  1320. if (READ(Z_MIN_PROBE_PIN)) SBI(current_endstop_bits_local, Z_MIN_PROBE);
  1321. #endif
  1322. #if HAS_Z2_MIN
  1323. if (READ(Z2_MIN_PIN)) SBI(current_endstop_bits_local, Z2_MIN);
  1324. #endif
  1325. #if HAS_Z2_MAX
  1326. if (READ(Z2_MAX_PIN)) SBI(current_endstop_bits_local, Z2_MAX);
  1327. #endif
  1328. uint16_t endstop_change = current_endstop_bits_local ^ old_endstop_bits_local;
  1329. if (endstop_change) {
  1330. #if HAS_X_MIN
  1331. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR("X_MIN:", !!TEST(current_endstop_bits_local, X_MIN));
  1332. #endif
  1333. #if HAS_X_MAX
  1334. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", !!TEST(current_endstop_bits_local, X_MAX));
  1335. #endif
  1336. #if HAS_Y_MIN
  1337. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", !!TEST(current_endstop_bits_local, Y_MIN));
  1338. #endif
  1339. #if HAS_Y_MAX
  1340. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", !!TEST(current_endstop_bits_local, Y_MAX));
  1341. #endif
  1342. #if HAS_Z_MIN
  1343. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", !!TEST(current_endstop_bits_local, Z_MIN));
  1344. #endif
  1345. #if HAS_Z_MAX
  1346. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", !!TEST(current_endstop_bits_local, Z_MAX));
  1347. #endif
  1348. #if HAS_Z_MIN_PROBE_PIN
  1349. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", !!TEST(current_endstop_bits_local, Z_MIN_PROBE));
  1350. #endif
  1351. #if HAS_Z2_MIN
  1352. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", !!TEST(current_endstop_bits_local, Z2_MIN));
  1353. #endif
  1354. #if HAS_Z2_MAX
  1355. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", !!TEST(current_endstop_bits_local, Z2_MAX));
  1356. #endif
  1357. SERIAL_PROTOCOLPGM("\n\n");
  1358. analogWrite(LED_PIN, local_LED_status);
  1359. local_LED_status ^= 255;
  1360. old_endstop_bits_local = current_endstop_bits_local;
  1361. }
  1362. }
  1363. #endif // PINS_DEBUGGING
  1364. /**
  1365. * Timer 0 is shared with millies so don't change the prescaler.
  1366. *
  1367. * This ISR uses the compare method so it runs at the base
  1368. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1369. * in OCR0B above (128 or halfway between OVFs).
  1370. *
  1371. * - Manage PWM to all the heaters and fan
  1372. * - Prepare or Measure one of the raw ADC sensor values
  1373. * - Check new temperature values for MIN/MAX errors (kill on error)
  1374. * - Step the babysteps value for each axis towards 0
  1375. * - For PINS_DEBUGGING, monitor and report endstop pins
  1376. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1377. */
  1378. ISR(TIMER0_COMPB_vect) { Temperature::isr(); }
  1379. volatile bool Temperature::in_temp_isr = false;
  1380. void Temperature::isr() {
  1381. // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR
  1382. // at the end of its run, potentially causing re-entry. This flag prevents it.
  1383. if (in_temp_isr) return;
  1384. in_temp_isr = true;
  1385. // Allow UART and stepper ISRs
  1386. CBI(TIMSK0, OCIE0B); //Disable Temperature ISR
  1387. sei();
  1388. static int8_t temp_count = -1;
  1389. static ADCSensorState adc_sensor_state = StartupDelay;
  1390. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1391. // avoid multiple loads of pwm_count
  1392. uint8_t pwm_count_tmp = pwm_count;
  1393. // Static members for each heater
  1394. #if ENABLED(SLOW_PWM_HEATERS)
  1395. static uint8_t slow_pwm_count = 0;
  1396. #define ISR_STATICS(n) \
  1397. static uint8_t soft_pwm_count_ ## n, \
  1398. state_heater_ ## n = 0, \
  1399. state_timer_heater_ ## n = 0
  1400. #else
  1401. #define ISR_STATICS(n) static uint8_t soft_pwm_count_ ## n = 0
  1402. #endif
  1403. // Statics per heater
  1404. ISR_STATICS(0);
  1405. #if HOTENDS > 1
  1406. ISR_STATICS(1);
  1407. #if HOTENDS > 2
  1408. ISR_STATICS(2);
  1409. #if HOTENDS > 3
  1410. ISR_STATICS(3);
  1411. #if HOTENDS > 4
  1412. ISR_STATICS(4);
  1413. #endif // HOTENDS > 4
  1414. #endif // HOTENDS > 3
  1415. #endif // HOTENDS > 2
  1416. #endif // HOTENDS > 1
  1417. #if HAS_HEATER_BED
  1418. ISR_STATICS(BED);
  1419. #endif
  1420. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1421. static unsigned long raw_filwidth_value = 0;
  1422. #endif
  1423. #if DISABLED(SLOW_PWM_HEATERS)
  1424. constexpr uint8_t pwm_mask =
  1425. #if ENABLED(SOFT_PWM_DITHER)
  1426. _BV(SOFT_PWM_SCALE) - 1
  1427. #else
  1428. 0
  1429. #endif
  1430. ;
  1431. /**
  1432. * Standard PWM modulation
  1433. */
  1434. if (pwm_count_tmp >= 127) {
  1435. pwm_count_tmp -= 127;
  1436. soft_pwm_count_0 = (soft_pwm_count_0 & pwm_mask) + soft_pwm_amount[0];
  1437. WRITE_HEATER_0(soft_pwm_count_0 > pwm_mask ? HIGH : LOW);
  1438. #if HOTENDS > 1
  1439. soft_pwm_count_1 = (soft_pwm_count_1 & pwm_mask) + soft_pwm_amount[1];
  1440. WRITE_HEATER_1(soft_pwm_count_1 > pwm_mask ? HIGH : LOW);
  1441. #if HOTENDS > 2
  1442. soft_pwm_count_2 = (soft_pwm_count_2 & pwm_mask) + soft_pwm_amount[2];
  1443. WRITE_HEATER_2(soft_pwm_count_2 > pwm_mask ? HIGH : LOW);
  1444. #if HOTENDS > 3
  1445. soft_pwm_count_3 = (soft_pwm_count_3 & pwm_mask) + soft_pwm_amount[3];
  1446. WRITE_HEATER_3(soft_pwm_count_3 > pwm_mask ? HIGH : LOW);
  1447. #if HOTENDS > 4
  1448. soft_pwm_count_4 = (soft_pwm_count_4 & pwm_mask) + soft_pwm_amount[4];
  1449. WRITE_HEATER_4(soft_pwm_count_4 > pwm_mask ? HIGH : LOW);
  1450. #endif // HOTENDS > 4
  1451. #endif // HOTENDS > 3
  1452. #endif // HOTENDS > 2
  1453. #endif // HOTENDS > 1
  1454. #if HAS_HEATER_BED
  1455. soft_pwm_count_BED = (soft_pwm_count_BED & pwm_mask) + soft_pwm_amount_bed;
  1456. WRITE_HEATER_BED(soft_pwm_count_BED > pwm_mask ? HIGH : LOW);
  1457. #endif
  1458. #if ENABLED(FAN_SOFT_PWM)
  1459. #if HAS_FAN0
  1460. soft_pwm_count_fan[0] = (soft_pwm_count_fan[0] & pwm_mask) + soft_pwm_amount_fan[0] >> 1;
  1461. WRITE_FAN(soft_pwm_count_fan[0] > pwm_mask ? HIGH : LOW);
  1462. #endif
  1463. #if HAS_FAN1
  1464. soft_pwm_count_fan[1] = (soft_pwm_count_fan[1] & pwm_mask) + soft_pwm_amount_fan[1] >> 1;
  1465. WRITE_FAN1(soft_pwm_count_fan[1] > pwm_mask ? HIGH : LOW);
  1466. #endif
  1467. #if HAS_FAN2
  1468. soft_pwm_count_fan[2] = (soft_pwm_count_fan[2] & pwm_mask) + soft_pwm_amount_fan[2] >> 1;
  1469. WRITE_FAN2(soft_pwm_count_fan[2] > pwm_mask ? HIGH : LOW);
  1470. #endif
  1471. #endif
  1472. }
  1473. else {
  1474. if (soft_pwm_count_0 <= pwm_count_tmp) WRITE_HEATER_0(0);
  1475. #if HOTENDS > 1
  1476. if (soft_pwm_count_1 <= pwm_count_tmp) WRITE_HEATER_1(0);
  1477. #if HOTENDS > 2
  1478. if (soft_pwm_count_2 <= pwm_count_tmp) WRITE_HEATER_2(0);
  1479. #if HOTENDS > 3
  1480. if (soft_pwm_count_3 <= pwm_count_tmp) WRITE_HEATER_3(0);
  1481. #if HOTENDS > 4
  1482. if (soft_pwm_count_4 <= pwm_count_tmp) WRITE_HEATER_4(0);
  1483. #endif // HOTENDS > 4
  1484. #endif // HOTENDS > 3
  1485. #endif // HOTENDS > 2
  1486. #endif // HOTENDS > 1
  1487. #if HAS_HEATER_BED
  1488. if (soft_pwm_count_BED <= pwm_count_tmp) WRITE_HEATER_BED(0);
  1489. #endif
  1490. #if ENABLED(FAN_SOFT_PWM)
  1491. #if HAS_FAN0
  1492. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1493. #endif
  1494. #if HAS_FAN1
  1495. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1496. #endif
  1497. #if HAS_FAN2
  1498. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1499. #endif
  1500. #endif
  1501. }
  1502. // SOFT_PWM_SCALE to frequency:
  1503. //
  1504. // 0: 16000000/64/256/128 = 7.6294 Hz
  1505. // 1: / 64 = 15.2588 Hz
  1506. // 2: / 32 = 30.5176 Hz
  1507. // 3: / 16 = 61.0352 Hz
  1508. // 4: / 8 = 122.0703 Hz
  1509. // 5: / 4 = 244.1406 Hz
  1510. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1511. #else // SLOW_PWM_HEATERS
  1512. /**
  1513. * SLOW PWM HEATERS
  1514. *
  1515. * For relay-driven heaters
  1516. */
  1517. #ifndef MIN_STATE_TIME
  1518. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1519. #endif
  1520. // Macros for Slow PWM timer logic
  1521. #define _SLOW_PWM_ROUTINE(NR, src) \
  1522. soft_pwm_ ##NR = src; \
  1523. if (soft_pwm_ ##NR > 0) { \
  1524. if (state_timer_heater_ ##NR == 0) { \
  1525. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1526. state_heater_ ##NR = 1; \
  1527. WRITE_HEATER_ ##NR(1); \
  1528. } \
  1529. } \
  1530. else { \
  1531. if (state_timer_heater_ ##NR == 0) { \
  1532. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1533. state_heater_ ##NR = 0; \
  1534. WRITE_HEATER_ ##NR(0); \
  1535. } \
  1536. }
  1537. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm_amount[n])
  1538. #define PWM_OFF_ROUTINE(NR) \
  1539. if (soft_pwm_ ##NR < slow_pwm_count) { \
  1540. if (state_timer_heater_ ##NR == 0) { \
  1541. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1542. state_heater_ ##NR = 0; \
  1543. WRITE_HEATER_ ##NR (0); \
  1544. } \
  1545. }
  1546. if (slow_pwm_count == 0) {
  1547. SLOW_PWM_ROUTINE(0);
  1548. #if HOTENDS > 1
  1549. SLOW_PWM_ROUTINE(1);
  1550. #if HOTENDS > 2
  1551. SLOW_PWM_ROUTINE(2);
  1552. #if HOTENDS > 3
  1553. SLOW_PWM_ROUTINE(3);
  1554. #if HOTENDS > 4
  1555. SLOW_PWM_ROUTINE(4);
  1556. #endif // HOTENDS > 4
  1557. #endif // HOTENDS > 3
  1558. #endif // HOTENDS > 2
  1559. #endif // HOTENDS > 1
  1560. #if HAS_HEATER_BED
  1561. _SLOW_PWM_ROUTINE(BED, soft_pwm_amount_bed); // BED
  1562. #endif
  1563. } // slow_pwm_count == 0
  1564. PWM_OFF_ROUTINE(0);
  1565. #if HOTENDS > 1
  1566. PWM_OFF_ROUTINE(1);
  1567. #if HOTENDS > 2
  1568. PWM_OFF_ROUTINE(2);
  1569. #if HOTENDS > 3
  1570. PWM_OFF_ROUTINE(3);
  1571. #if HOTENDS > 4
  1572. PWM_OFF_ROUTINE(4);
  1573. #endif // HOTENDS > 4
  1574. #endif // HOTENDS > 3
  1575. #endif // HOTENDS > 2
  1576. #endif // HOTENDS > 1
  1577. #if HAS_HEATER_BED
  1578. PWM_OFF_ROUTINE(BED); // BED
  1579. #endif
  1580. #if ENABLED(FAN_SOFT_PWM)
  1581. if (pwm_count_tmp >= 127) {
  1582. pwm_count_tmp = 0;
  1583. #if HAS_FAN0
  1584. soft_pwm_count_fan[0] = soft_pwm_amount_fan[0] >> 1;
  1585. WRITE_FAN(soft_pwm_count_fan[0] > 0 ? HIGH : LOW);
  1586. #endif
  1587. #if HAS_FAN1
  1588. soft_pwm_count_fan[1] = soft_pwm_amount_fan[1] >> 1;
  1589. WRITE_FAN1(soft_pwm_count_fan[1] > 0 ? HIGH : LOW);
  1590. #endif
  1591. #if HAS_FAN2
  1592. soft_pwm_count_fan[2] = soft_pwm_amount_fan[2] >> 1;
  1593. WRITE_FAN2(soft_pwm_count_fan[2] > 0 ? HIGH : LOW);
  1594. #endif
  1595. }
  1596. #if HAS_FAN0
  1597. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1598. #endif
  1599. #if HAS_FAN1
  1600. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1601. #endif
  1602. #if HAS_FAN2
  1603. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1604. #endif
  1605. #endif // FAN_SOFT_PWM
  1606. // SOFT_PWM_SCALE to frequency:
  1607. //
  1608. // 0: 16000000/64/256/128 = 7.6294 Hz
  1609. // 1: / 64 = 15.2588 Hz
  1610. // 2: / 32 = 30.5176 Hz
  1611. // 3: / 16 = 61.0352 Hz
  1612. // 4: / 8 = 122.0703 Hz
  1613. // 5: / 4 = 244.1406 Hz
  1614. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1615. // increment slow_pwm_count only every 64th pwm_count,
  1616. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1617. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1618. slow_pwm_count++;
  1619. slow_pwm_count &= 0x7F;
  1620. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1621. #if HOTENDS > 1
  1622. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1623. #if HOTENDS > 2
  1624. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1625. #if HOTENDS > 3
  1626. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1627. #if HOTENDS > 4
  1628. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1629. #endif // HOTENDS > 4
  1630. #endif // HOTENDS > 3
  1631. #endif // HOTENDS > 2
  1632. #endif // HOTENDS > 1
  1633. #if HAS_HEATER_BED
  1634. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1635. #endif
  1636. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1637. #endif // SLOW_PWM_HEATERS
  1638. //
  1639. // Update lcd buttons 488 times per second
  1640. //
  1641. static bool do_buttons;
  1642. if ((do_buttons ^= true)) lcd_buttons_update();
  1643. /**
  1644. * One sensor is sampled on every other call of the ISR.
  1645. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1646. *
  1647. * On each Prepare pass, ADC is started for a sensor pin.
  1648. * On the next pass, the ADC value is read and accumulated.
  1649. *
  1650. * This gives each ADC 0.9765ms to charge up.
  1651. */
  1652. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1653. #ifdef MUX5
  1654. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1655. #else
  1656. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1657. #endif
  1658. switch (adc_sensor_state) {
  1659. case SensorsReady: {
  1660. // All sensors have been read. Stay in this state for a few
  1661. // ISRs to save on calls to temp update/checking code below.
  1662. constexpr int extra_loops = MIN_ADC_ISR_LOOPS - (int)SensorsReady;
  1663. static uint8_t delay_count = 0;
  1664. if (extra_loops > 0) {
  1665. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1666. if (--delay_count) // While delaying...
  1667. adc_sensor_state = (ADCSensorState)(int(SensorsReady) - 1); // retain this state (else, next state will be 0)
  1668. break;
  1669. }
  1670. else
  1671. adc_sensor_state = (ADCSensorState)0; // Fall-through to start first sensor now
  1672. }
  1673. #if HAS_TEMP_0
  1674. case PrepareTemp_0:
  1675. START_ADC(TEMP_0_PIN);
  1676. break;
  1677. case MeasureTemp_0:
  1678. raw_temp_value[0] += ADC;
  1679. break;
  1680. #endif
  1681. #if HAS_TEMP_BED
  1682. case PrepareTemp_BED:
  1683. START_ADC(TEMP_BED_PIN);
  1684. break;
  1685. case MeasureTemp_BED:
  1686. raw_temp_bed_value += ADC;
  1687. break;
  1688. #endif
  1689. #if HAS_TEMP_1
  1690. case PrepareTemp_1:
  1691. START_ADC(TEMP_1_PIN);
  1692. break;
  1693. case MeasureTemp_1:
  1694. raw_temp_value[1] += ADC;
  1695. break;
  1696. #endif
  1697. #if HAS_TEMP_2
  1698. case PrepareTemp_2:
  1699. START_ADC(TEMP_2_PIN);
  1700. break;
  1701. case MeasureTemp_2:
  1702. raw_temp_value[2] += ADC;
  1703. break;
  1704. #endif
  1705. #if HAS_TEMP_3
  1706. case PrepareTemp_3:
  1707. START_ADC(TEMP_3_PIN);
  1708. break;
  1709. case MeasureTemp_3:
  1710. raw_temp_value[3] += ADC;
  1711. break;
  1712. #endif
  1713. #if HAS_TEMP_4
  1714. case PrepareTemp_4:
  1715. START_ADC(TEMP_4_PIN);
  1716. break;
  1717. case MeasureTemp_4:
  1718. raw_temp_value[4] += ADC;
  1719. break;
  1720. #endif
  1721. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1722. case Prepare_FILWIDTH:
  1723. START_ADC(FILWIDTH_PIN);
  1724. break;
  1725. case Measure_FILWIDTH:
  1726. if (ADC > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1727. raw_filwidth_value -= (raw_filwidth_value >> 7); // Subtract 1/128th of the raw_filwidth_value
  1728. raw_filwidth_value += ((unsigned long)ADC << 7); // Add new ADC reading, scaled by 128
  1729. }
  1730. break;
  1731. #endif
  1732. case StartupDelay: break;
  1733. } // switch(adc_sensor_state)
  1734. if (!adc_sensor_state && ++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1735. temp_count = 0;
  1736. // Update the raw values if they've been read. Else we could be updating them during reading.
  1737. if (!temp_meas_ready) set_current_temp_raw();
  1738. // Filament Sensor - can be read any time since IIR filtering is used
  1739. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1740. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1741. #endif
  1742. ZERO(raw_temp_value);
  1743. raw_temp_bed_value = 0;
  1744. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1745. int constexpr temp_dir[] = {
  1746. #if ENABLED(HEATER_0_USES_MAX6675)
  1747. 0
  1748. #else
  1749. TEMPDIR(0)
  1750. #endif
  1751. #if HOTENDS > 1
  1752. , TEMPDIR(1)
  1753. #if HOTENDS > 2
  1754. , TEMPDIR(2)
  1755. #if HOTENDS > 3
  1756. , TEMPDIR(3)
  1757. #if HOTENDS > 4
  1758. , TEMPDIR(4)
  1759. #endif // HOTENDS > 4
  1760. #endif // HOTENDS > 3
  1761. #endif // HOTENDS > 2
  1762. #endif // HOTENDS > 1
  1763. };
  1764. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1765. const int16_t tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1766. if (rawtemp > maxttemp_raw[e] * tdir && target_temperature[e] > 0) max_temp_error(e);
  1767. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && target_temperature[e] > 0) {
  1768. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1769. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1770. #endif
  1771. min_temp_error(e);
  1772. }
  1773. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1774. else
  1775. consecutive_low_temperature_error[e] = 0;
  1776. #endif
  1777. }
  1778. #if HAS_TEMP_BED
  1779. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1780. #define GEBED <=
  1781. #else
  1782. #define GEBED >=
  1783. #endif
  1784. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && target_temperature_bed > 0) max_temp_error(-1);
  1785. if (bed_minttemp_raw GEBED current_temperature_bed_raw && target_temperature_bed > 0) min_temp_error(-1);
  1786. #endif
  1787. } // temp_count >= OVERSAMPLENR
  1788. // Go to the next state, up to SensorsReady
  1789. adc_sensor_state = (ADCSensorState)((int(adc_sensor_state) + 1) % int(StartupDelay));
  1790. #if ENABLED(BABYSTEPPING)
  1791. LOOP_XYZ(axis) {
  1792. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1793. if (curTodo > 0) {
  1794. stepper.babystep((AxisEnum)axis,/*fwd*/true);
  1795. babystepsTodo[axis]--; //fewer to do next time
  1796. }
  1797. else if (curTodo < 0) {
  1798. stepper.babystep((AxisEnum)axis,/*fwd*/false);
  1799. babystepsTodo[axis]++; //fewer to do next time
  1800. }
  1801. }
  1802. #endif //BABYSTEPPING
  1803. #if ENABLED(PINS_DEBUGGING)
  1804. extern bool endstop_monitor_flag;
  1805. // run the endstop monitor at 15Hz
  1806. static uint8_t endstop_monitor_count = 16; // offset this check from the others
  1807. if (endstop_monitor_flag) {
  1808. endstop_monitor_count += _BV(1); // 15 Hz
  1809. endstop_monitor_count &= 0x7F;
  1810. if (!endstop_monitor_count) endstop_monitor(); // report changes in endstop status
  1811. }
  1812. #endif
  1813. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1814. extern volatile uint8_t e_hit;
  1815. if (e_hit && ENDSTOPS_ENABLED) {
  1816. endstops.update(); // call endstop update routine
  1817. e_hit--;
  1818. }
  1819. #endif
  1820. cli();
  1821. in_temp_isr = false;
  1822. SBI(TIMSK0, OCIE0B); //re-enable Temperature ISR
  1823. }