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

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