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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(PRUSA_MMU2)
  24. #include "mmu2.h"
  25. #include "../../lcd/menu/menu_mmu2.h"
  26. MMU2 mmu2;
  27. #include "../../gcode/gcode.h"
  28. #include "../../lcd/ultralcd.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/indirection.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 60000ul // 5min timeout for mmu commands (except P0)
  46. #define MMU_P0_TIMEOUT 3000ul // Timeout for P0 command: 3seconds
  47. #define MMU_CMD_NONE 0
  48. #define MMU_CMD_T0 0x10
  49. #define MMU_CMD_T1 0x11
  50. #define MMU_CMD_T2 0x12
  51. #define MMU_CMD_T3 0x13
  52. #define MMU_CMD_T4 0x14
  53. #define MMU_CMD_L0 0x20
  54. #define MMU_CMD_L1 0x21
  55. #define MMU_CMD_L2 0x22
  56. #define MMU_CMD_L3 0x23
  57. #define MMU_CMD_L4 0x24
  58. #define MMU_CMD_C0 0x30
  59. #define MMU_CMD_U0 0x40
  60. #define MMU_CMD_E0 0x50
  61. #define MMU_CMD_E1 0x51
  62. #define MMU_CMD_E2 0x52
  63. #define MMU_CMD_E3 0x53
  64. #define MMU_CMD_E4 0x54
  65. #define MMU_CMD_R0 0x60
  66. #define MMU_CMD_F0 0x70
  67. #define MMU_CMD_F1 0x71
  68. #define MMU_CMD_F2 0x72
  69. #define MMU_CMD_F3 0x73
  70. #define MMU_CMD_F4 0x74
  71. #if ENABLED(MMU2_MODE_12V)
  72. #define MMU_REQUIRED_FW_BUILDNR 132
  73. #else
  74. #define MMU_REQUIRED_FW_BUILDNR 126
  75. #endif
  76. #define MMU2_NO_TOOL 99
  77. #define MMU_BAUD 115200
  78. #define mmuSerial MMU2_SERIAL
  79. bool MMU2::enabled, MMU2::ready, MMU2::mmu_print_saved;
  80. uint8_t MMU2::cmd, MMU2::cmd_arg, MMU2::last_cmd, MMU2::extruder;
  81. int8_t MMU2::state = 0;
  82. volatile int8_t MMU2::finda = 1;
  83. volatile bool MMU2::finda_runout_valid;
  84. int16_t MMU2::version = -1, MMU2::buildnr = -1;
  85. millis_t MMU2::last_request, MMU2::next_P0_request;
  86. char MMU2::rx_buffer[MMU_RX_SIZE], MMU2::tx_buffer[MMU_TX_SIZE];
  87. #if HAS_LCD_MENU && ENABLED(MMU2_MENUS)
  88. struct E_Step {
  89. float extrude; //!< extrude distance in mm
  90. feedRate_t feedRate; //!< feed rate in mm/s
  91. };
  92. static constexpr E_Step ramming_sequence[] PROGMEM = { MMU2_RAMMING_SEQUENCE };
  93. static constexpr E_Step load_to_nozzle_sequence[] PROGMEM = { MMU2_LOAD_TO_NOZZLE_SEQUENCE };
  94. #endif // MMU2_MENUS
  95. MMU2::MMU2() {
  96. rx_buffer[0] = '\0';
  97. }
  98. void MMU2::init() {
  99. set_runout_valid(false);
  100. #if PIN_EXISTS(MMU2_RST)
  101. // TODO use macros for this
  102. WRITE(MMU2_RST_PIN, HIGH);
  103. SET_OUTPUT(MMU2_RST_PIN);
  104. #endif
  105. mmuSerial.begin(MMU_BAUD);
  106. extruder = MMU2_NO_TOOL;
  107. safe_delay(10);
  108. reset();
  109. rx_buffer[0] = '\0';
  110. state = -1;
  111. }
  112. void MMU2::reset() {
  113. DEBUG_ECHOLNPGM("MMU <= reset");
  114. #if PIN_EXISTS(MMU2_RST)
  115. WRITE(MMU2_RST_PIN, LOW);
  116. safe_delay(20);
  117. WRITE(MMU2_RST_PIN, HIGH);
  118. #else
  119. tx_str_P(PSTR("X0\n")); // Send soft reset
  120. #endif
  121. }
  122. uint8_t MMU2::get_current_tool() {
  123. return extruder == MMU2_NO_TOOL ? -1 : extruder;
  124. }
  125. void MMU2::mmu_loop() {
  126. switch (state) {
  127. case 0: break;
  128. case -1:
  129. if (rx_start()) {
  130. DEBUG_ECHOLNPGM("MMU => 'start'");
  131. DEBUG_ECHOLNPGM("MMU <= 'S1'");
  132. // send "read version" request
  133. tx_str_P(PSTR("S1\n"));
  134. state = -2;
  135. }
  136. else if (millis() > 3000000) {
  137. SERIAL_ECHOLNPGM("MMU not responding - DISABLED");
  138. state = 0;
  139. }
  140. break;
  141. case -2:
  142. if (rx_ok()) {
  143. sscanf(rx_buffer, "%uok\n", &version);
  144. DEBUG_ECHOLNPAIR("MMU => ", version, "\nMMU <= 'S2'");
  145. tx_str_P(PSTR("S2\n")); // read build number
  146. state = -3;
  147. }
  148. break;
  149. case -3:
  150. if (rx_ok()) {
  151. sscanf(rx_buffer, "%uok\n", &buildnr);
  152. DEBUG_ECHOLNPAIR("MMU => ", buildnr);
  153. check_version();
  154. #if ENABLED(MMU2_MODE_12V)
  155. DEBUG_ECHOLNPGM("MMU <= 'M1'");
  156. tx_str_P(PSTR("M1\n")); // switch to stealth mode
  157. state = -5;
  158. #else
  159. DEBUG_ECHOLNPGM("MMU <= 'P0'");
  160. tx_str_P(PSTR("P0\n")); // read finda
  161. state = -4;
  162. #endif
  163. }
  164. break;
  165. #if ENABLED(MMU2_MODE_12V)
  166. case -5:
  167. // response to M1
  168. if (rx_ok()) {
  169. DEBUG_ECHOLNPGM("MMU => ok");
  170. DEBUG_ECHOLNPGM("MMU <= 'P0'");
  171. tx_str_P(PSTR("P0\n")); // read finda
  172. state = -4;
  173. }
  174. break;
  175. #endif
  176. case -4:
  177. if (rx_ok()) {
  178. sscanf(rx_buffer, "%hhuok\n", &finda);
  179. DEBUG_ECHOLNPAIR("MMU => ", finda, "\nMMU - ENABLED");
  180. enabled = true;
  181. state = 1;
  182. }
  183. break;
  184. case 1:
  185. if (cmd) {
  186. if (WITHIN(cmd, MMU_CMD_T0, MMU_CMD_T4)) {
  187. // tool change
  188. int filament = cmd - MMU_CMD_T0;
  189. DEBUG_ECHOLNPAIR("MMU <= T", filament);
  190. tx_printf_P(PSTR("T%d\n"), filament);
  191. state = 3; // wait for response
  192. }
  193. else if (WITHIN(cmd, MMU_CMD_L0, MMU_CMD_L4)) {
  194. // load
  195. int filament = cmd - MMU_CMD_L0;
  196. DEBUG_ECHOLNPAIR("MMU <= L", filament);
  197. tx_printf_P(PSTR("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. tx_str_P(PSTR("C0\n"));
  204. state = 3; // wait for response
  205. }
  206. else if (cmd == MMU_CMD_U0) {
  207. // unload current
  208. DEBUG_ECHOLNPGM("MMU <= 'U0'");
  209. tx_str_P(PSTR("U0\n"));
  210. state = 3; // wait for response
  211. }
  212. else if (WITHIN(cmd, MMU_CMD_E0, MMU_CMD_E4)) {
  213. // eject filament
  214. int filament = cmd - MMU_CMD_E0;
  215. DEBUG_ECHOLNPAIR("MMU <= E", filament);
  216. tx_printf_P(PSTR("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. tx_str_P(PSTR("R0\n"));
  223. state = 3; // wait for response
  224. }
  225. else if (WITHIN(cmd, MMU_CMD_F0, MMU_CMD_F4)) {
  226. // filament type
  227. int filament = cmd - MMU_CMD_F0;
  228. DEBUG_ECHOPAIR("MMU <= F", filament, " ");
  229. DEBUG_ECHO_F(cmd_arg, DEC);
  230. DEBUG_EOL();
  231. tx_printf_P(PSTR("F%d %d\n"), filament, cmd_arg);
  232. state = 3; // wait for response
  233. }
  234. last_cmd = cmd;
  235. cmd = MMU_CMD_NONE;
  236. }
  237. else if (ELAPSED(millis(), next_P0_request)) {
  238. // read FINDA
  239. tx_str_P(PSTR("P0\n"));
  240. state = 2; // wait for response
  241. }
  242. break;
  243. case 2: // response to command P0
  244. if (rx_ok()) {
  245. sscanf(rx_buffer, "%hhuok\n", &finda);
  246. // This is super annoying. Only activate if necessary
  247. // if (finda_runout_valid) DEBUG_ECHOLNPAIR_F("MMU <= 'P0'\nMMU => ", finda, 6);
  248. state = 1;
  249. if (cmd == 0) ready = true;
  250. if (!finda && finda_runout_valid) filament_runout();
  251. }
  252. else if (ELAPSED(millis(), last_request + MMU_P0_TIMEOUT)) // Resend request after timeout (3s)
  253. state = 1;
  254. break;
  255. case 3: // response to mmu commands
  256. if (rx_ok()) {
  257. DEBUG_ECHOLNPGM("MMU => 'ok'");
  258. ready = true;
  259. state = 1;
  260. last_cmd = MMU_CMD_NONE;
  261. }
  262. else if (ELAPSED(millis(), last_request + MMU_CMD_TIMEOUT)) {
  263. // resend request after timeout
  264. if (last_cmd) {
  265. DEBUG_ECHOLNPGM("MMU retry");
  266. cmd = last_cmd;
  267. last_cmd = MMU_CMD_NONE;
  268. }
  269. state = 1;
  270. }
  271. break;
  272. }
  273. }
  274. /**
  275. * Check if MMU was started
  276. */
  277. bool MMU2::rx_start() {
  278. // check for start message
  279. if (rx_str_P(PSTR("start\n"))) {
  280. next_P0_request = millis() + 300;
  281. return true;
  282. }
  283. return false;
  284. }
  285. /**
  286. * Check if the data received ends with the given string.
  287. */
  288. bool MMU2::rx_str_P(const char* str) {
  289. uint8_t i = strlen(rx_buffer);
  290. while (mmuSerial.available()) {
  291. rx_buffer[i++] = mmuSerial.read();
  292. rx_buffer[i] = '\0';
  293. if (i == sizeof(rx_buffer) - 1) {
  294. DEBUG_ECHOLNPGM("rx buffer overrun");
  295. break;
  296. }
  297. }
  298. uint8_t len = strlen_P(str);
  299. if (i < len) return false;
  300. str += len;
  301. while (len--) {
  302. char c0 = pgm_read_byte(str--), c1 = rx_buffer[i--];
  303. if (c0 == c1) continue;
  304. if (c0 == '\r' && c1 == '\n') continue; // match cr as lf
  305. if (c0 == '\n' && c1 == '\r') continue; // match lf as cr
  306. return false;
  307. }
  308. return true;
  309. }
  310. /**
  311. * Transfer data to MMU, no argument
  312. */
  313. void MMU2::tx_str_P(const char* str) {
  314. clear_rx_buffer();
  315. uint8_t len = strlen_P(str);
  316. LOOP_L_N(i, len) mmuSerial.write(pgm_read_byte(str++));
  317. rx_buffer[0] = '\0';
  318. last_request = millis();
  319. }
  320. /**
  321. * Transfer data to MMU, single argument
  322. */
  323. void MMU2::tx_printf_P(const char* format, int argument = -1) {
  324. clear_rx_buffer();
  325. uint8_t len = sprintf_P(tx_buffer, format, argument);
  326. LOOP_L_N(i, len) mmuSerial.write(tx_buffer[i]);
  327. rx_buffer[0] = '\0';
  328. last_request = millis();
  329. }
  330. /**
  331. * Transfer data to MMU, two arguments
  332. */
  333. void MMU2::tx_printf_P(const char* format, int argument1, int argument2) {
  334. clear_rx_buffer();
  335. uint8_t len = sprintf_P(tx_buffer, format, argument1, argument2);
  336. LOOP_L_N(i, len) mmuSerial.write(tx_buffer[i]);
  337. rx_buffer[0] = '\0';
  338. last_request = millis();
  339. }
  340. /**
  341. * Empty the rx buffer
  342. */
  343. void MMU2::clear_rx_buffer() {
  344. while (mmuSerial.available()) mmuSerial.read();
  345. rx_buffer[0] = '\0';
  346. }
  347. /**
  348. * Check if we received 'ok' from MMU
  349. */
  350. bool MMU2::rx_ok() {
  351. if (rx_str_P(PSTR("ok\n"))) {
  352. next_P0_request = millis() + 300;
  353. return true;
  354. }
  355. return false;
  356. }
  357. /**
  358. * Check if MMU has compatible firmware
  359. */
  360. void MMU2::check_version() {
  361. if (buildnr < MMU_REQUIRED_FW_BUILDNR) {
  362. SERIAL_ERROR_MSG("Invalid MMU2 firmware. Version >= " STRINGIFY(MMU_REQUIRED_FW_BUILDNR) " required.");
  363. kill(GET_TEXT(MSG_MMU2_WRONG_FIRMWARE));
  364. }
  365. }
  366. /**
  367. * Handle tool change
  368. */
  369. void MMU2::tool_change(uint8_t index) {
  370. if (!enabled) return;
  371. set_runout_valid(false);
  372. if (index != extruder) {
  373. DISABLE_AXIS_E0();
  374. ui.status_printf_P(0, GET_TEXT(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
  375. command(MMU_CMD_T0 + index);
  376. manage_response(true, true);
  377. command(MMU_CMD_C0);
  378. extruder = index; //filament change is finished
  379. active_extruder = 0;
  380. ENABLE_AXIS_E0();
  381. SERIAL_ECHO_START();
  382. SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER, int(extruder));
  383. ui.reset_status();
  384. }
  385. set_runout_valid(true);
  386. }
  387. /**
  388. *
  389. * Handle special T?/Tx/Tc commands
  390. *
  391. * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
  392. * 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.
  393. * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
  394. *
  395. */
  396. void MMU2::tool_change(const char* special) {
  397. if (!enabled) return;
  398. #if ENABLED(MMU2_MENUS)
  399. set_runout_valid(false);
  400. switch (*special) {
  401. case '?': {
  402. uint8_t index = mmu2_choose_filament();
  403. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  404. load_filament_to_nozzle(index);
  405. } break;
  406. case 'x': {
  407. planner.synchronize();
  408. uint8_t index = mmu2_choose_filament();
  409. DISABLE_AXIS_E0();
  410. command(MMU_CMD_T0 + index);
  411. manage_response(true, true);
  412. command(MMU_CMD_C0);
  413. mmu_loop();
  414. ENABLE_AXIS_E0();
  415. extruder = index;
  416. active_extruder = 0;
  417. } break;
  418. case 'c': {
  419. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  420. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
  421. } break;
  422. }
  423. set_runout_valid(true);
  424. #endif
  425. }
  426. /**
  427. * Set next command
  428. */
  429. void MMU2::command(const uint8_t mmu_cmd) {
  430. if (!enabled) return;
  431. cmd = mmu_cmd;
  432. ready = false;
  433. }
  434. /**
  435. * Wait for response from MMU
  436. */
  437. bool MMU2::get_response() {
  438. while (cmd != MMU_CMD_NONE) idle();
  439. while (!ready) {
  440. idle();
  441. if (state != 3) break;
  442. }
  443. const bool ret = ready;
  444. ready = false;
  445. return ret;
  446. }
  447. /**
  448. * Wait for response and deal with timeout if nexcessary
  449. */
  450. void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
  451. constexpr xyz_pos_t park_point = NOZZLE_PARK_POINT;
  452. bool response = false;
  453. mmu_print_saved = false;
  454. xyz_pos_t resume_position;
  455. int16_t resume_hotend_temp;
  456. KEEPALIVE_STATE(PAUSED_FOR_USER);
  457. while (!response) {
  458. response = get_response(); // wait for "ok" from mmu
  459. if (!response) { // No "ok" was received in reserved time frame, user will fix the issue on mmu unit
  460. if (!mmu_print_saved) { // First occurrence. Save current position, park print head, disable nozzle heater.
  461. planner.synchronize();
  462. mmu_print_saved = true;
  463. SERIAL_ECHOLNPGM("MMU not responding");
  464. resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
  465. resume_position = current_position;
  466. if (move_axes && all_axes_homed())
  467. nozzle.park(2, park_point /*= NOZZLE_PARK_POINT*/);
  468. if (turn_off_nozzle) thermalManager.setTargetHotend(0, active_extruder);
  469. LCD_MESSAGEPGM(MSG_MMU2_NOT_RESPONDING);
  470. BUZZ(100, 659);
  471. BUZZ(200, 698);
  472. BUZZ(100, 659);
  473. BUZZ(300, 440);
  474. BUZZ(100, 659);
  475. }
  476. }
  477. else if (mmu_print_saved) {
  478. SERIAL_ECHOLNPGM("MMU starts responding\n");
  479. if (turn_off_nozzle && resume_hotend_temp) {
  480. thermalManager.setTargetHotend(resume_hotend_temp, active_extruder);
  481. LCD_MESSAGEPGM(MSG_HEATING);
  482. BUZZ(200, 40);
  483. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(1000);
  484. }
  485. if (move_axes && all_axes_homed()) {
  486. LCD_MESSAGEPGM(MSG_MMU2_RESUMING);
  487. BUZZ(200, 404);
  488. BUZZ(200, 404);
  489. // Move XY to starting position, then Z
  490. do_blocking_move_to_xy(resume_position, feedRate_t(NOZZLE_PARK_XY_FEEDRATE));
  491. // Move Z_AXIS to saved position
  492. do_blocking_move_to_z(resume_position.z, feedRate_t(NOZZLE_PARK_Z_FEEDRATE));
  493. }
  494. else {
  495. BUZZ(200, 404);
  496. BUZZ(200, 404);
  497. LCD_MESSAGEPGM(MSG_MMU2_RESUMING);
  498. }
  499. }
  500. }
  501. }
  502. void MMU2::set_filament_type(uint8_t index, uint8_t filamentType) {
  503. if (!enabled) return;
  504. cmd_arg = filamentType;
  505. command(MMU_CMD_F0 + index);
  506. manage_response(true, true);
  507. }
  508. void MMU2::filament_runout() {
  509. queue.inject_P(PSTR(MMU2_FILAMENT_RUNOUT_SCRIPT));
  510. planner.synchronize();
  511. }
  512. #if HAS_LCD_MENU && ENABLED(MMU2_MENUS)
  513. // Load filament into MMU2
  514. void MMU2::load_filament(uint8_t index) {
  515. if (!enabled) return;
  516. command(MMU_CMD_L0 + index);
  517. manage_response(false, false);
  518. BUZZ(200, 404);
  519. }
  520. /**
  521. *
  522. * Switch material and load to nozzle
  523. *
  524. */
  525. bool MMU2::load_filament_to_nozzle(uint8_t index) {
  526. if (!enabled) return false;
  527. if (thermalManager.tooColdToExtrude(active_extruder)) {
  528. BUZZ(200, 404);
  529. LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD);
  530. return false;
  531. }
  532. else {
  533. command(MMU_CMD_T0 + index);
  534. manage_response(true, true);
  535. command(MMU_CMD_C0);
  536. mmu_loop();
  537. extruder = index;
  538. active_extruder = 0;
  539. load_to_nozzle();
  540. BUZZ(200, 404);
  541. return true;
  542. }
  543. }
  544. /**
  545. *
  546. * Load filament to nozzle of multimaterial printer
  547. *
  548. * This function is used only only after T? (user select filament) and M600 (change filament).
  549. * It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
  550. * filament to nozzle.
  551. */
  552. void MMU2::load_to_nozzle() {
  553. if (!enabled) return;
  554. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
  555. }
  556. bool MMU2::eject_filament(uint8_t index, bool recover) {
  557. if (!enabled) return false;
  558. if (thermalManager.tooColdToExtrude(active_extruder)) {
  559. BUZZ(200, 404);
  560. LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD);
  561. return false;
  562. }
  563. LCD_MESSAGEPGM(MSG_MMU2_EJECTING_FILAMENT);
  564. ENABLE_AXIS_E0();
  565. current_position.e -= MMU2_FILAMENTCHANGE_EJECT_FEED;
  566. line_to_current_position(2500 / 60);
  567. planner.synchronize();
  568. command(MMU_CMD_E0 + index);
  569. manage_response(false, false);
  570. if (recover) {
  571. LCD_MESSAGEPGM(MSG_MMU2_EJECT_RECOVER);
  572. BUZZ(200, 404);
  573. wait_for_user = true;
  574. #if ENABLED(HOST_PROMPT_SUPPORT)
  575. host_prompt_do(PROMPT_USER_CONTINUE, PSTR("MMU2 Eject Recover"), CONTINUE_STR);
  576. #endif
  577. #if ENABLED(EXTENSIBLE_UI)
  578. ExtUI::onUserConfirmRequired_P(PSTR("MMU2 Eject Recover"));
  579. #endif
  580. while (wait_for_user) idle();
  581. BUZZ(200, 404);
  582. BUZZ(200, 404);
  583. command(MMU_CMD_R0);
  584. manage_response(false, false);
  585. }
  586. ui.reset_status();
  587. // no active tool
  588. extruder = MMU2_NO_TOOL;
  589. set_runout_valid(false);
  590. BUZZ(200, 404);
  591. DISABLE_AXIS_E0();
  592. return true;
  593. }
  594. /**
  595. *
  596. * unload from hotend and retract to MMU
  597. *
  598. */
  599. bool MMU2::unload() {
  600. if (!enabled) return false;
  601. if (thermalManager.tooColdToExtrude(active_extruder)) {
  602. BUZZ(200, 404);
  603. LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD);
  604. return false;
  605. }
  606. filament_ramming();
  607. command(MMU_CMD_U0);
  608. manage_response(false, true);
  609. BUZZ(200, 404);
  610. // no active tool
  611. extruder = MMU2_NO_TOOL;
  612. set_runout_valid(false);
  613. return true;
  614. }
  615. /**
  616. * Unload sequence to optimize shape of the tip of the unloaded filament
  617. */
  618. void MMU2::filament_ramming() {
  619. execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step));
  620. }
  621. void MMU2::execute_extruder_sequence(const E_Step * sequence, int steps) {
  622. planner.synchronize();
  623. ENABLE_AXIS_E0();
  624. const E_Step* step = sequence;
  625. LOOP_L_N(i, steps) {
  626. const float es = pgm_read_float(&(step->extrude));
  627. const feedRate_t fr_mm_m = pgm_read_float(&(step->feedRate));
  628. DEBUG_ECHO_START();
  629. DEBUG_ECHOLNPAIR("E step ", es, "/", fr_mm_m);
  630. current_position.e += es;
  631. line_to_current_position(MMM_TO_MMS(fr_mm_m));
  632. planner.synchronize();
  633. step++;
  634. }
  635. DISABLE_AXIS_E0();
  636. }
  637. #endif // HAS_LCD_MENU && MMU2_MENUS
  638. #endif // PRUSA_MMU2