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.

mmu2.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if HAS_PRUSA_MMU2
  24. #include "mmu2.h"
  25. #include "../../lcd/menu/menu_mmu2.h"
  26. MMU2 mmu2;
  27. #include "../../gcode/gcode.h"
  28. #include "../../lcd/marlinui.h"
  29. #include "../../libs/buzzer.h"
  30. #include "../../libs/nozzle.h"
  31. #include "../../module/temperature.h"
  32. #include "../../module/planner.h"
  33. #include "../../module/stepper.h"
  34. #include "../../MarlinCore.h"
  35. #if ENABLED(HOST_PROMPT_SUPPORT)
  36. #include "../../feature/host_actions.h"
  37. #endif
  38. #if ENABLED(EXTENSIBLE_UI)
  39. #include "../../lcd/extui/ui_api.h"
  40. #endif
  41. #define DEBUG_OUT ENABLED(MMU2_DEBUG)
  42. #include "../../core/debug_out.h"
  43. #define MMU_TODELAY 100
  44. #define MMU_TIMEOUT 10
  45. #define MMU_CMD_TIMEOUT 45000UL // 45s timeout for mmu commands (except P0)
  46. #define MMU_P0_TIMEOUT 3000UL // Timeout for P0 command: 3seconds
  47. #define MMU2_SEND(S) tx_str(F(S "\n"))
  48. #define MMU2_RECV(S) rx_str(F(S "\n"))
  49. #if ENABLED(MMU_EXTRUDER_SENSOR)
  50. uint8_t mmu_idl_sens = 0;
  51. static bool mmu_loading_flag = false;
  52. #endif
  53. #define MMU_CMD_NONE 0
  54. #define MMU_CMD_T0 0x10 // up to supported filaments
  55. #define MMU_CMD_L0 0x20 // up to supported filaments
  56. #define MMU_CMD_C0 0x30
  57. #define MMU_CMD_U0 0x40
  58. #define MMU_CMD_E0 0x50 // up to supported filaments
  59. #define MMU_CMD_R0 0x60
  60. #define MMU_CMD_F0 0x70 // up to supported filaments
  61. #define MMU_REQUIRED_FW_BUILDNR TERN(MMU2_MODE_12V, 132, 126)
  62. #define MMU2_NO_TOOL 99
  63. #define MMU_BAUD 115200
  64. bool MMU2::_enabled, MMU2::ready, MMU2::mmu_print_saved;
  65. #if HAS_PRUSA_MMU2S
  66. bool MMU2::mmu2s_triggered;
  67. #endif
  68. uint8_t MMU2::cmd, MMU2::cmd_arg, MMU2::last_cmd, MMU2::extruder;
  69. int8_t MMU2::state = 0;
  70. volatile int8_t MMU2::finda = 1;
  71. volatile bool MMU2::finda_runout_valid;
  72. int16_t MMU2::version = -1, MMU2::buildnr = -1;
  73. millis_t MMU2::prev_request, MMU2::prev_P0_request;
  74. char MMU2::rx_buffer[MMU_RX_SIZE], MMU2::tx_buffer[MMU_TX_SIZE];
  75. struct E_Step {
  76. float extrude; //!< extrude distance in mm
  77. feedRate_t feedRate; //!< feed rate in mm/s
  78. };
  79. static constexpr E_Step
  80. ramming_sequence[] PROGMEM = { MMU2_RAMMING_SEQUENCE }
  81. , load_to_nozzle_sequence[] PROGMEM = { MMU2_LOAD_TO_NOZZLE_SEQUENCE }
  82. #if HAS_PRUSA_MMU2S
  83. , can_load_sequence[] PROGMEM = { MMU2_CAN_LOAD_SEQUENCE }
  84. , can_load_increment_sequence[] PROGMEM = { MMU2_CAN_LOAD_INCREMENT_SEQUENCE }
  85. #endif
  86. ;
  87. MMU2::MMU2() {
  88. rx_buffer[0] = '\0';
  89. }
  90. void MMU2::init() {
  91. set_runout_valid(false);
  92. #if PIN_EXISTS(MMU2_RST)
  93. WRITE(MMU2_RST_PIN, HIGH);
  94. SET_OUTPUT(MMU2_RST_PIN);
  95. #endif
  96. MMU2_SERIAL.begin(MMU_BAUD);
  97. extruder = MMU2_NO_TOOL;
  98. safe_delay(10);
  99. reset();
  100. rx_buffer[0] = '\0';
  101. state = -1;
  102. }
  103. void MMU2::reset() {
  104. DEBUG_ECHOLNPGM("MMU <= reset");
  105. #if PIN_EXISTS(MMU2_RST)
  106. WRITE(MMU2_RST_PIN, LOW);
  107. safe_delay(20);
  108. WRITE(MMU2_RST_PIN, HIGH);
  109. #else
  110. MMU2_SEND("X0"); // Send soft reset
  111. #endif
  112. }
  113. uint8_t MMU2::get_current_tool() {
  114. return extruder == MMU2_NO_TOOL ? -1 : extruder;
  115. }
  116. #if EITHER(HAS_PRUSA_MMU2S, MMU_EXTRUDER_SENSOR)
  117. #define FILAMENT_PRESENT() (READ(FIL_RUNOUT1_PIN) != FIL_RUNOUT1_STATE)
  118. #endif
  119. void mmu2_attn_buzz(const bool two=false) {
  120. BUZZ(200, 404);
  121. if (two) { BUZZ(10, 0); BUZZ(200, 404); }
  122. }
  123. void MMU2::mmu_loop() {
  124. switch (state) {
  125. case 0: break;
  126. case -1:
  127. if (rx_start()) {
  128. prev_P0_request = millis(); // Initialize finda sensor timeout
  129. DEBUG_ECHOLNPGM("MMU => 'start'");
  130. DEBUG_ECHOLNPGM("MMU <= 'S1'");
  131. MMU2_SEND("S1"); // Read Version
  132. state = -2;
  133. }
  134. else if (millis() > 30000) { // 30sec after reset disable MMU
  135. SERIAL_ECHOLNPGM("MMU not responding - DISABLED");
  136. state = 0;
  137. }
  138. break;
  139. case -2:
  140. if (rx_ok()) {
  141. sscanf(rx_buffer, "%huok\n", &version);
  142. DEBUG_ECHOLNPGM("MMU => ", version, "\nMMU <= 'S2'");
  143. MMU2_SEND("S2"); // Read Build Number
  144. state = -3;
  145. }
  146. break;
  147. case -3:
  148. if (rx_ok()) {
  149. sscanf(rx_buffer, "%huok\n", &buildnr);
  150. DEBUG_ECHOLNPGM("MMU => ", buildnr);
  151. check_version();
  152. #if ENABLED(MMU2_MODE_12V)
  153. DEBUG_ECHOLNPGM("MMU <= 'M1'");
  154. MMU2_SEND("M1"); // Stealth Mode
  155. state = -5;
  156. #else
  157. DEBUG_ECHOLNPGM("MMU <= 'P0'");
  158. MMU2_SEND("P0"); // Read FINDA
  159. state = -4;
  160. #endif
  161. }
  162. break;
  163. #if ENABLED(MMU2_MODE_12V)
  164. case -5:
  165. // response to M1
  166. if (rx_ok()) {
  167. DEBUG_ECHOLNPGM("MMU => ok");
  168. DEBUG_ECHOLNPGM("MMU <= 'P0'");
  169. MMU2_SEND("P0"); // Read FINDA
  170. state = -4;
  171. }
  172. break;
  173. #endif
  174. case -4:
  175. if (rx_ok()) {
  176. sscanf(rx_buffer, "%hhuok\n", &finda);
  177. DEBUG_ECHOLNPGM("MMU => ", finda, "\nMMU - ENABLED");
  178. _enabled = true;
  179. state = 1;
  180. TERN_(HAS_PRUSA_MMU2S, mmu2s_triggered = false);
  181. }
  182. break;
  183. case 1:
  184. if (cmd) {
  185. if (WITHIN(cmd, MMU_CMD_T0, MMU_CMD_T0 + EXTRUDERS - 1)) {
  186. // tool change
  187. const int filament = cmd - MMU_CMD_T0;
  188. DEBUG_ECHOLNPGM("MMU <= T", filament);
  189. tx_printf(F("T%d\n"), filament);
  190. TERN_(MMU_EXTRUDER_SENSOR, mmu_idl_sens = 1); // enable idler sensor, if any
  191. state = 3; // wait for response
  192. }
  193. else if (WITHIN(cmd, MMU_CMD_L0, MMU_CMD_L0 + EXTRUDERS - 1)) {
  194. // load
  195. const int filament = cmd - MMU_CMD_L0;
  196. DEBUG_ECHOLNPGM("MMU <= L", filament);
  197. tx_printf(F("L%d\n"), filament);
  198. state = 3; // wait for response
  199. }
  200. else if (cmd == MMU_CMD_C0) {
  201. // continue loading
  202. DEBUG_ECHOLNPGM("MMU <= 'C0'");
  203. MMU2_SEND("C0");
  204. state = 3; // wait for response
  205. }
  206. else if (cmd == MMU_CMD_U0) {
  207. // unload current
  208. DEBUG_ECHOLNPGM("MMU <= 'U0'");
  209. MMU2_SEND("U0");
  210. state = 3; // wait for response
  211. }
  212. else if (WITHIN(cmd, MMU_CMD_E0, MMU_CMD_E0 + EXTRUDERS - 1)) {
  213. // eject filament
  214. const int filament = cmd - MMU_CMD_E0;
  215. DEBUG_ECHOLNPGM("MMU <= E", filament);
  216. tx_printf(F("E%d\n"), filament);
  217. state = 3; // wait for response
  218. }
  219. else if (cmd == MMU_CMD_R0) {
  220. // recover after eject
  221. DEBUG_ECHOLNPGM("MMU <= 'R0'");
  222. MMU2_SEND("R0");
  223. state = 3; // wait for response
  224. }
  225. else if (WITHIN(cmd, MMU_CMD_F0, MMU_CMD_F0 + EXTRUDERS - 1)) {
  226. // filament type
  227. const int filament = cmd - MMU_CMD_F0;
  228. DEBUG_ECHOLNPGM("MMU <= F", filament, " ", cmd_arg);
  229. tx_printf(F("F%d %d\n"), filament, cmd_arg);
  230. state = 3; // wait for response
  231. }
  232. last_cmd = cmd;
  233. cmd = MMU_CMD_NONE;
  234. }
  235. else if (ELAPSED(millis(), prev_P0_request + 300)) {
  236. MMU2_SEND("P0"); // Read FINDA
  237. state = 2; // wait for response
  238. }
  239. TERN_(HAS_PRUSA_MMU2S, check_filament());
  240. break;
  241. case 2: // response to command P0
  242. if (rx_ok()) {
  243. sscanf(rx_buffer, "%hhuok\n", &finda);
  244. // This is super annoying. Only activate if necessary
  245. // if (finda_runout_valid) DEBUG_ECHOLNPAIR_F("MMU <= 'P0'\nMMU => ", finda, 6);
  246. if (!finda && finda_runout_valid) filament_runout();
  247. if (cmd == MMU_CMD_NONE) ready = true;
  248. state = 1;
  249. }
  250. else if (ELAPSED(millis(), prev_request + MMU_P0_TIMEOUT)) // Resend request after timeout (3s)
  251. state = 1;
  252. TERN_(HAS_PRUSA_MMU2S, check_filament());
  253. break;
  254. case 3: // response to mmu commands
  255. #if ENABLED(MMU_EXTRUDER_SENSOR)
  256. if (mmu_idl_sens) {
  257. if (FILAMENT_PRESENT() && mmu_loading_flag) {
  258. DEBUG_ECHOLNPGM("MMU <= 'A'");
  259. MMU2_SEND("A"); // send 'abort' request
  260. mmu_idl_sens = 0;
  261. DEBUG_ECHOLNPGM("MMU IDLER_SENSOR = 0 - ABORT");
  262. }
  263. }
  264. #endif
  265. if (rx_ok()) {
  266. #if HAS_PRUSA_MMU2S
  267. // Respond to C0 MMU command in MMU2S model
  268. const bool keep_trying = !mmu2s_triggered && last_cmd == MMU_CMD_C0;
  269. if (keep_trying) {
  270. // MMU ok received but filament sensor not triggered, retrying...
  271. DEBUG_ECHOLNPGM("MMU => 'ok' (no filament in gears)");
  272. DEBUG_ECHOLNPGM("MMU <= 'C0' (keep trying)");
  273. MMU2_SEND("C0");
  274. }
  275. #else
  276. constexpr bool keep_trying = false;
  277. #endif
  278. if (!keep_trying) {
  279. DEBUG_ECHOLNPGM("MMU => 'ok'");
  280. ready = true;
  281. state = 1;
  282. last_cmd = MMU_CMD_NONE;
  283. }
  284. }
  285. else if (ELAPSED(millis(), prev_request + MMU_CMD_TIMEOUT)) {
  286. // resend request after timeout
  287. if (last_cmd) {
  288. DEBUG_ECHOLNPGM("MMU retry");
  289. cmd = last_cmd;
  290. last_cmd = MMU_CMD_NONE;
  291. }
  292. state = 1;
  293. }
  294. TERN_(HAS_PRUSA_MMU2S, check_filament());
  295. break;
  296. }
  297. }
  298. /**
  299. * Check if MMU was started
  300. */
  301. bool MMU2::rx_start() {
  302. // check for start message
  303. return MMU2_RECV("start");
  304. }
  305. /**
  306. * Check if the data received ends with the given string.
  307. */
  308. bool MMU2::rx_str(FSTR_P fstr) {
  309. PGM_P pstr = FTOP(fstr);
  310. uint8_t i = strlen(rx_buffer);
  311. while (MMU2_SERIAL.available()) {
  312. rx_buffer[i++] = MMU2_SERIAL.read();
  313. if (i == sizeof(rx_buffer) - 1) {
  314. DEBUG_ECHOLNPGM("rx buffer overrun");
  315. break;
  316. }
  317. }
  318. rx_buffer[i] = '\0';
  319. uint8_t len = strlen_P(pstr);
  320. if (i < len) return false;
  321. pstr += len;
  322. while (len--) {
  323. char c0 = pgm_read_byte(pstr--), c1 = rx_buffer[i--];
  324. if (c0 == c1) continue;
  325. if (c0 == '\r' && c1 == '\n') continue; // match cr as lf
  326. if (c0 == '\n' && c1 == '\r') continue; // match lf as cr
  327. return false;
  328. }
  329. return true;
  330. }
  331. /**
  332. * Transfer data to MMU, no argument
  333. */
  334. void MMU2::tx_str(FSTR_P fstr) {
  335. clear_rx_buffer();
  336. PGM_P pstr = FTOP(fstr);
  337. while (const char c = pgm_read_byte(pstr)) { MMU2_SERIAL.write(c); pstr++; }
  338. prev_request = millis();
  339. }
  340. /**
  341. * Transfer data to MMU, single argument
  342. */
  343. void MMU2::tx_printf(FSTR_P format, int argument = -1) {
  344. clear_rx_buffer();
  345. const uint8_t len = sprintf_P(tx_buffer, FTOP(format), argument);
  346. LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
  347. prev_request = millis();
  348. }
  349. /**
  350. * Transfer data to MMU, two arguments
  351. */
  352. void MMU2::tx_printf(FSTR_P format, int argument1, int argument2) {
  353. clear_rx_buffer();
  354. const uint8_t len = sprintf_P(tx_buffer, FTOP(format), argument1, argument2);
  355. LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
  356. prev_request = millis();
  357. }
  358. /**
  359. * Empty the rx buffer
  360. */
  361. void MMU2::clear_rx_buffer() {
  362. while (MMU2_SERIAL.available()) MMU2_SERIAL.read();
  363. rx_buffer[0] = '\0';
  364. }
  365. /**
  366. * Check if we received 'ok' from MMU
  367. */
  368. bool MMU2::rx_ok() {
  369. if (MMU2_RECV("ok")) {
  370. prev_P0_request = millis();
  371. return true;
  372. }
  373. return false;
  374. }
  375. /**
  376. * Check if MMU has compatible firmware
  377. */
  378. void MMU2::check_version() {
  379. if (buildnr < MMU_REQUIRED_FW_BUILDNR) {
  380. SERIAL_ERROR_MSG("Invalid MMU2 firmware. Version >= " STRINGIFY(MMU_REQUIRED_FW_BUILDNR) " required.");
  381. kill(GET_TEXT_F(MSG_KILL_MMU2_FIRMWARE));
  382. }
  383. }
  384. static void mmu2_not_responding() {
  385. LCD_MESSAGE(MSG_MMU2_NOT_RESPONDING);
  386. BUZZ(100, 659);
  387. BUZZ(200, 698);
  388. BUZZ(100, 659);
  389. BUZZ(300, 440);
  390. BUZZ(100, 659);
  391. }
  392. #if HAS_PRUSA_MMU2S
  393. bool MMU2::load_to_gears() {
  394. command(MMU_CMD_C0);
  395. manage_response(true, true);
  396. LOOP_L_N(i, MMU2_C0_RETRY) { // Keep loading until filament reaches gears
  397. if (mmu2s_triggered) break;
  398. command(MMU_CMD_C0);
  399. manage_response(true, true);
  400. check_filament();
  401. }
  402. const bool success = mmu2s_triggered && can_load();
  403. if (!success) mmu2_not_responding();
  404. return success;
  405. }
  406. /**
  407. * Handle tool change
  408. */
  409. void MMU2::tool_change(const uint8_t index) {
  410. if (!_enabled) return;
  411. set_runout_valid(false);
  412. if (index != extruder) {
  413. stepper.disable_extruder();
  414. ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
  415. command(MMU_CMD_T0 + index);
  416. manage_response(true, true);
  417. if (load_to_gears()) {
  418. extruder = index; // filament change is finished
  419. active_extruder = 0;
  420. stepper.enable_extruder();
  421. SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, extruder);
  422. }
  423. ui.reset_status();
  424. }
  425. set_runout_valid(true);
  426. }
  427. /**
  428. * Handle special T?/Tx/Tc commands
  429. *
  430. * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
  431. * Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load.
  432. * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
  433. */
  434. void MMU2::tool_change(const char *special) {
  435. if (!_enabled) return;
  436. set_runout_valid(false);
  437. switch (*special) {
  438. case '?': {
  439. #if ENABLED(MMU2_MENUS)
  440. const uint8_t index = mmu2_choose_filament();
  441. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  442. load_filament_to_nozzle(index);
  443. #else
  444. ERR_BUZZ();
  445. #endif
  446. } break;
  447. case 'x': {
  448. #if ENABLED(MMU2_MENUS)
  449. planner.synchronize();
  450. const uint8_t index = mmu2_choose_filament();
  451. stepper.disable_extruder();
  452. command(MMU_CMD_T0 + index);
  453. manage_response(true, true);
  454. if (load_to_gears()) {
  455. mmu_loop();
  456. stepper.enable_extruder();
  457. extruder = index;
  458. active_extruder = 0;
  459. }
  460. #else
  461. ERR_BUZZ();
  462. #endif
  463. } break;
  464. case 'c': {
  465. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  466. load_to_nozzle();
  467. } break;
  468. }
  469. set_runout_valid(true);
  470. }
  471. #elif ENABLED(MMU_EXTRUDER_SENSOR)
  472. /**
  473. * Handle tool change
  474. */
  475. void MMU2::tool_change(const uint8_t index) {
  476. if (!_enabled) return;
  477. set_runout_valid(false);
  478. if (index != extruder) {
  479. stepper.disable_extruder();
  480. if (FILAMENT_PRESENT()) {
  481. DEBUG_ECHOLNPGM("Unloading\n");
  482. mmu_loading_flag = false;
  483. command(MMU_CMD_U0);
  484. manage_response(true, true);
  485. }
  486. ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
  487. mmu_loading_flag = true;
  488. command(MMU_CMD_T0 + index);
  489. manage_response(true, true);
  490. mmu_continue_loading();
  491. //command(MMU_CMD_C0);
  492. extruder = index;
  493. active_extruder = 0;
  494. stepper.enable_extruder();
  495. SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, extruder);
  496. ui.reset_status();
  497. }
  498. set_runout_valid(true);
  499. }
  500. /**
  501. * Handle special T?/Tx/Tc commands
  502. *
  503. * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
  504. * Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load.
  505. * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
  506. */
  507. void MMU2::tool_change(const char *special) {
  508. if (!_enabled) return;
  509. set_runout_valid(false);
  510. switch (*special) {
  511. case '?': {
  512. DEBUG_ECHOLNPGM("case ?\n");
  513. #if ENABLED(MMU2_MENUS)
  514. uint8_t index = mmu2_choose_filament();
  515. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  516. load_filament_to_nozzle(index);
  517. #else
  518. ERR_BUZZ();
  519. #endif
  520. } break;
  521. case 'x': {
  522. DEBUG_ECHOLNPGM("case x\n");
  523. #if ENABLED(MMU2_MENUS)
  524. planner.synchronize();
  525. uint8_t index = mmu2_choose_filament();
  526. stepper.disable_extruder();
  527. command(MMU_CMD_T0 + index);
  528. manage_response(true, true);
  529. mmu_continue_loading();
  530. command(MMU_CMD_C0);
  531. mmu_loop();
  532. stepper.enable_extruder();
  533. extruder = index;
  534. active_extruder = 0;
  535. #else
  536. ERR_BUZZ();
  537. #endif
  538. } break;
  539. case 'c': {
  540. DEBUG_ECHOLNPGM("case c\n");
  541. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  542. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
  543. } break;
  544. }
  545. set_runout_valid(true);
  546. }
  547. void MMU2::mmu_continue_loading() {
  548. // Try to load the filament a limited number of times
  549. bool fil_present = 0;
  550. for (uint8_t i = 0; i < MMU_LOADING_ATTEMPTS_NR; i++) {
  551. DEBUG_ECHOLNPGM("Load attempt #", i + 1);
  552. // Done as soon as filament is present
  553. fil_present = FILAMENT_PRESENT();
  554. if (fil_present) break;
  555. // Attempt to load the filament, 1mm at a time, for 3s
  556. command(MMU_CMD_C0);
  557. stepper.enable_extruder();
  558. const millis_t expire_ms = millis() + 3000;
  559. do {
  560. current_position.e += 1;
  561. line_to_current_position(MMU_LOAD_FEEDRATE);
  562. planner.synchronize();
  563. // When (T0 rx->ok) load is ready, but in fact it did not load
  564. // successfully or an overload created pressure in the extruder.
  565. // Send (C0) to load more and move E_AXIS a little to release pressure.
  566. if ((fil_present = FILAMENT_PRESENT())) MMU2_SEND("A");
  567. } while (!fil_present && PENDING(millis(), expire_ms));
  568. stepper.disable_extruder();
  569. manage_response(true, true);
  570. }
  571. // Was the filament still missing in the last check?
  572. if (!fil_present) {
  573. DEBUG_ECHOLNPGM("Filament never reached sensor, runout");
  574. filament_runout();
  575. }
  576. mmu_idl_sens = 0;
  577. }
  578. #else // !HAS_PRUSA_MMU2S && !MMU_EXTRUDER_SENSOR
  579. /**
  580. * Handle tool change
  581. */
  582. void MMU2::tool_change(const uint8_t index) {
  583. if (!_enabled) return;
  584. set_runout_valid(false);
  585. if (index != extruder) {
  586. stepper.disable_extruder();
  587. ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
  588. command(MMU_CMD_T0 + index);
  589. manage_response(true, true);
  590. command(MMU_CMD_C0);
  591. extruder = index; // Filament change is finished
  592. active_extruder = 0;
  593. stepper.enable_extruder();
  594. SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, extruder);
  595. ui.reset_status();
  596. }
  597. set_runout_valid(true);
  598. }
  599. /**
  600. * Handle special T?/Tx/Tc commands
  601. *
  602. * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
  603. * Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load.
  604. * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
  605. */
  606. void MMU2::tool_change(const char *special) {
  607. if (!_enabled) return;
  608. set_runout_valid(false);
  609. switch (*special) {
  610. case '?': {
  611. DEBUG_ECHOLNPGM("case ?\n");
  612. #if ENABLED(MMU2_MENUS)
  613. uint8_t index = mmu2_choose_filament();
  614. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  615. load_filament_to_nozzle(index);
  616. #else
  617. ERR_BUZZ();
  618. #endif
  619. } break;
  620. case 'x': {
  621. DEBUG_ECHOLNPGM("case x\n");
  622. #if ENABLED(MMU2_MENUS)
  623. planner.synchronize();
  624. uint8_t index = mmu2_choose_filament();
  625. stepper.disable_extruder();
  626. command(MMU_CMD_T0 + index);
  627. manage_response(true, true);
  628. command(MMU_CMD_C0);
  629. mmu_loop();
  630. stepper.enable_extruder();
  631. extruder = index;
  632. active_extruder = 0;
  633. #else
  634. ERR_BUZZ();
  635. #endif
  636. } break;
  637. case 'c': {
  638. DEBUG_ECHOLNPGM("case c\n");
  639. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  640. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
  641. } break;
  642. }
  643. set_runout_valid(true);
  644. }
  645. #endif // HAS_PRUSA_MMU2S
  646. /**
  647. * Set next command
  648. */
  649. void MMU2::command(const uint8_t mmu_cmd) {
  650. if (!_enabled) return;
  651. cmd = mmu_cmd;
  652. ready = false;
  653. }
  654. /**
  655. * Wait for response from MMU
  656. */
  657. bool MMU2::get_response() {
  658. while (cmd != MMU_CMD_NONE) idle();
  659. while (!ready) {
  660. idle();
  661. if (state != 3) break;
  662. }
  663. const bool ret = ready;
  664. ready = false;
  665. return ret;
  666. }
  667. /**
  668. * Wait for response and deal with timeout if necessary
  669. */
  670. void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
  671. constexpr xyz_pos_t park_point = NOZZLE_PARK_POINT;
  672. bool response = false;
  673. mmu_print_saved = false;
  674. xyz_pos_t resume_position;
  675. celsius_t resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
  676. KEEPALIVE_STATE(PAUSED_FOR_USER);
  677. while (!response) {
  678. response = get_response(); // wait for "ok" from mmu
  679. if (!response) { // No "ok" was received in reserved time frame, user will fix the issue on mmu unit
  680. if (!mmu_print_saved) { // First occurrence. Save current position, park print head, disable nozzle heater.
  681. planner.synchronize();
  682. mmu_print_saved = true;
  683. SERIAL_ECHOLNPGM("MMU not responding");
  684. resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
  685. resume_position = current_position;
  686. if (move_axes && all_axes_homed())
  687. nozzle.park(0, park_point /*= NOZZLE_PARK_POINT*/);
  688. if (turn_off_nozzle) thermalManager.setTargetHotend(0, active_extruder);
  689. mmu2_not_responding();
  690. }
  691. }
  692. else if (mmu_print_saved) {
  693. SERIAL_ECHOLNPGM("MMU starts responding\n");
  694. if (turn_off_nozzle && resume_hotend_temp) {
  695. thermalManager.setTargetHotend(resume_hotend_temp, active_extruder);
  696. LCD_MESSAGE(MSG_HEATING);
  697. ERR_BUZZ();
  698. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(1000);
  699. }
  700. LCD_MESSAGE(MSG_MMU2_RESUMING);
  701. mmu2_attn_buzz(true);
  702. #pragma GCC diagnostic push
  703. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  704. if (move_axes && all_axes_homed()) {
  705. // Move XY to starting position, then Z
  706. do_blocking_move_to_xy(resume_position, feedRate_t(NOZZLE_PARK_XY_FEEDRATE));
  707. // Move Z_AXIS to saved position
  708. do_blocking_move_to_z(resume_position.z, feedRate_t(NOZZLE_PARK_Z_FEEDRATE));
  709. }
  710. #pragma GCC diagnostic pop
  711. }
  712. }
  713. }
  714. void MMU2::set_filament_type(const uint8_t index, const uint8_t filamentType) {
  715. if (!_enabled) return;
  716. cmd_arg = filamentType;
  717. command(MMU_CMD_F0 + index);
  718. manage_response(true, true);
  719. }
  720. void MMU2::filament_runout() {
  721. queue.inject(F(MMU2_FILAMENT_RUNOUT_SCRIPT));
  722. planner.synchronize();
  723. }
  724. #if HAS_PRUSA_MMU2S
  725. void MMU2::check_filament() {
  726. const bool present = FILAMENT_PRESENT();
  727. if (cmd == MMU_CMD_NONE && last_cmd == MMU_CMD_C0) {
  728. if (present && !mmu2s_triggered) {
  729. DEBUG_ECHOLNPGM("MMU <= 'A'");
  730. MMU2_SEND("A");
  731. }
  732. // Slowly spin the extruder during C0
  733. else {
  734. while (planner.movesplanned() < 3) {
  735. current_position.e += 0.25;
  736. line_to_current_position(MMM_TO_MMS(120));
  737. }
  738. }
  739. }
  740. mmu2s_triggered = present;
  741. }
  742. bool MMU2::can_load() {
  743. execute_extruder_sequence((const E_Step *)can_load_sequence, COUNT(can_load_sequence));
  744. int filament_detected_count = 0;
  745. const int steps = (MMU2_CAN_LOAD_RETRACT) / (MMU2_CAN_LOAD_INCREMENT);
  746. DEBUG_ECHOLNPGM("MMU can_load:");
  747. LOOP_L_N(i, steps) {
  748. execute_extruder_sequence((const E_Step *)can_load_increment_sequence, COUNT(can_load_increment_sequence));
  749. check_filament(); // Don't trust the idle function
  750. DEBUG_CHAR(mmu2s_triggered ? 'O' : 'o');
  751. if (mmu2s_triggered) ++filament_detected_count;
  752. }
  753. if (filament_detected_count <= steps - (MMU2_CAN_LOAD_DEVIATION) / (MMU2_CAN_LOAD_INCREMENT)) {
  754. DEBUG_ECHOLNPGM(" failed.");
  755. return false;
  756. }
  757. DEBUG_ECHOLNPGM(" succeeded.");
  758. return true;
  759. }
  760. #endif
  761. // Load filament into MMU2
  762. void MMU2::load_filament(const uint8_t index) {
  763. if (!_enabled) return;
  764. command(MMU_CMD_L0 + index);
  765. manage_response(false, false);
  766. mmu2_attn_buzz();
  767. }
  768. /**
  769. * Switch material and load to nozzle
  770. */
  771. bool MMU2::load_filament_to_nozzle(const uint8_t index) {
  772. if (!_enabled) return false;
  773. if (thermalManager.tooColdToExtrude(active_extruder)) {
  774. mmu2_attn_buzz();
  775. LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
  776. return false;
  777. }
  778. stepper.disable_extruder();
  779. command(MMU_CMD_T0 + index);
  780. manage_response(true, true);
  781. const bool success = load_to_gears();
  782. if (success) {
  783. mmu_loop();
  784. extruder = index;
  785. active_extruder = 0;
  786. load_to_nozzle();
  787. mmu2_attn_buzz();
  788. }
  789. return success;
  790. }
  791. /**
  792. * Load filament to nozzle of multimaterial printer
  793. *
  794. * This function is used only after T? (user select filament) and M600 (change filament).
  795. * It is not used after T0 .. T4 command (select filament), in such case, G-code is responsible for loading
  796. * filament to nozzle.
  797. */
  798. void MMU2::load_to_nozzle() {
  799. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
  800. }
  801. bool MMU2::eject_filament(const uint8_t index, const bool recover) {
  802. if (!_enabled) return false;
  803. if (thermalManager.tooColdToExtrude(active_extruder)) {
  804. mmu2_attn_buzz();
  805. LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
  806. return false;
  807. }
  808. LCD_MESSAGE(MSG_MMU2_EJECTING_FILAMENT);
  809. stepper.enable_extruder();
  810. current_position.e -= MMU2_FILAMENTCHANGE_EJECT_FEED;
  811. line_to_current_position(MMM_TO_MMS(2500));
  812. planner.synchronize();
  813. command(MMU_CMD_E0 + index);
  814. manage_response(false, false);
  815. if (recover) {
  816. LCD_MESSAGE(MSG_MMU2_EJECT_RECOVER);
  817. mmu2_attn_buzz();
  818. TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_do(PROMPT_USER_CONTINUE, F("MMU2 Eject Recover"), FPSTR(CONTINUE_STR)));
  819. TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired(F("MMU2 Eject Recover")));
  820. TERN_(HAS_RESUME_CONTINUE, wait_for_user_response());
  821. mmu2_attn_buzz(true);
  822. command(MMU_CMD_R0);
  823. manage_response(false, false);
  824. }
  825. ui.reset_status();
  826. // no active tool
  827. extruder = MMU2_NO_TOOL;
  828. set_runout_valid(false);
  829. mmu2_attn_buzz();
  830. stepper.disable_extruder();
  831. return true;
  832. }
  833. /**
  834. * Unload from hotend and retract to MMU
  835. */
  836. bool MMU2::unload() {
  837. if (!_enabled) return false;
  838. if (thermalManager.tooColdToExtrude(active_extruder)) {
  839. mmu2_attn_buzz();
  840. LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
  841. return false;
  842. }
  843. // Unload sequence to optimize shape of the tip of the unloaded filament
  844. execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step));
  845. command(MMU_CMD_U0);
  846. manage_response(false, true);
  847. mmu2_attn_buzz();
  848. // no active tool
  849. extruder = MMU2_NO_TOOL;
  850. set_runout_valid(false);
  851. return true;
  852. }
  853. void MMU2::execute_extruder_sequence(const E_Step * sequence, int steps) {
  854. planner.synchronize();
  855. stepper.enable_extruder();
  856. const E_Step* step = sequence;
  857. LOOP_L_N(i, steps) {
  858. const float es = pgm_read_float(&(step->extrude));
  859. const feedRate_t fr_mm_m = pgm_read_float(&(step->feedRate));
  860. DEBUG_ECHO_MSG("E step ", es, "/", fr_mm_m);
  861. current_position.e += es;
  862. line_to_current_position(MMM_TO_MMS(fr_mm_m));
  863. planner.synchronize();
  864. step++;
  865. }
  866. stepper.disable_extruder();
  867. }
  868. #endif // HAS_PRUSA_MMU2