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

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