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 56KB

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