1043 lines
27 KiB
C++
Raw Normal View History

2019-02-01 02:10:52 +01:00
/**
* Marlin 3D Printer Firmware
2020-02-03 08:00:57 -06:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2019-02-01 02:10:52 +01:00
*
* Based on Sprinter and grbl.
2019-06-27 23:57:50 -05:00
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
2019-02-01 02:10:52 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2020-07-23 05:20:14 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-02-01 02:10:52 +01:00
*
*/
#include "../../inc/MarlinConfig.h"
2020-11-18 08:27:21 +01:00
#if HAS_PRUSA_MMU2
2019-02-01 02:10:52 +01:00
#include "mmu2.h"
2019-02-06 17:38:46 -06:00
#include "../../lcd/menu/menu_mmu2.h"
2019-02-01 02:10:52 +01:00
MMU2 mmu2;
#include "../../gcode/gcode.h"
2020-10-16 19:36:25 -05:00
#include "../../lcd/marlinui.h"
2019-02-01 02:10:52 +01:00
#include "../../libs/buzzer.h"
#include "../../libs/nozzle.h"
#include "../../module/temperature.h"
#include "../../module/planner.h"
#include "../../module/stepper.h"
#include "../../MarlinCore.h"
2019-02-01 02:10:52 +01:00
2019-02-12 16:55:47 -05:00
#if ENABLED(HOST_PROMPT_SUPPORT)
#include "../../feature/host_actions.h"
#endif
#if ENABLED(EXTENSIBLE_UI)
2020-03-13 16:29:29 -05:00
#include "../../lcd/extui/ui_api.h"
#endif
#define DEBUG_OUT ENABLED(MMU2_DEBUG)
#include "../../core/debug_out.h"
2019-02-01 02:10:52 +01:00
#define MMU_TODELAY 100
#define MMU_TIMEOUT 10
2020-05-17 21:52:45 +02:00
#define MMU_CMD_TIMEOUT 45000UL // 45s timeout for mmu commands (except P0)
#define MMU_P0_TIMEOUT 3000UL // Timeout for P0 command: 3seconds
#define MMU2_COMMAND(S) tx_str(F(S "\n"))
2020-05-17 21:52:45 +02:00
#if ENABLED(MMU_EXTRUDER_SENSOR)
uint8_t mmu_idl_sens = 0;
static bool mmu_loading_flag = false;
#endif
2019-02-01 02:10:52 +01:00
#define MMU_CMD_NONE 0
#define MMU_CMD_T0 0x10 // up to supported filaments
#define MMU_CMD_L0 0x20 // up to supported filaments
2019-02-01 02:10:52 +01:00
#define MMU_CMD_C0 0x30
#define MMU_CMD_U0 0x40
#define MMU_CMD_E0 0x50 // up to supported filaments
2019-02-01 02:10:52 +01:00
#define MMU_CMD_R0 0x60
#define MMU_CMD_F0 0x70 // up to supported filaments
2019-02-01 02:10:52 +01:00
2020-05-17 21:52:45 +02:00
#define MMU_REQUIRED_FW_BUILDNR TERN(MMU2_MODE_12V, 132, 126)
2019-02-01 02:10:52 +01:00
#define MMU2_NO_TOOL 99
#define MMU_BAUD 115200
2021-07-17 03:10:54 -05:00
bool MMU2::_enabled, MMU2::ready, MMU2::mmu_print_saved;
2020-11-18 08:27:21 +01:00
#if HAS_PRUSA_MMU2S
bool MMU2::mmu2s_triggered;
#endif
2019-02-01 02:10:52 +01:00
uint8_t MMU2::cmd, MMU2::cmd_arg, MMU2::last_cmd, MMU2::extruder;
int8_t MMU2::state = 0;
volatile int8_t MMU2::finda = 1;
2019-04-08 17:56:40 -05:00
volatile bool MMU2::finda_runout_valid;
2019-02-01 02:10:52 +01:00
int16_t MMU2::version = -1, MMU2::buildnr = -1;
2020-05-17 21:52:45 +02:00
millis_t MMU2::prev_request, MMU2::prev_P0_request;
char MMU2::rx_buffer[MMU_RX_SIZE], MMU2::tx_buffer[MMU_TX_SIZE];
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
struct E_Step {
float extrude; //!< extrude distance in mm
feedRate_t feedRate; //!< feed rate in mm/s
};
static constexpr E_Step
ramming_sequence[] PROGMEM = { MMU2_RAMMING_SEQUENCE }
, load_to_nozzle_sequence[] PROGMEM = { MMU2_LOAD_TO_NOZZLE_SEQUENCE }
#if HAS_PRUSA_MMU2S
, can_load_sequence[] PROGMEM = { MMU2_CAN_LOAD_SEQUENCE }
, can_load_increment_sequence[] PROGMEM = { MMU2_CAN_LOAD_INCREMENT_SEQUENCE }
#endif
;
2019-02-01 02:10:52 +01:00
MMU2::MMU2() {
rx_buffer[0] = '\0';
}
void MMU2::init() {
2019-02-06 03:56:22 +01:00
set_runout_valid(false);
2019-02-01 02:10:52 +01:00
#if PIN_EXISTS(MMU2_RST)
WRITE(MMU2_RST_PIN, HIGH);
SET_OUTPUT(MMU2_RST_PIN);
#endif
MMU2_SERIAL.begin(MMU_BAUD);
2019-02-01 02:10:52 +01:00
extruder = MMU2_NO_TOOL;
safe_delay(10);
reset();
rx_buffer[0] = '\0';
state = -1;
}
void MMU2::reset() {
DEBUG_ECHOLNPGM("MMU <= reset");
2019-02-01 02:10:52 +01:00
#if PIN_EXISTS(MMU2_RST)
WRITE(MMU2_RST_PIN, LOW);
safe_delay(20);
WRITE(MMU2_RST_PIN, HIGH);
#else
MMU2_COMMAND("X0"); // Send soft reset
2019-02-01 02:10:52 +01:00
#endif
}
2019-04-08 17:56:40 -05:00
uint8_t MMU2::get_current_tool() {
2019-02-01 02:10:52 +01:00
return extruder == MMU2_NO_TOOL ? -1 : extruder;
}
2020-11-18 08:27:21 +01:00
#if EITHER(HAS_PRUSA_MMU2S, MMU_EXTRUDER_SENSOR)
#define FILAMENT_PRESENT() (READ(FIL_RUNOUT1_PIN) != FIL_RUNOUT1_STATE)
2020-05-17 21:52:45 +02:00
#endif
2019-04-08 17:56:40 -05:00
void MMU2::mmu_loop() {
2019-02-01 02:10:52 +01:00
switch (state) {
case 0: break;
case -1:
if (rx_start()) {
prev_P0_request = millis(); // Initialize finda sensor timeout
DEBUG_ECHOLNPGM("MMU => 'start'");
DEBUG_ECHOLNPGM("MMU <= 'S1'");
2019-02-01 02:10:52 +01:00
MMU2_COMMAND("S1"); // Read Version
2019-02-01 02:10:52 +01:00
state = -2;
}
else if (millis() > 30000) { // 30sec after reset disable MMU
2019-02-01 02:10:52 +01:00
SERIAL_ECHOLNPGM("MMU not responding - DISABLED");
state = 0;
}
break;
case -2:
if (rx_ok()) {
2020-11-18 08:27:21 +01:00
sscanf(rx_buffer, "%huok\n", &version);
2019-02-01 02:10:52 +01:00
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("MMU => ", version, "\nMMU <= 'S2'");
2019-02-01 02:10:52 +01:00
MMU2_COMMAND("S2"); // Read Build Number
2019-02-01 02:10:52 +01:00
state = -3;
}
break;
case -3:
if (rx_ok()) {
2020-11-18 08:27:21 +01:00
sscanf(rx_buffer, "%huok\n", &buildnr);
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("MMU => ", buildnr);
2019-02-01 02:10:52 +01:00
2019-04-08 17:56:40 -05:00
check_version();
2019-02-01 02:10:52 +01:00
#if ENABLED(MMU2_MODE_12V)
DEBUG_ECHOLNPGM("MMU <= 'M1'");
2019-02-01 02:10:52 +01:00
MMU2_COMMAND("M1"); // Stealth Mode
2019-02-01 02:10:52 +01:00
state = -5;
#else
DEBUG_ECHOLNPGM("MMU <= 'P0'");
2019-02-01 02:10:52 +01:00
MMU2_COMMAND("P0"); // Read FINDA
2019-02-01 02:10:52 +01:00
state = -4;
#endif
}
break;
#if ENABLED(MMU2_MODE_12V)
2019-02-01 02:10:52 +01:00
case -5:
// response to M1
if (rx_ok()) {
DEBUG_ECHOLNPGM("MMU => ok");
2019-02-01 02:10:52 +01:00
DEBUG_ECHOLNPGM("MMU <= 'P0'");
2019-02-01 02:10:52 +01:00
MMU2_COMMAND("P0"); // Read FINDA
2019-02-01 02:10:52 +01:00
state = -4;
}
break;
#endif
2019-02-01 02:10:52 +01:00
2019-02-03 23:00:57 -06:00
case -4:
2019-02-01 02:10:52 +01:00
if (rx_ok()) {
sscanf(rx_buffer, "%hhuok\n", &finda);
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("MMU => ", finda, "\nMMU - ENABLED");
2019-02-01 02:10:52 +01:00
2021-07-17 03:10:54 -05:00
_enabled = true;
2019-02-01 02:10:52 +01:00
state = 1;
2020-11-18 08:27:21 +01:00
TERN_(HAS_PRUSA_MMU2S, mmu2s_triggered = false);
2019-02-01 02:10:52 +01:00
}
break;
case 1:
if (cmd) {
if (WITHIN(cmd, MMU_CMD_T0, MMU_CMD_T0 + EXTRUDERS - 1)) {
2019-02-01 02:10:52 +01:00
// tool change
const int filament = cmd - MMU_CMD_T0;
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("MMU <= T", filament);
tx_printf(F("T%d\n"), filament);
2020-05-17 21:52:45 +02:00
TERN_(MMU_EXTRUDER_SENSOR, mmu_idl_sens = 1); // enable idler sensor, if any
2019-02-01 02:10:52 +01:00
state = 3; // wait for response
}
else if (WITHIN(cmd, MMU_CMD_L0, MMU_CMD_L0 + EXTRUDERS - 1)) {
2019-02-01 02:10:52 +01:00
// load
const int filament = cmd - MMU_CMD_L0;
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("MMU <= L", filament);
tx_printf(F("L%d\n"), filament);
2019-02-01 02:10:52 +01:00
state = 3; // wait for response
}
else if (cmd == MMU_CMD_C0) {
// continue loading
DEBUG_ECHOLNPGM("MMU <= 'C0'");
MMU2_COMMAND("C0");
2019-02-01 02:10:52 +01:00
state = 3; // wait for response
}
else if (cmd == MMU_CMD_U0) {
// unload current
DEBUG_ECHOLNPGM("MMU <= 'U0'");
2019-02-01 02:10:52 +01:00
MMU2_COMMAND("U0");
2019-02-01 02:10:52 +01:00
state = 3; // wait for response
}
else if (WITHIN(cmd, MMU_CMD_E0, MMU_CMD_E0 + EXTRUDERS - 1)) {
2019-02-01 02:10:52 +01:00
// eject filament
const int filament = cmd - MMU_CMD_E0;
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("MMU <= E", filament);
tx_printf(F("E%d\n"), filament);
2019-02-01 02:10:52 +01:00
state = 3; // wait for response
}
else if (cmd == MMU_CMD_R0) {
// recover after eject
DEBUG_ECHOLNPGM("MMU <= 'R0'");
MMU2_COMMAND("R0");
2019-02-01 02:10:52 +01:00
state = 3; // wait for response
}
else if (WITHIN(cmd, MMU_CMD_F0, MMU_CMD_F0 + EXTRUDERS - 1)) {
2019-02-01 02:10:52 +01:00
// filament type
const int filament = cmd - MMU_CMD_F0;
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("MMU <= F", filament, " ", cmd_arg);
tx_printf(F("F%d %d\n"), filament, cmd_arg);
2019-02-01 02:10:52 +01:00
state = 3; // wait for response
}
last_cmd = cmd;
cmd = MMU_CMD_NONE;
}
2020-05-17 21:52:45 +02:00
else if (ELAPSED(millis(), prev_P0_request + 300)) {
MMU2_COMMAND("P0"); // Read FINDA
2019-02-01 02:10:52 +01:00
state = 2; // wait for response
}
2020-04-28 04:31:59 -05:00
2020-11-18 08:27:21 +01:00
TERN_(HAS_PRUSA_MMU2S, check_filament());
2019-02-01 02:10:52 +01:00
break;
case 2: // response to command P0
if (rx_ok()) {
sscanf(rx_buffer, "%hhuok\n", &finda);
// This is super annoying. Only activate if necessary
2019-04-08 17:56:40 -05:00
// if (finda_runout_valid) DEBUG_ECHOLNPAIR_F("MMU <= 'P0'\nMMU => ", finda, 6);
2019-02-01 02:10:52 +01:00
2019-04-08 17:56:40 -05:00
if (!finda && finda_runout_valid) filament_runout();
if (cmd == MMU_CMD_NONE) ready = true;
2020-05-17 21:52:45 +02:00
state = 1;
2019-02-01 02:10:52 +01:00
}
2020-05-17 21:52:45 +02:00
else if (ELAPSED(millis(), prev_request + MMU_P0_TIMEOUT)) // Resend request after timeout (3s)
2019-02-01 02:10:52 +01:00
state = 1;
2020-11-18 08:27:21 +01:00
TERN_(HAS_PRUSA_MMU2S, check_filament());
2019-02-01 02:10:52 +01:00
break;
case 3: // response to mmu commands
2020-05-17 21:52:45 +02:00
#if ENABLED(MMU_EXTRUDER_SENSOR)
if (mmu_idl_sens) {
if (FILAMENT_PRESENT() && mmu_loading_flag) {
DEBUG_ECHOLNPGM("MMU <= 'A'");
MMU2_COMMAND("A"); // send 'abort' request
2020-05-17 21:52:45 +02:00
mmu_idl_sens = 0;
DEBUG_ECHOLNPGM("MMU IDLER_SENSOR = 0 - ABORT");
2020-05-17 21:52:45 +02:00
}
}
#endif
2019-02-01 02:10:52 +01:00
if (rx_ok()) {
2020-11-18 08:27:21 +01:00
#if HAS_PRUSA_MMU2S
// Respond to C0 MMU command in MMU2S model
const bool keep_trying = !mmu2s_triggered && last_cmd == MMU_CMD_C0;
if (keep_trying) {
// MMU ok received but filament sensor not triggered, retrying...
DEBUG_ECHOLNPGM("MMU => 'ok' (filament not present in gears)");
DEBUG_ECHOLNPGM("MMU <= 'C0' (keep trying)");
MMU2_COMMAND("C0");
}
#else
constexpr bool keep_trying = false;
#endif
if (!keep_trying) {
DEBUG_ECHOLNPGM("MMU => 'ok'");
ready = true;
state = 1;
last_cmd = MMU_CMD_NONE;
}
2019-02-01 02:10:52 +01:00
}
2020-05-17 21:52:45 +02:00
else if (ELAPSED(millis(), prev_request + MMU_CMD_TIMEOUT)) {
2019-02-01 02:10:52 +01:00
// resend request after timeout
if (last_cmd) {
DEBUG_ECHOLNPGM("MMU retry");
2019-02-01 02:10:52 +01:00
cmd = last_cmd;
last_cmd = MMU_CMD_NONE;
}
state = 1;
}
2020-11-18 08:27:21 +01:00
TERN_(HAS_PRUSA_MMU2S, check_filament());
2019-02-01 02:10:52 +01:00
break;
}
}
/**
* Check if MMU was started
*/
bool MMU2::rx_start() {
// check for start message
return rx_str(F("start\n"));
2019-02-01 02:10:52 +01:00
}
/**
* Check if the data received ends with the given string.
*/
bool MMU2::rx_str(FSTR_P fstr) {
PGM_P pstr = FTOP(fstr);
2019-02-01 02:10:52 +01:00
uint8_t i = strlen(rx_buffer);
while (MMU2_SERIAL.available()) {
rx_buffer[i++] = MMU2_SERIAL.read();
2019-02-01 02:10:52 +01:00
if (i == sizeof(rx_buffer) - 1) {
DEBUG_ECHOLNPGM("rx buffer overrun");
2019-02-01 02:10:52 +01:00
break;
}
}
rx_buffer[i] = '\0';
2019-02-01 02:10:52 +01:00
uint8_t len = strlen_P(pstr);
2019-02-01 02:10:52 +01:00
if (i < len) return false;
pstr += len;
2019-02-01 02:10:52 +01:00
while (len--) {
char c0 = pgm_read_byte(pstr--), c1 = rx_buffer[i--];
2019-02-01 02:10:52 +01:00
if (c0 == c1) continue;
if (c0 == '\r' && c1 == '\n') continue; // match cr as lf
if (c0 == '\n' && c1 == '\r') continue; // match lf as cr
return false;
}
return true;
}
/**
* Transfer data to MMU, no argument
*/
void MMU2::tx_str(FSTR_P fstr) {
2019-02-01 02:10:52 +01:00
clear_rx_buffer();
PGM_P pstr = FTOP(fstr);
while (const char c = pgm_read_byte(pstr)) { MMU2_SERIAL.write(c); pstr++; }
2020-05-17 21:52:45 +02:00
prev_request = millis();
2019-02-01 02:10:52 +01:00
}
/**
* Transfer data to MMU, single argument
*/
void MMU2::tx_printf(FSTR_P format, int argument = -1) {
2019-02-01 02:10:52 +01:00
clear_rx_buffer();
const uint8_t len = sprintf_P(tx_buffer, FTOP(format), argument);
LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
2020-05-17 21:52:45 +02:00
prev_request = millis();
2019-02-01 02:10:52 +01:00
}
/**
* Transfer data to MMU, two arguments
*/
void MMU2::tx_printf(FSTR_P format, int argument1, int argument2) {
2019-02-01 02:10:52 +01:00
clear_rx_buffer();
const uint8_t len = sprintf_P(tx_buffer, FTOP(format), argument1, argument2);
LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
2020-05-17 21:52:45 +02:00
prev_request = millis();
2019-02-01 02:10:52 +01:00
}
/**
* Empty the rx buffer
*/
void MMU2::clear_rx_buffer() {
while (MMU2_SERIAL.available()) MMU2_SERIAL.read();
2019-02-01 02:10:52 +01:00
rx_buffer[0] = '\0';
}
/**
* Check if we received 'ok' from MMU
*/
bool MMU2::rx_ok() {
if (rx_str(F("ok\n"))) {
2020-05-17 21:52:45 +02:00
prev_P0_request = millis();
2019-02-01 02:10:52 +01:00
return true;
}
return false;
}
/**
* Check if MMU has compatible firmware
*/
2019-04-08 17:56:40 -05:00
void MMU2::check_version() {
2019-02-01 02:10:52 +01:00
if (buildnr < MMU_REQUIRED_FW_BUILDNR) {
SERIAL_ERROR_MSG("Invalid MMU2 firmware. Version >= " STRINGIFY(MMU_REQUIRED_FW_BUILDNR) " required.");
2020-04-13 14:53:21 -05:00
kill(GET_TEXT(MSG_KILL_MMU2_FIRMWARE));
2019-02-01 02:10:52 +01:00
}
}
2020-07-23 22:27:40 -04:00
static void mmu2_not_responding() {
2021-09-25 17:05:11 -05:00
LCD_MESSAGE(MSG_MMU2_NOT_RESPONDING);
BUZZ(100, 659);
BUZZ(200, 698);
BUZZ(100, 659);
BUZZ(300, 440);
BUZZ(100, 659);
}
2020-11-18 08:27:21 +01:00
#if HAS_PRUSA_MMU2S
bool MMU2::load_to_gears() {
command(MMU_CMD_C0);
manage_response(true, true);
LOOP_L_N(i, MMU2_C0_RETRY) { // Keep loading until filament reaches gears
if (mmu2s_triggered) break;
command(MMU_CMD_C0);
manage_response(true, true);
check_filament();
}
const bool success = mmu2s_triggered && can_load();
if (!success) mmu2_not_responding();
return success;
}
2020-05-17 21:52:45 +02:00
/**
* Handle tool change
*/
void MMU2::tool_change(const uint8_t index) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2020-05-17 21:52:45 +02:00
set_runout_valid(false);
if (index != extruder) {
stepper.disable_extruder();
2021-09-25 17:05:11 -05:00
ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
2020-05-17 21:52:45 +02:00
command(MMU_CMD_T0 + index);
manage_response(true, true);
if (load_to_gears()) {
extruder = index; // filament change is finished
active_extruder = 0;
stepper.enable_extruder();
SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, extruder);
2020-05-17 21:52:45 +02:00
}
ui.reset_status();
}
set_runout_valid(true);
}
/**
* Handle special T?/Tx/Tc commands
*
* T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
* 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.
* Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
*/
2021-03-29 20:36:37 -05:00
void MMU2::tool_change(const char *special) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2020-05-17 21:52:45 +02:00
set_runout_valid(false);
switch (*special) {
case '?': {
2020-11-18 08:27:21 +01:00
#if ENABLED(MMU2_MENUS)
const uint8_t index = mmu2_choose_filament();
while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
load_filament_to_nozzle(index);
#else
BUZZ(400, 40);
#endif
2020-05-17 21:52:45 +02:00
} break;
case 'x': {
2020-11-18 08:27:21 +01:00
#if ENABLED(MMU2_MENUS)
planner.synchronize();
const uint8_t index = mmu2_choose_filament();
stepper.disable_extruder();
2020-11-18 08:27:21 +01:00
command(MMU_CMD_T0 + index);
manage_response(true, true);
if (load_to_gears()) {
mmu_loop();
stepper.enable_extruder();
2020-11-18 08:27:21 +01:00
extruder = index;
active_extruder = 0;
}
#else
BUZZ(400, 40);
#endif
2020-05-17 21:52:45 +02:00
} break;
case 'c': {
while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
load_to_nozzle();
2020-05-17 21:52:45 +02:00
} break;
}
set_runout_valid(true);
}
#elif ENABLED(MMU_EXTRUDER_SENSOR)
/**
* Handle tool change
*/
void MMU2::tool_change(const uint8_t index) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2020-05-17 21:52:45 +02:00
set_runout_valid(false);
if (index != extruder) {
stepper.disable_extruder();
2020-05-17 21:52:45 +02:00
if (FILAMENT_PRESENT()) {
DEBUG_ECHOLNPGM("Unloading\n");
mmu_loading_flag = false;
command(MMU_CMD_U0);
manage_response(true, true);
}
2021-09-25 17:05:11 -05:00
ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
2020-05-17 21:52:45 +02:00
mmu_loading_flag = true;
command(MMU_CMD_T0 + index);
manage_response(true, true);
mmu_continue_loading();
command(MMU_CMD_C0);
extruder = index;
active_extruder = 0;
stepper.enable_extruder();
SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, extruder);
2020-05-17 21:52:45 +02:00
ui.reset_status();
}
set_runout_valid(true);
}
/**
* Handle special T?/Tx/Tc commands
*
* T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
* 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.
* Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
*/
2021-03-29 20:36:37 -05:00
void MMU2::tool_change(const char *special) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2020-05-17 21:52:45 +02:00
2020-11-18 08:27:21 +01:00
set_runout_valid(false);
2020-05-17 21:52:45 +02:00
2020-11-18 08:27:21 +01:00
switch (*special) {
case '?': {
DEBUG_ECHOLNPGM("case ?\n");
#if ENABLED(MMU2_MENUS)
2020-05-17 21:52:45 +02:00
uint8_t index = mmu2_choose_filament();
while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
load_filament_to_nozzle(index);
2020-11-18 08:27:21 +01:00
#else
BUZZ(400, 40);
#endif
} break;
2020-05-17 21:52:45 +02:00
2020-11-18 08:27:21 +01:00
case 'x': {
DEBUG_ECHOLNPGM("case x\n");
#if ENABLED(MMU2_MENUS)
2020-05-17 21:52:45 +02:00
planner.synchronize();
uint8_t index = mmu2_choose_filament();
stepper.disable_extruder();
2020-05-17 21:52:45 +02:00
command(MMU_CMD_T0 + index);
manage_response(true, true);
mmu_continue_loading();
command(MMU_CMD_C0);
mmu_loop();
stepper.enable_extruder();
2020-05-17 21:52:45 +02:00
extruder = index;
active_extruder = 0;
2020-11-18 08:27:21 +01:00
#else
BUZZ(400, 40);
#endif
} break;
2020-05-17 21:52:45 +02:00
2020-11-18 08:27:21 +01:00
case 'c': {
DEBUG_ECHOLNPGM("case c\n");
while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
} break;
}
2020-05-17 21:52:45 +02:00
2020-11-18 08:27:21 +01:00
set_runout_valid(true);
2020-05-17 21:52:45 +02:00
}
void MMU2::mmu_continue_loading() {
for (uint8_t i = 0; i < MMU_LOADING_ATTEMPTS_NR; i++) {
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("Additional load attempt #", i);
2020-05-17 21:52:45 +02:00
if (FILAMENT_PRESENT()) break;
command(MMU_CMD_C0);
manage_response(true, true);
}
if (!FILAMENT_PRESENT()) {
DEBUG_ECHOLNPGM("Filament never reached sensor, runout");
filament_runout();
}
mmu_idl_sens = 0;
}
2020-11-18 08:27:21 +01:00
#else // !HAS_PRUSA_MMU2S && !MMU_EXTRUDER_SENSOR
2020-11-18 08:27:21 +01:00
/**
* Handle tool change
*/
void MMU2::tool_change(const uint8_t index) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
set_runout_valid(false);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
if (index != extruder) {
stepper.disable_extruder();
2021-09-25 17:05:11 -05:00
ui.status_printf(0, GET_TEXT_F(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
2020-11-18 08:27:21 +01:00
command(MMU_CMD_T0 + index);
manage_response(true, true);
command(MMU_CMD_C0);
extruder = index; //filament change is finished
active_extruder = 0;
stepper.enable_extruder();
SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, extruder);
2020-11-18 08:27:21 +01:00
ui.reset_status();
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
set_runout_valid(true);
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
/**
* Handle special T?/Tx/Tc commands
*
* T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
* 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.
* Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
*/
2021-03-29 20:36:37 -05:00
void MMU2::tool_change(const char *special) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2019-02-01 02:10:52 +01:00
2019-02-06 03:56:22 +01:00
set_runout_valid(false);
2019-02-01 02:10:52 +01:00
2019-02-12 16:25:49 -06:00
switch (*special) {
2019-02-01 02:10:52 +01:00
case '?': {
2020-05-17 21:52:45 +02:00
DEBUG_ECHOLNPGM("case ?\n");
2020-11-18 08:27:21 +01:00
#if ENABLED(MMU2_MENUS)
uint8_t index = mmu2_choose_filament();
while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
load_filament_to_nozzle(index);
#else
BUZZ(400, 40);
#endif
2019-02-01 02:10:52 +01:00
} break;
case 'x': {
2020-05-17 21:52:45 +02:00
DEBUG_ECHOLNPGM("case x\n");
2020-11-18 08:27:21 +01:00
#if ENABLED(MMU2_MENUS)
planner.synchronize();
uint8_t index = mmu2_choose_filament();
stepper.disable_extruder();
2020-11-18 08:27:21 +01:00
command(MMU_CMD_T0 + index);
manage_response(true, true);
command(MMU_CMD_C0);
mmu_loop();
2019-02-01 02:10:52 +01:00
stepper.enable_extruder();
2020-11-18 08:27:21 +01:00
extruder = index;
active_extruder = 0;
#else
BUZZ(400, 40);
#endif
2019-02-01 02:10:52 +01:00
} break;
case 'c': {
2020-05-17 21:52:45 +02:00
DEBUG_ECHOLNPGM("case c\n");
2019-02-01 02:10:52 +01:00
while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
2019-04-08 17:56:40 -05:00
execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
2019-02-01 02:10:52 +01:00
} break;
}
2019-02-06 03:56:22 +01:00
set_runout_valid(true);
2020-05-17 21:52:45 +02:00
}
2020-11-18 08:27:21 +01:00
#endif // HAS_PRUSA_MMU2S
2019-02-01 02:10:52 +01:00
/**
* Set next command
*/
void MMU2::command(const uint8_t mmu_cmd) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2019-02-01 02:10:52 +01:00
cmd = mmu_cmd;
ready = false;
}
/**
* Wait for response from MMU
*/
2019-09-16 20:31:08 -05:00
bool MMU2::get_response() {
2019-02-01 02:10:52 +01:00
while (cmd != MMU_CMD_NONE) idle();
while (!ready) {
idle();
if (state != 3) break;
}
const bool ret = ready;
ready = false;
return ret;
}
/**
* Wait for response and deal with timeout if necessary
2019-02-01 02:10:52 +01:00
*/
void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
2019-02-01 02:10:52 +01:00
2019-09-29 04:25:39 -05:00
constexpr xyz_pos_t park_point = NOZZLE_PARK_POINT;
2019-02-01 02:10:52 +01:00
bool response = false;
mmu_print_saved = false;
2019-09-29 04:25:39 -05:00
xyz_pos_t resume_position;
2021-03-24 04:11:43 -05:00
celsius_t resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
2019-02-01 02:10:52 +01:00
KEEPALIVE_STATE(PAUSED_FOR_USER);
2019-02-01 02:10:52 +01:00
while (!response) {
response = get_response(); // wait for "ok" from mmu
2019-02-01 02:10:52 +01:00
if (!response) { // No "ok" was received in reserved time frame, user will fix the issue on mmu unit
if (!mmu_print_saved) { // First occurrence. Save current position, park print head, disable nozzle heater.
2019-02-01 02:10:52 +01:00
planner.synchronize();
mmu_print_saved = true;
SERIAL_ECHOLNPGM("MMU not responding");
resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
2019-09-29 04:25:39 -05:00
resume_position = current_position;
2019-02-01 02:10:52 +01:00
if (move_axes && all_axes_homed())
2020-04-25 05:39:08 +02:00
nozzle.park(0, park_point /*= NOZZLE_PARK_POINT*/);
2019-02-01 02:10:52 +01:00
if (turn_off_nozzle) thermalManager.setTargetHotend(0, active_extruder);
mmu2_not_responding();
2019-02-01 02:10:52 +01:00
}
}
else if (mmu_print_saved) {
SERIAL_ECHOLNPGM("MMU starts responding\n");
if (turn_off_nozzle && resume_hotend_temp) {
thermalManager.setTargetHotend(resume_hotend_temp, active_extruder);
2021-09-25 17:05:11 -05:00
LCD_MESSAGE(MSG_HEATING);
2019-02-01 02:10:52 +01:00
BUZZ(200, 40);
while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(1000);
}
if (move_axes && all_axes_homed()) {
2021-09-25 17:05:11 -05:00
LCD_MESSAGE(MSG_MMU2_RESUMING);
2020-09-27 02:01:56 -05:00
BUZZ(198, 404); BUZZ(4, 0); BUZZ(198, 404);
2019-02-01 02:10:52 +01:00
// Move XY to starting position, then Z
2019-09-29 04:25:39 -05:00
do_blocking_move_to_xy(resume_position, feedRate_t(NOZZLE_PARK_XY_FEEDRATE));
2019-02-01 02:10:52 +01:00
// Move Z_AXIS to saved position
2019-09-29 04:25:39 -05:00
do_blocking_move_to_z(resume_position.z, feedRate_t(NOZZLE_PARK_Z_FEEDRATE));
2019-02-01 02:10:52 +01:00
}
else {
2020-09-27 02:01:56 -05:00
BUZZ(198, 404); BUZZ(4, 0); BUZZ(198, 404);
2021-09-25 17:05:11 -05:00
LCD_MESSAGE(MSG_MMU2_RESUMING);
2019-02-01 02:10:52 +01:00
}
}
}
}
2020-05-17 21:52:45 +02:00
void MMU2::set_filament_type(const uint8_t index, const uint8_t filamentType) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2019-02-01 02:10:52 +01:00
cmd_arg = filamentType;
command(MMU_CMD_F0 + index);
2019-04-08 17:56:40 -05:00
manage_response(true, true);
2019-02-01 02:10:52 +01:00
}
2019-04-08 17:56:40 -05:00
void MMU2::filament_runout() {
queue.inject(F(MMU2_FILAMENT_RUNOUT_SCRIPT));
2019-02-01 02:10:52 +01:00
planner.synchronize();
}
2020-11-18 08:27:21 +01:00
#if HAS_PRUSA_MMU2S
2020-05-17 21:52:45 +02:00
void MMU2::check_filament() {
2020-05-17 21:52:45 +02:00
const bool present = FILAMENT_PRESENT();
if (cmd == MMU_CMD_NONE && last_cmd == MMU_CMD_C0) {
if (present && !mmu2s_triggered) {
DEBUG_ECHOLNPGM("MMU <= 'A'");
tx_str(F("A\n"));
}
// Slowly spin the extruder during C0
else {
while (planner.movesplanned() < 3) {
current_position.e += 0.25;
line_to_current_position(MMM_TO_MMS(120));
}
}
2020-04-28 04:31:59 -05:00
}
2020-05-17 21:52:45 +02:00
mmu2s_triggered = present;
}
bool MMU2::can_load() {
execute_extruder_sequence((const E_Step *)can_load_sequence, COUNT(can_load_sequence));
2020-04-28 04:31:59 -05:00
int filament_detected_count = 0;
2020-05-17 21:52:45 +02:00
const int steps = (MMU2_CAN_LOAD_RETRACT) / (MMU2_CAN_LOAD_INCREMENT);
DEBUG_ECHOLNPGM("MMU can_load:");
LOOP_L_N(i, steps) {
execute_extruder_sequence((const E_Step *)can_load_increment_sequence, COUNT(can_load_increment_sequence));
check_filament(); // Don't trust the idle function
DEBUG_CHAR(mmu2s_triggered ? 'O' : 'o');
if (mmu2s_triggered) ++filament_detected_count;
}
2020-05-17 21:52:45 +02:00
if (filament_detected_count <= steps - (MMU2_CAN_LOAD_DEVIATION) / (MMU2_CAN_LOAD_INCREMENT)) {
DEBUG_ECHOLNPGM(" failed.");
return false;
}
DEBUG_ECHOLNPGM(" succeeded.");
return true;
}
2020-11-18 08:27:21 +01:00
#endif
2020-11-18 08:27:21 +01:00
// Load filament into MMU2
void MMU2::load_filament(const uint8_t index) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return;
2020-11-18 08:27:21 +01:00
command(MMU_CMD_L0 + index);
manage_response(false, false);
BUZZ(200, 404);
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
/**
* Switch material and load to nozzle
*/
bool MMU2::load_filament_to_nozzle(const uint8_t index) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return false;
2020-11-18 08:27:21 +01:00
if (thermalManager.tooColdToExtrude(active_extruder)) {
2019-02-01 02:10:52 +01:00
BUZZ(200, 404);
2021-09-25 17:05:11 -05:00
LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
2020-11-18 08:27:21 +01:00
return false;
2019-02-01 02:10:52 +01:00
}
stepper.disable_extruder();
2020-11-18 08:27:21 +01:00
command(MMU_CMD_T0 + index);
manage_response(true, true);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
const bool success = load_to_gears();
if (success) {
mmu_loop();
extruder = index;
active_extruder = 0;
load_to_nozzle();
BUZZ(200, 404);
}
return success;
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
/**
* Load filament to nozzle of multimaterial printer
*
2021-01-13 02:38:51 +01:00
* This function is used only after T? (user select filament) and M600 (change filament).
2020-11-18 08:27:21 +01:00
* It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
* filament to nozzle.
*/
void MMU2::load_to_nozzle() {
execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
bool MMU2::eject_filament(const uint8_t index, const bool recover) {
2021-07-17 03:10:54 -05:00
if (!_enabled) return false;
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
if (thermalManager.tooColdToExtrude(active_extruder)) {
BUZZ(200, 404);
2021-09-25 17:05:11 -05:00
LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
2020-11-18 08:27:21 +01:00
return false;
2019-02-01 02:10:52 +01:00
}
2021-09-25 17:05:11 -05:00
LCD_MESSAGE(MSG_MMU2_EJECTING_FILAMENT);
2019-02-01 02:10:52 +01:00
stepper.enable_extruder();
2020-11-18 08:27:21 +01:00
current_position.e -= MMU2_FILAMENTCHANGE_EJECT_FEED;
line_to_current_position(MMM_TO_MMS(2500));
planner.synchronize();
command(MMU_CMD_E0 + index);
manage_response(false, false);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
if (recover) {
2021-09-25 17:05:11 -05:00
LCD_MESSAGE(MSG_MMU2_EJECT_RECOVER);
2020-11-18 08:27:21 +01:00
BUZZ(200, 404);
2021-09-27 11:55:08 -05:00
TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_USER_CONTINUE, F("MMU2 Eject Recover"), FPSTR(CONTINUE_STR)));
2021-09-25 22:11:48 -05:00
TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired(F("MMU2 Eject Recover")));
TERN_(HAS_RESUME_CONTINUE, wait_for_user_response());
2020-11-18 08:27:21 +01:00
BUZZ(200, 404);
BUZZ(200, 404);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
command(MMU_CMD_R0);
2019-04-08 17:56:40 -05:00
manage_response(false, false);
2020-11-18 08:27:21 +01:00
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
ui.reset_status();
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
// no active tool
extruder = MMU2_NO_TOOL;
2019-02-06 03:56:22 +01:00
2020-11-18 08:27:21 +01:00
set_runout_valid(false);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
BUZZ(200, 404);
2019-02-01 02:10:52 +01:00
stepper.disable_extruder();
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
return true;
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
/**
* Unload from hotend and retract to MMU
*/
bool MMU2::unload() {
2019-02-01 02:10:52 +01:00
2021-07-17 03:10:54 -05:00
if (!_enabled) return false;
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
if (thermalManager.tooColdToExtrude(active_extruder)) {
BUZZ(200, 404);
2021-09-25 17:05:11 -05:00
LCD_ALERTMESSAGE(MSG_HOTEND_TOO_COLD);
2020-11-18 08:27:21 +01:00
return false;
}
2019-02-01 02:10:52 +01:00
// Unload sequence to optimize shape of the tip of the unloaded filament
execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step));
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
command(MMU_CMD_U0);
manage_response(false, true);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
BUZZ(200, 404);
2019-02-06 03:56:22 +01:00
2020-11-18 08:27:21 +01:00
// no active tool
extruder = MMU2_NO_TOOL;
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
set_runout_valid(false);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
return true;
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
void MMU2::execute_extruder_sequence(const E_Step * sequence, int steps) {
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
planner.synchronize();
stepper.enable_extruder();
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
const E_Step* step = sequence;
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
LOOP_L_N(i, steps) {
const float es = pgm_read_float(&(step->extrude));
const feedRate_t fr_mm_m = pgm_read_float(&(step->feedRate));
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
DEBUG_ECHO_START();
2021-09-09 04:57:05 -05:00
DEBUG_ECHOLNPGM("E step ", es, "/", fr_mm_m);
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
current_position.e += es;
line_to_current_position(MMM_TO_MMS(fr_mm_m));
planner.synchronize();
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
step++;
2019-02-01 02:10:52 +01:00
}
stepper.disable_extruder();
2020-11-18 08:27:21 +01:00
}
2019-02-01 02:10:52 +01:00
2020-11-18 08:27:21 +01:00
#endif // HAS_PRUSA_MMU2