My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. for (uint8_t i = 0; i < MMU_LOADING_ATTEMPTS_NR; i++) {
  548. DEBUG_ECHOLNPGM("Additional load attempt #", i);
  549. if (FILAMENT_PRESENT()) break;
  550. command(MMU_CMD_C0);
  551. manage_response(true, true);
  552. }
  553. if (!FILAMENT_PRESENT()) {
  554. DEBUG_ECHOLNPGM("Filament never reached sensor, runout");
  555. filament_runout();
  556. }
  557. mmu_idl_sens = 0;
  558. }
  559. #else // !HAS_PRUSA_MMU2S && !MMU_EXTRUDER_SENSOR
  560. /**
  561. * Handle tool change
  562. */
  563. void MMU2::tool_change(const uint8_t index) {
  564. if (!_enabled) return;
  565. set_runout_valid(false);
  566. if (index != extruder) {
  567. stepper.disable_extruder();
  568. ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
  569. command(MMU_CMD_T0 + index);
  570. manage_response(true, true);
  571. command(MMU_CMD_C0);
  572. extruder = index; //filament change is finished
  573. active_extruder = 0;
  574. stepper.enable_extruder();
  575. SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, extruder);
  576. ui.reset_status();
  577. }
  578. set_runout_valid(true);
  579. }
  580. /**
  581. * Handle special T?/Tx/Tc commands
  582. *
  583. * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
  584. * 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.
  585. * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
  586. */
  587. void MMU2::tool_change(const char *special) {
  588. if (!_enabled) return;
  589. set_runout_valid(false);
  590. switch (*special) {
  591. case '?': {
  592. DEBUG_ECHOLNPGM("case ?\n");
  593. #if ENABLED(MMU2_MENUS)
  594. uint8_t index = mmu2_choose_filament();
  595. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  596. load_filament_to_nozzle(index);
  597. #else
  598. ERR_BUZZ();
  599. #endif
  600. } break;
  601. case 'x': {
  602. DEBUG_ECHOLNPGM("case x\n");
  603. #if ENABLED(MMU2_MENUS)
  604. planner.synchronize();
  605. uint8_t index = mmu2_choose_filament();
  606. stepper.disable_extruder();
  607. command(MMU_CMD_T0 + index);
  608. manage_response(true, true);
  609. command(MMU_CMD_C0);
  610. mmu_loop();
  611. stepper.enable_extruder();
  612. extruder = index;
  613. active_extruder = 0;
  614. #else
  615. ERR_BUZZ();
  616. #endif
  617. } break;
  618. case 'c': {
  619. DEBUG_ECHOLNPGM("case c\n");
  620. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
  621. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
  622. } break;
  623. }
  624. set_runout_valid(true);
  625. }
  626. #endif // HAS_PRUSA_MMU2S
  627. /**
  628. * Set next command
  629. */
  630. void MMU2::command(const uint8_t mmu_cmd) {
  631. if (!_enabled) return;
  632. cmd = mmu_cmd;
  633. ready = false;
  634. }
  635. /**
  636. * Wait for response from MMU
  637. */
  638. bool MMU2::get_response() {
  639. while (cmd != MMU_CMD_NONE) idle();
  640. while (!ready) {
  641. idle();
  642. if (state != 3) break;
  643. }
  644. const bool ret = ready;
  645. ready = false;
  646. return ret;
  647. }
  648. /**
  649. * Wait for response and deal with timeout if necessary
  650. */
  651. void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
  652. constexpr xyz_pos_t park_point = NOZZLE_PARK_POINT;
  653. bool response = false;
  654. mmu_print_saved = false;
  655. xyz_pos_t resume_position;
  656. celsius_t resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
  657. KEEPALIVE_STATE(PAUSED_FOR_USER);
  658. while (!response) {
  659. response = get_response(); // wait for "ok" from mmu
  660. if (!response) { // No "ok" was received in reserved time frame, user will fix the issue on mmu unit
  661. if (!mmu_print_saved) { // First occurrence. Save current position, park print head, disable nozzle heater.
  662. planner.synchronize();
  663. mmu_print_saved = true;
  664. SERIAL_ECHOLNPGM("MMU not responding");
  665. resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
  666. resume_position = current_position;
  667. if (move_axes && all_axes_homed())
  668. nozzle.park(0, park_point /*= NOZZLE_PARK_POINT*/);
  669. if (turn_off_nozzle) thermalManager.setTargetHotend(0, active_extruder);
  670. mmu2_not_responding();
  671. }
  672. }
  673. else if (mmu_print_saved) {
  674. SERIAL_ECHOLNPGM("MMU starts responding\n");
  675. if (turn_off_nozzle && resume_hotend_temp) {
  676. thermalManager.setTargetHotend(resume_hotend_temp, active_extruder);
  677. LCD_MESSAGE(MSG_HEATING);
  678. ERR_BUZZ();
  679. while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(1000);
  680. }
  681. LCD_MESSAGE(MSG_MMU2_RESUMING);
  682. mmu2_attn_buzz(true);
  683. #pragma GCC diagnostic push
  684. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  685. if (move_axes && all_axes_homed()) {
  686. // Move XY to starting position, then Z
  687. do_blocking_move_to_xy(resume_position, feedRate_t(NOZZLE_PARK_XY_FEEDRATE));
  688. // Move Z_AXIS to saved position
  689. do_blocking_move_to_z(resume_position.z, feedRate_t(NOZZLE_PARK_Z_FEEDRATE));
  690. }
  691. #pragma GCC diagnostic pop
  692. }
  693. }
  694. }
  695. void MMU2::set_filament_type(const uint8_t index, const uint8_t filamentType) {
  696. if (!_enabled) return;
  697. cmd_arg = filamentType;
  698. command(MMU_CMD_F0 + index);
  699. manage_response(true, true);
  700. }
  701. void MMU2::filament_runout() {
  702. queue.inject(F(MMU2_FILAMENT_RUNOUT_SCRIPT));
  703. planner.synchronize();
  704. }
  705. #if HAS_PRUSA_MMU2S
  706. void MMU2::check_filament() {
  707. const bool present = FILAMENT_PRESENT();
  708. if (cmd == MMU_CMD_NONE && last_cmd == MMU_CMD_C0) {
  709. if (present && !mmu2s_triggered) {
  710. DEBUG_ECHOLNPGM("MMU <= 'A'");
  711. tx_str(F("A\n"));
  712. }
  713. // Slowly spin the extruder during C0
  714. else {
  715. while (planner.movesplanned() < 3) {
  716. current_position.e += 0.25;
  717. line_to_current_position(MMM_TO_MMS(120));
  718. }
  719. }
  720. }
  721. mmu2s_triggered = present;
  722. }
  723. bool MMU2::can_load() {
  724. execute_extruder_sequence((const E_Step *)can_load_sequence, COUNT(can_load_sequence));
  725. int filament_detected_count = 0;
  726. const int steps = (MMU2_CAN_LOAD_RETRACT) / (MMU2_CAN_LOAD_INCREMENT);
  727. DEBUG_ECHOLNPGM("MMU can_load:");
  728. LOOP_L_N(i, steps) {
  729. execute_extruder_sequence((const E_Step *)can_load_increment_sequence, COUNT(can_load_increment_sequence));
  730. check_filament(); // Don't trust the idle function
  731. DEBUG_CHAR(mmu2s_triggered ? 'O' : 'o');
  732. if (mmu2s_triggered) ++filament_detected_count;
  733. }
  734. if (filament_detected_count <= steps - (MMU2_CAN_LOAD_DEVIATION) / (MMU2_CAN_LOAD_INCREMENT)) {
  735. DEBUG_ECHOLNPGM(" failed.");
  736. return false;
  737. }
  738. DEBUG_ECHOLNPGM(" succeeded.");
  739. return true;
  740. }
  741. #endif
  742. // Load filament into MMU2
  743. void MMU2::load_filament(const uint8_t index) {
  744. if (!_enabled) return;
  745. command(MMU_CMD_L0 + index);
  746. manage_response(false, false);
  747. mmu2_attn_buzz();
  748. }
  749. /**
  750. * Switch material and load to nozzle
  751. */
  752. bool MMU2::load_filament_to_nozzle(const uint8_t index) {
  753. if (!_enabled) return false;
  754. if (thermalManager.tooColdToExtrude(active_extruder)) {
  755. mmu2_attn_buzz();
  756. LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
  757. return false;
  758. }
  759. stepper.disable_extruder();
  760. command(MMU_CMD_T0 + index);
  761. manage_response(true, true);
  762. const bool success = load_to_gears();
  763. if (success) {
  764. mmu_loop();
  765. extruder = index;
  766. active_extruder = 0;
  767. load_to_nozzle();
  768. mmu2_attn_buzz();
  769. }
  770. return success;
  771. }
  772. /**
  773. * Load filament to nozzle of multimaterial printer
  774. *
  775. * This function is used only after T? (user select filament) and M600 (change filament).
  776. * It is not used after T0 .. T4 command (select filament), in such case, G-code is responsible for loading
  777. * filament to nozzle.
  778. */
  779. void MMU2::load_to_nozzle() {
  780. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
  781. }
  782. bool MMU2::eject_filament(const uint8_t index, const bool recover) {
  783. if (!_enabled) return false;
  784. if (thermalManager.tooColdToExtrude(active_extruder)) {
  785. mmu2_attn_buzz();
  786. LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
  787. return false;
  788. }
  789. LCD_MESSAGE(MSG_MMU2_EJECTING_FILAMENT);
  790. stepper.enable_extruder();
  791. current_position.e -= MMU2_FILAMENTCHANGE_EJECT_FEED;
  792. line_to_current_position(MMM_TO_MMS(2500));
  793. planner.synchronize();
  794. command(MMU_CMD_E0 + index);
  795. manage_response(false, false);
  796. if (recover) {
  797. LCD_MESSAGE(MSG_MMU2_EJECT_RECOVER);
  798. mmu2_attn_buzz();
  799. TERN_(HOST_PROMPT_SUPPORT, hostui.prompt_do(PROMPT_USER_CONTINUE, F("MMU2 Eject Recover"), FPSTR(CONTINUE_STR)));
  800. TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired(F("MMU2 Eject Recover")));
  801. TERN_(HAS_RESUME_CONTINUE, wait_for_user_response());
  802. mmu2_attn_buzz(true);
  803. command(MMU_CMD_R0);
  804. manage_response(false, false);
  805. }
  806. ui.reset_status();
  807. // no active tool
  808. extruder = MMU2_NO_TOOL;
  809. set_runout_valid(false);
  810. mmu2_attn_buzz();
  811. stepper.disable_extruder();
  812. return true;
  813. }
  814. /**
  815. * Unload from hotend and retract to MMU
  816. */
  817. bool MMU2::unload() {
  818. if (!_enabled) return false;
  819. if (thermalManager.tooColdToExtrude(active_extruder)) {
  820. mmu2_attn_buzz();
  821. LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
  822. return false;
  823. }
  824. // Unload sequence to optimize shape of the tip of the unloaded filament
  825. execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step));
  826. command(MMU_CMD_U0);
  827. manage_response(false, true);
  828. mmu2_attn_buzz();
  829. // no active tool
  830. extruder = MMU2_NO_TOOL;
  831. set_runout_valid(false);
  832. return true;
  833. }
  834. void MMU2::execute_extruder_sequence(const E_Step * sequence, int steps) {
  835. planner.synchronize();
  836. stepper.enable_extruder();
  837. const E_Step* step = sequence;
  838. LOOP_L_N(i, steps) {
  839. const float es = pgm_read_float(&(step->extrude));
  840. const feedRate_t fr_mm_m = pgm_read_float(&(step->feedRate));
  841. DEBUG_ECHO_MSG("E step ", es, "/", fr_mm_m);
  842. current_position.e += es;
  843. line_to_current_position(MMM_TO_MMS(fr_mm_m));
  844. planner.synchronize();
  845. step++;
  846. }
  847. stepper.disable_extruder();
  848. }
  849. #endif // HAS_PRUSA_MMU2