My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

temperature.cpp 57KB

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