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

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