My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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