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

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