My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

temperature.cpp 60KB

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