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

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