My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

temperature.cpp 67KB

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