My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 69KB

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