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

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