My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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