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

temperature.cpp 56KB

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