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

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