mmu.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. //mmu.cpp
  2. #include "mmu.h"
  3. #include "planner.h"
  4. #include "language.h"
  5. #include "lcd.h"
  6. #include "uart2.h"
  7. #include "temperature.h"
  8. #include "Configuration_prusa.h"
  9. #include "fsensor.h"
  10. #include "cardreader.h"
  11. #include "ultralcd.h"
  12. #include "sound.h"
  13. #include "printers.h"
  14. #include <avr/pgmspace.h>
  15. #include "io_atmega2560.h"
  16. #ifdef TMC2130
  17. #include "tmc2130.h"
  18. #endif //TMC2130
  19. #define CHECK_FINDA ((IS_SD_PRINTING || is_usb_printing) && (mcode_in_progress != 600) && !saved_printing && e_active())
  20. #define MMU_TODELAY 100
  21. #define MMU_TIMEOUT 10
  22. #define MMU_CMD_TIMEOUT 300000ul //5min timeout for mmu commands (except P0)
  23. #define MMU_P0_TIMEOUT 3000ul //timeout for P0 command: 3seconds
  24. #ifdef MMU_HWRESET
  25. #define MMU_RST_PIN 76
  26. #endif //MMU_HWRESET
  27. bool mmu_enabled = false;
  28. bool mmu_ready = false;
  29. bool mmu_fil_loaded = false; //if true: blocks execution of duplicit T-codes
  30. static int8_t mmu_state = 0;
  31. uint8_t mmu_cmd = 0;
  32. #ifdef MMU_IDLER_SENSOR_PIN
  33. uint8_t mmu_idl_sens = 0;
  34. #endif //MMU_IDLER_SENSOR_PIN
  35. uint8_t mmu_extruder = MMU_FILAMENT_UNKNOWN;
  36. //! This variable probably has no meaning and is planed to be removed
  37. uint8_t tmp_extruder = MMU_FILAMENT_UNKNOWN;
  38. int8_t mmu_finda = -1;
  39. int16_t mmu_version = -1;
  40. int16_t mmu_buildnr = -1;
  41. uint32_t mmu_last_request = 0;
  42. uint32_t mmu_last_response = 0;
  43. //clear rx buffer
  44. void mmu_clr_rx_buf(void)
  45. {
  46. while (fgetc(uart2io) >= 0);
  47. }
  48. //send command - puts
  49. int mmu_puts_P(const char* str)
  50. {
  51. mmu_clr_rx_buf(); //clear rx buffer
  52. int r = fputs_P(str, uart2io); //send command
  53. mmu_last_request = millis();
  54. return r;
  55. }
  56. //send command - printf
  57. int mmu_printf_P(const char* format, ...)
  58. {
  59. va_list args;
  60. va_start(args, format);
  61. mmu_clr_rx_buf(); //clear rx buffer
  62. int r = vfprintf_P(uart2io, format, args); //send command
  63. va_end(args);
  64. mmu_last_request = millis();
  65. return r;
  66. }
  67. //check 'ok' response
  68. int8_t mmu_rx_ok(void)
  69. {
  70. int8_t res = uart2_rx_str_P(PSTR("ok\n"));
  71. if (res == 1) mmu_last_response = millis();
  72. return res;
  73. }
  74. //check 'start' response
  75. int8_t mmu_rx_start(void)
  76. {
  77. int8_t res = uart2_rx_str_P(PSTR("start\n"));
  78. if (res == 1) mmu_last_response = millis();
  79. return res;
  80. }
  81. //initialize mmu2 unit - first part - should be done at begining of startup process
  82. void mmu_init(void)
  83. {
  84. #ifdef MMU_HWRESET
  85. digitalWrite(MMU_RST_PIN, HIGH);
  86. pinMode(MMU_RST_PIN, OUTPUT); //setup reset pin
  87. #endif //MMU_HWRESET
  88. uart2_init(); //init uart2
  89. _delay_ms(10); //wait 10ms for sure
  90. mmu_reset(); //reset mmu (HW or SW), do not wait for response
  91. mmu_state = -1;
  92. #ifdef MMU_IDLER_SENSOR_PIN
  93. PIN_INP(MMU_IDLER_SENSOR_PIN); //input mode
  94. PIN_SET(MMU_IDLER_SENSOR_PIN); //pullup
  95. #endif //MMU_IDLER_SENSOR_PIN
  96. }
  97. //mmu main loop - state machine processing
  98. void mmu_loop(void)
  99. {
  100. int filament = 0;
  101. // printf_P(PSTR("MMU loop, state=%d\n"), mmu_state);
  102. switch (mmu_state)
  103. {
  104. case 0:
  105. return;
  106. case -1:
  107. if (mmu_rx_start() > 0)
  108. {
  109. #ifdef MMU_DEBUG
  110. puts_P(PSTR("MMU => 'start'"));
  111. puts_P(PSTR("MMU <= 'S1'"));
  112. #endif //MMU_DEBUG
  113. mmu_puts_P(PSTR("S1\n")); //send 'read version' request
  114. mmu_state = -2;
  115. }
  116. else if (millis() > 30000) //30sec after reset disable mmu
  117. {
  118. puts_P(PSTR("MMU not responding - DISABLED"));
  119. mmu_state = 0;
  120. }
  121. return;
  122. case -2:
  123. if (mmu_rx_ok() > 0)
  124. {
  125. fscanf_P(uart2io, PSTR("%u"), &mmu_version); //scan version from buffer
  126. #ifdef MMU_DEBUG
  127. printf_P(PSTR("MMU => '%dok'\n"), mmu_version);
  128. puts_P(PSTR("MMU <= 'S2'"));
  129. #endif //MMU_DEBUG
  130. mmu_puts_P(PSTR("S2\n")); //send 'read buildnr' request
  131. mmu_state = -3;
  132. }
  133. return;
  134. case -3:
  135. if (mmu_rx_ok() > 0)
  136. {
  137. fscanf_P(uart2io, PSTR("%u"), &mmu_buildnr); //scan buildnr from buffer
  138. #ifdef MMU_DEBUG
  139. printf_P(PSTR("MMU => '%dok'\n"), mmu_buildnr);
  140. #endif //MMU_DEBUG
  141. bool version_valid = mmu_check_version();
  142. if (!version_valid) mmu_show_warning();
  143. else puts_P(PSTR("MMU version valid"));
  144. if ((PRINTER_TYPE == PRINTER_MK3) || (PRINTER_TYPE == PRINTER_MK3_SNMM))
  145. {
  146. #ifdef MMU_DEBUG
  147. puts_P(PSTR("MMU <= 'P0'"));
  148. #endif //MMU_DEBUG
  149. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  150. mmu_state = -4;
  151. }
  152. else
  153. {
  154. #ifdef MMU_DEBUG
  155. puts_P(PSTR("MMU <= 'M1'"));
  156. #endif //MMU_DEBUG
  157. mmu_puts_P(PSTR("M1\n")); //set mmu mode to stealth
  158. mmu_state = -5;
  159. }
  160. }
  161. return;
  162. case -5:
  163. if (mmu_rx_ok() > 0)
  164. {
  165. #ifdef MMU_DEBUG
  166. puts_P(PSTR("MMU <= 'P0'"));
  167. #endif //MMU_DEBUG
  168. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  169. mmu_state = -4;
  170. }
  171. return;
  172. case -4:
  173. if (mmu_rx_ok() > 0)
  174. {
  175. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  176. #ifdef MMU_DEBUG
  177. printf_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  178. #endif //MMU_DEBUG
  179. puts_P(PSTR("MMU - ENABLED"));
  180. mmu_enabled = true;
  181. mmu_state = 1;
  182. }
  183. return;
  184. case 1:
  185. if (mmu_cmd) //command request ?
  186. {
  187. if ((mmu_cmd >= MMU_CMD_T0) && (mmu_cmd <= MMU_CMD_T4))
  188. {
  189. filament = mmu_cmd - MMU_CMD_T0;
  190. #ifdef MMU_DEBUG
  191. printf_P(PSTR("MMU <= 'T%d'\n"), filament);
  192. #endif //MMU_DEBUG
  193. mmu_printf_P(PSTR("T%d\n"), filament);
  194. mmu_state = 3; // wait for response
  195. mmu_fil_loaded = true;
  196. #ifdef MMU_IDLER_SENSOR_PIN
  197. mmu_idl_sens = 1; //enable idler sensor
  198. #endif //MMU_IDLER_SENSOR_PIN
  199. }
  200. else if ((mmu_cmd >= MMU_CMD_L0) && (mmu_cmd <= MMU_CMD_L4))
  201. {
  202. filament = mmu_cmd - MMU_CMD_L0;
  203. #ifdef MMU_DEBUG
  204. printf_P(PSTR("MMU <= 'L%d'\n"), filament);
  205. #endif //MMU_DEBUG
  206. mmu_printf_P(PSTR("L%d\n"), filament);
  207. mmu_state = 3; // wait for response
  208. }
  209. else if (mmu_cmd == MMU_CMD_C0)
  210. {
  211. #ifdef MMU_DEBUG
  212. printf_P(PSTR("MMU <= 'C0'\n"));
  213. #endif //MMU_DEBUG
  214. mmu_puts_P(PSTR("C0\n")); //send 'continue loading'
  215. mmu_state = 3;
  216. #ifdef MMU_IDLER_SENSOR_PIN
  217. mmu_idl_sens = 1; //enable idler sensor
  218. #endif //MMU_IDLER_SENSOR_PIN
  219. }
  220. else if (mmu_cmd == MMU_CMD_U0)
  221. {
  222. #ifdef MMU_DEBUG
  223. printf_P(PSTR("MMU <= 'U0'\n"));
  224. #endif //MMU_DEBUG
  225. mmu_puts_P(PSTR("U0\n")); //send 'unload current filament'
  226. mmu_fil_loaded = false;
  227. mmu_state = 3;
  228. }
  229. else if ((mmu_cmd >= MMU_CMD_E0) && (mmu_cmd <= MMU_CMD_E4))
  230. {
  231. int filament = mmu_cmd - MMU_CMD_E0;
  232. #ifdef MMU_DEBUG
  233. printf_P(PSTR("MMU <= 'E%d'\n"), filament);
  234. #endif //MMU_DEBUG
  235. mmu_printf_P(PSTR("E%d\n"), filament); //send eject filament
  236. mmu_fil_loaded = false;
  237. mmu_state = 3; // wait for response
  238. }
  239. else if (mmu_cmd == MMU_CMD_R0)
  240. {
  241. #ifdef MMU_DEBUG
  242. printf_P(PSTR("MMU <= 'R0'\n"));
  243. #endif //MMU_DEBUG
  244. mmu_puts_P(PSTR("R0\n")); //send recover after eject
  245. mmu_state = 3; // wait for response
  246. }
  247. mmu_cmd = 0;
  248. }
  249. else if ((mmu_last_response + 300) < millis()) //request every 300ms
  250. {
  251. #ifdef MMU_DEBUG
  252. puts_P(PSTR("MMU <= 'P0'"));
  253. #endif //MMU_DEBUG
  254. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  255. mmu_state = 2;
  256. }
  257. return;
  258. case 2: //response to command P0
  259. if (mmu_rx_ok() > 0)
  260. {
  261. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  262. #ifdef MMU_DEBUG
  263. printf_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  264. #endif //MMU_DEBUG
  265. //printf_P(PSTR("Eact: %d\n"), int(e_active()));
  266. if (!mmu_finda && CHECK_FINDA && fsensor_enabled) {
  267. fsensor_stop_and_save_print();
  268. enquecommand_front_P(PSTR("FSENSOR_RECOVER")); //then recover
  269. if (lcd_autoDepleteEnabled()) enquecommand_front_P(PSTR("M600 AUTO")); //save print and run M600 command
  270. else enquecommand_front_P(PSTR("M600")); //save print and run M600 command
  271. }
  272. mmu_state = 1;
  273. if (mmu_cmd == 0)
  274. mmu_ready = true;
  275. }
  276. else if ((mmu_last_request + MMU_P0_TIMEOUT) < millis())
  277. { //resend request after timeout (30s)
  278. mmu_state = 1;
  279. }
  280. return;
  281. case 3: //response to mmu commands
  282. #ifdef MMU_IDLER_SENSOR_PIN
  283. if (mmu_idl_sens)
  284. {
  285. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0)
  286. {
  287. #ifdef MMU_DEBUG
  288. printf_P(PSTR("MMU <= 'A'\n"));
  289. #endif //MMU_DEBUG
  290. mmu_puts_P(PSTR("A\n")); //send 'abort' request
  291. mmu_idl_sens = 0;
  292. //printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
  293. }
  294. //else
  295. //printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
  296. }
  297. #endif //MMU_IDLER_SENSOR_PIN
  298. if (mmu_rx_ok() > 0)
  299. {
  300. #ifdef MMU_DEBUG
  301. printf_P(PSTR("MMU => 'ok'\n"));
  302. #endif //MMU_DEBUG
  303. mmu_ready = true;
  304. mmu_state = 1;
  305. }
  306. else if ((mmu_last_request + MMU_CMD_TIMEOUT) < millis())
  307. { //resend request after timeout (5 min)
  308. mmu_state = 1;
  309. }
  310. return;
  311. }
  312. }
  313. void mmu_reset(void)
  314. {
  315. #ifdef MMU_HWRESET //HW - pulse reset pin
  316. digitalWrite(MMU_RST_PIN, LOW);
  317. _delay_us(100);
  318. digitalWrite(MMU_RST_PIN, HIGH);
  319. #else //SW - send X0 command
  320. mmu_puts_P(PSTR("X0\n"));
  321. #endif
  322. }
  323. int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament)
  324. {
  325. printf_P(PSTR("MMU <= 'F%d %d'\n"), extruder, filament);
  326. mmu_printf_P(PSTR("F%d %d\n"), extruder, filament);
  327. unsigned char timeout = MMU_TIMEOUT; //10x100ms
  328. while ((mmu_rx_ok() <= 0) && (--timeout))
  329. delay_keep_alive(MMU_TODELAY);
  330. return timeout?1:0;
  331. }
  332. void mmu_command(uint8_t cmd)
  333. {
  334. #ifdef TMC2130
  335. if ((cmd >= MMU_CMD_T0) && (cmd <= MMU_CMD_T4))
  336. {
  337. //disable extruder motor
  338. tmc2130_set_pwr(E_AXIS, 0);
  339. //printf_P(PSTR("E-axis disabled\n"));
  340. }
  341. #endif //TMC2130
  342. mmu_cmd = cmd;
  343. mmu_ready = false;
  344. }
  345. void mmu_load_step() {
  346. current_position[E_AXIS] = current_position[E_AXIS] + MMU_LOAD_FEEDRATE * 0.1;
  347. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  348. st_synchronize();
  349. }
  350. bool mmu_get_response(uint8_t move)
  351. {
  352. printf_P(PSTR("mmu_get_response - begin move:%d\n"), move);
  353. KEEPALIVE_STATE(IN_PROCESS);
  354. while (mmu_cmd != 0)
  355. {
  356. // mmu_loop();
  357. delay_keep_alive(100);
  358. }
  359. while (!mmu_ready)
  360. {
  361. // mmu_loop();
  362. if (mmu_state != 3)
  363. break;
  364. switch (move) {
  365. case MMU_LOAD_MOVE:
  366. mmu_load_step();
  367. break;
  368. case MMU_UNLOAD_MOVE:
  369. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0) //filament is still detected by idler sensor, printer helps with unlading
  370. {
  371. printf_P(PSTR("Unload 1\n"));
  372. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  373. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  374. st_synchronize();
  375. }
  376. else //filament was unloaded from idler, no additional movements needed
  377. {
  378. printf_P(PSTR("Unloading finished 1\n"));
  379. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  380. move = MMU_NO_MOVE;
  381. }
  382. break;
  383. case MMU_TCODE_MOVE: //first do unload and then continue with infinite loading movements
  384. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0) //filament detected by idler sensor, we must unload first
  385. {
  386. printf_P(PSTR("Unload 2\n"));
  387. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  388. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  389. st_synchronize();
  390. }
  391. else //delay to allow mmu unit to pull out filament from bondtech gears and then start with infinite loading
  392. {
  393. printf_P(PSTR("Unloading finished 2\n"));
  394. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  395. delay_keep_alive(MMU_LOAD_TIME_MS);
  396. move = MMU_LOAD_MOVE;
  397. }
  398. break;
  399. case MMU_NO_MOVE:
  400. default:
  401. delay_keep_alive(100);
  402. break;
  403. }
  404. }
  405. bool ret = mmu_ready;
  406. mmu_ready = false;
  407. // printf_P(PSTR("mmu_get_response - end %d\n"), ret?1:0);
  408. return ret;
  409. /* //waits for "ok" from mmu
  410. //function returns true if "ok" was received
  411. //if timeout is set to true function return false if there is no "ok" received before timeout
  412. bool response = true;
  413. LongTimer mmu_get_reponse_timeout;
  414. KEEPALIVE_STATE(IN_PROCESS);
  415. mmu_get_reponse_timeout.start();
  416. while (mmu_rx_ok() <= 0)
  417. {
  418. delay_keep_alive(100);
  419. if (timeout && mmu_get_reponse_timeout.expired(5 * 60 * 1000ul))
  420. { //5 minutes timeout
  421. response = false;
  422. break;
  423. }
  424. }
  425. printf_P(PSTR("mmu_get_response - end %d\n"), response?1:0);
  426. return response;*/
  427. }
  428. void manage_response(bool move_axes, bool turn_off_nozzle, uint8_t move)
  429. {
  430. bool response = false;
  431. mmu_print_saved = false;
  432. bool lcd_update_was_enabled = false;
  433. float hotend_temp_bckp = degTargetHotend(active_extruder);
  434. float z_position_bckp = current_position[Z_AXIS];
  435. float x_position_bckp = current_position[X_AXIS];
  436. float y_position_bckp = current_position[Y_AXIS];
  437. uint8_t screen = 0; //used for showing multiscreen messages
  438. while(!response)
  439. {
  440. response = mmu_get_response(move); //wait for "ok" from mmu
  441. if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit
  442. if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
  443. eeprom_update_byte((uint8_t*)EEPROM_MMU_FAIL, eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL) + 1);
  444. eeprom_update_word((uint16_t*)EEPROM_MMU_FAIL_TOT, eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT) + 1);
  445. if (lcd_update_enabled) {
  446. lcd_update_was_enabled = true;
  447. lcd_update_enable(false);
  448. }
  449. st_synchronize();
  450. mmu_print_saved = true;
  451. printf_P(PSTR("MMU not responding\n"));
  452. hotend_temp_bckp = degTargetHotend(active_extruder);
  453. if (move_axes) {
  454. z_position_bckp = current_position[Z_AXIS];
  455. x_position_bckp = current_position[X_AXIS];
  456. y_position_bckp = current_position[Y_AXIS];
  457. //lift z
  458. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  459. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  460. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  461. st_synchronize();
  462. //Move XY to side
  463. current_position[X_AXIS] = X_PAUSE_POS;
  464. current_position[Y_AXIS] = Y_PAUSE_POS;
  465. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  466. st_synchronize();
  467. }
  468. if (turn_off_nozzle) {
  469. //set nozzle target temperature to 0
  470. setAllTargetHotends(0);
  471. }
  472. }
  473. //first three lines are used for printing multiscreen message; last line contains measured and target nozzle temperature
  474. if (screen == 0) { //screen 0
  475. lcd_display_message_fullscreen_P(_i("MMU needs user attention."));
  476. screen++;
  477. }
  478. else { //screen 1
  479. if((degTargetHotend(active_extruder) == 0) && turn_off_nozzle) lcd_display_message_fullscreen_P(_i("Press the knob to resume nozzle temperature."));
  480. else lcd_display_message_fullscreen_P(_i("Fix the issue and then press button on MMU unit."));
  481. screen=0;
  482. }
  483. lcd_set_degree();
  484. lcd_set_cursor(0, 4); //line 4
  485. //Print the hotend temperature (9 chars total) and fill rest of the line with space
  486. int chars = lcd_printf_P(_N("%c%3d/%d%c"), LCD_STR_THERMOMETER[0],(int)(degHotend(active_extruder) + 0.5), (int)(degTargetHotend(active_extruder) + 0.5), LCD_STR_DEGREE[0]);
  487. lcd_space(9 - chars);
  488. //5 seconds delay
  489. for (uint8_t i = 0; i < 50; i++) {
  490. if (lcd_clicked()) {
  491. setTargetHotend(hotend_temp_bckp, active_extruder);
  492. break;
  493. }
  494. delay_keep_alive(100);
  495. }
  496. }
  497. else if (mmu_print_saved) {
  498. printf_P(PSTR("MMU starts responding\n"));
  499. if (turn_off_nozzle)
  500. {
  501. lcd_clear();
  502. setTargetHotend(hotend_temp_bckp, active_extruder);
  503. if (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
  504. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature..."));
  505. delay_keep_alive(3000);
  506. }
  507. while ((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)
  508. {
  509. delay_keep_alive(1000);
  510. lcd_wait_for_heater();
  511. }
  512. }
  513. if (move_axes) {
  514. lcd_clear();
  515. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming position..."));
  516. current_position[X_AXIS] = x_position_bckp;
  517. current_position[Y_AXIS] = y_position_bckp;
  518. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  519. st_synchronize();
  520. current_position[Z_AXIS] = z_position_bckp;
  521. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  522. st_synchronize();
  523. }
  524. else {
  525. lcd_clear();
  526. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming..."));
  527. delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
  528. }
  529. }
  530. }
  531. if (lcd_update_was_enabled) lcd_update_enable(true);
  532. #ifdef TMC2130
  533. //enable extruder motor (disabled in mmu_command, start of T-code processing)
  534. tmc2130_set_pwr(E_AXIS, 1);
  535. //printf_P(PSTR("E-axis enabled\n"));
  536. #endif //TMC2130
  537. }
  538. //! @brief load filament to nozzle of multimaterial printer
  539. //!
  540. //! This function is used only only after T? (user select filament) and M600 (change filament).
  541. //! It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
  542. //! filament to nozzle.
  543. //!
  544. void mmu_load_to_nozzle()
  545. {
  546. st_synchronize();
  547. bool saved_e_relative_mode = axis_relative_modes[E_AXIS];
  548. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = true;
  549. current_position[E_AXIS] += 7.2f;
  550. float feedrate = 562;
  551. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  552. st_synchronize();
  553. current_position[E_AXIS] += 14.4f;
  554. feedrate = 871;
  555. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  556. st_synchronize();
  557. current_position[E_AXIS] += 36.0f;
  558. feedrate = 1393;
  559. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  560. st_synchronize();
  561. current_position[E_AXIS] += 14.4f;
  562. feedrate = 871;
  563. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  564. st_synchronize();
  565. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = false;
  566. }
  567. void mmu_M600_wait_and_beep() {
  568. //Beep and wait for user to remove old filament and prepare new filament for load
  569. KEEPALIVE_STATE(PAUSED_FOR_USER);
  570. int counterBeep = 0;
  571. lcd_display_message_fullscreen_P(_i("Remove old filament and press the knob to start loading new filament."));
  572. bool bFirst=true;
  573. while (!lcd_clicked()){
  574. manage_heater();
  575. manage_inactivity(true);
  576. #if BEEPER > 0
  577. if (counterBeep == 500) {
  578. counterBeep = 0;
  579. }
  580. SET_OUTPUT(BEEPER);
  581. if (counterBeep == 0) {
  582. if((eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
  583. {
  584. bFirst=false;
  585. WRITE(BEEPER, HIGH);
  586. }
  587. }
  588. if (counterBeep == 20) {
  589. WRITE(BEEPER, LOW);
  590. }
  591. counterBeep++;
  592. #endif //BEEPER > 0
  593. delay_keep_alive(4);
  594. }
  595. WRITE(BEEPER, LOW);
  596. }
  597. void mmu_M600_load_filament(bool automatic)
  598. {
  599. //load filament for mmu v2
  600. tmp_extruder = mmu_extruder;
  601. if (!automatic) {
  602. #ifdef MMU_M600_SWITCH_EXTRUDER
  603. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  604. if(yes) tmp_extruder = choose_extruder_menu();
  605. #endif //MMU_M600_SWITCH_EXTRUDER
  606. }
  607. else {
  608. tmp_extruder = (tmp_extruder+1)%5;
  609. }
  610. lcd_update_enable(false);
  611. lcd_clear();
  612. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  613. lcd_print(" ");
  614. lcd_print(tmp_extruder + 1);
  615. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  616. // printf_P(PSTR("T code: %d \n"), tmp_extruder);
  617. // mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  618. mmu_command(MMU_CMD_T0 + tmp_extruder);
  619. manage_response(false, true, MMU_LOAD_MOVE);
  620. mmu_continue_loading();
  621. mmu_extruder = tmp_extruder; //filament change is finished
  622. mmu_load_to_nozzle();
  623. load_filament_final_feed();
  624. st_synchronize();
  625. }
  626. #ifdef SNMM
  627. void extr_mov(float shift, float feed_rate)
  628. { //move extruder no matter what the current heater temperature is
  629. set_extrude_min_temp(.0);
  630. current_position[E_AXIS] += shift;
  631. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feed_rate, active_extruder);
  632. set_extrude_min_temp(EXTRUDE_MINTEMP);
  633. }
  634. #endif //SNMM
  635. void change_extr(int
  636. #ifdef SNMM
  637. extr
  638. #endif //SNMM
  639. ) { //switches multiplexer for extruders
  640. #ifdef SNMM
  641. st_synchronize();
  642. delay(100);
  643. disable_e0();
  644. disable_e1();
  645. disable_e2();
  646. mmu_extruder = extr;
  647. pinMode(E_MUX0_PIN, OUTPUT);
  648. pinMode(E_MUX1_PIN, OUTPUT);
  649. switch (extr) {
  650. case 1:
  651. WRITE(E_MUX0_PIN, HIGH);
  652. WRITE(E_MUX1_PIN, LOW);
  653. break;
  654. case 2:
  655. WRITE(E_MUX0_PIN, LOW);
  656. WRITE(E_MUX1_PIN, HIGH);
  657. break;
  658. case 3:
  659. WRITE(E_MUX0_PIN, HIGH);
  660. WRITE(E_MUX1_PIN, HIGH);
  661. break;
  662. default:
  663. WRITE(E_MUX0_PIN, LOW);
  664. WRITE(E_MUX1_PIN, LOW);
  665. break;
  666. }
  667. delay(100);
  668. #endif
  669. }
  670. int get_ext_nr()
  671. { //reads multiplexer input pins and return current extruder number (counted from 0)
  672. #ifndef SNMM
  673. return(mmu_extruder); //update needed
  674. #else
  675. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  676. #endif
  677. }
  678. void display_loading()
  679. {
  680. switch (mmu_extruder)
  681. {
  682. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  683. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  684. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  685. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  686. }
  687. }
  688. void extr_adj(int extruder) //loading filament for SNMM
  689. {
  690. #ifndef SNMM
  691. uint8_t cmd = MMU_CMD_L0 + extruder;
  692. if (cmd > MMU_CMD_L4)
  693. {
  694. printf_P(PSTR("Filament out of range %d \n"),extruder);
  695. return;
  696. }
  697. mmu_command(cmd);
  698. //show which filament is currently loaded
  699. lcd_update_enable(false);
  700. lcd_clear();
  701. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  702. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  703. //else lcd.print(" ");
  704. lcd_print(" ");
  705. lcd_print(extruder + 1);
  706. // get response
  707. manage_response(false, false);
  708. lcd_update_enable(true);
  709. //lcd_return_to_status();
  710. #else
  711. bool correct;
  712. max_feedrate[E_AXIS] =80;
  713. //max_feedrate[E_AXIS] = 50;
  714. START:
  715. lcd_clear();
  716. lcd_set_cursor(0, 0);
  717. switch (extruder) {
  718. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  719. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  720. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  721. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  722. }
  723. KEEPALIVE_STATE(PAUSED_FOR_USER);
  724. do{
  725. extr_mov(0.001,1000);
  726. delay_keep_alive(2);
  727. } while (!lcd_clicked());
  728. //delay_keep_alive(500);
  729. KEEPALIVE_STATE(IN_HANDLER);
  730. st_synchronize();
  731. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  732. //if (!correct) goto START;
  733. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  734. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  735. extr_mov(bowden_length[extruder], 500);
  736. lcd_clear();
  737. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  738. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  739. else lcd_print(" ");
  740. lcd_print(mmu_extruder + 1);
  741. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  742. st_synchronize();
  743. max_feedrate[E_AXIS] = 50;
  744. lcd_update_enable(true);
  745. lcd_return_to_status();
  746. lcdDrawUpdate = 2;
  747. #endif
  748. }
  749. struct E_step
  750. {
  751. float extrude; //!< extrude distance in mm
  752. float feed_rate; //!< feed rate in mm/s
  753. };
  754. static const E_step ramming_sequence[] PROGMEM =
  755. {
  756. {1.0, 1000.0/60},
  757. {1.0, 1500.0/60},
  758. {2.0, 2000.0/60},
  759. {1.5, 3000.0/60},
  760. {2.5, 4000.0/60},
  761. {-15.0, 5000.0/60},
  762. {-14.0, 1200.0/60},
  763. {-6.0, 600.0/60},
  764. {10.0, 700.0/60},
  765. {-10.0, 400.0/60},
  766. {-50.0, 2000.0/60},
  767. };
  768. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  769. static void filament_ramming()
  770. {
  771. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  772. {
  773. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  774. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  775. current_position[E_AXIS], pgm_read_float(&(ramming_sequence[i].feed_rate)), active_extruder);
  776. st_synchronize();
  777. }
  778. }
  779. void extr_unload()
  780. { //unload just current filament for multimaterial printers
  781. #ifdef SNMM
  782. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  783. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  784. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  785. #endif
  786. if (degHotend0() > EXTRUDE_MINTEMP)
  787. {
  788. #ifndef SNMM
  789. st_synchronize();
  790. //show which filament is currently unloaded
  791. lcd_update_enable(false);
  792. lcd_clear();
  793. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  794. lcd_print(" ");
  795. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(" ");
  796. else lcd_print(mmu_extruder + 1);
  797. filament_ramming();
  798. mmu_command(MMU_CMD_U0);
  799. // get response
  800. manage_response(false, true, MMU_UNLOAD_MOVE);
  801. lcd_update_enable(true);
  802. #else //SNMM
  803. lcd_clear();
  804. lcd_display_message_fullscreen_P(PSTR(""));
  805. max_feedrate[E_AXIS] = 50;
  806. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  807. lcd_print(" ");
  808. lcd_print(mmu_extruder + 1);
  809. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  810. if (current_position[Z_AXIS] < 15) {
  811. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  812. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  813. }
  814. current_position[E_AXIS] += 10; //extrusion
  815. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  816. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  817. if (current_temperature[0] < 230) { //PLA & all other filaments
  818. current_position[E_AXIS] += 5.4;
  819. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  820. current_position[E_AXIS] += 3.2;
  821. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  822. current_position[E_AXIS] += 3;
  823. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  824. }
  825. else { //ABS
  826. current_position[E_AXIS] += 3.1;
  827. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  828. current_position[E_AXIS] += 3.1;
  829. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  830. current_position[E_AXIS] += 4;
  831. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  832. /*current_position[X_AXIS] += 23; //delay
  833. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay
  834. current_position[X_AXIS] -= 23; //delay
  835. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  836. delay_keep_alive(4700);
  837. }
  838. max_feedrate[E_AXIS] = 80;
  839. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  840. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  841. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  842. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  843. st_synchronize();
  844. //st_current_init();
  845. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  846. else st_current_set(2, tmp_motor_loud[2]);
  847. lcd_update_enable(true);
  848. lcd_return_to_status();
  849. max_feedrate[E_AXIS] = 50;
  850. #endif //SNMM
  851. }
  852. else
  853. {
  854. show_preheat_nozzle_warning();
  855. }
  856. //lcd_return_to_status();
  857. }
  858. //wrapper functions for loading filament
  859. void extr_adj_0()
  860. {
  861. #ifndef SNMM
  862. enquecommand_P(PSTR("M701 E0"));
  863. #else
  864. change_extr(0);
  865. extr_adj(0);
  866. #endif
  867. }
  868. void extr_adj_1()
  869. {
  870. #ifndef SNMM
  871. enquecommand_P(PSTR("M701 E1"));
  872. #else
  873. change_extr(1);
  874. extr_adj(1);
  875. #endif
  876. }
  877. void extr_adj_2()
  878. {
  879. #ifndef SNMM
  880. enquecommand_P(PSTR("M701 E2"));
  881. #else
  882. change_extr(2);
  883. extr_adj(2);
  884. #endif
  885. }
  886. void extr_adj_3()
  887. {
  888. #ifndef SNMM
  889. enquecommand_P(PSTR("M701 E3"));
  890. #else
  891. change_extr(3);
  892. extr_adj(3);
  893. #endif
  894. }
  895. void extr_adj_4()
  896. {
  897. #ifndef SNMM
  898. enquecommand_P(PSTR("M701 E4"));
  899. #else
  900. change_extr(4);
  901. extr_adj(4);
  902. #endif
  903. }
  904. void mmu_load_to_nozzle_0()
  905. {
  906. lcd_mmu_load_to_nozzle(0);
  907. }
  908. void mmu_load_to_nozzle_1()
  909. {
  910. lcd_mmu_load_to_nozzle(1);
  911. }
  912. void mmu_load_to_nozzle_2()
  913. {
  914. lcd_mmu_load_to_nozzle(2);
  915. }
  916. void mmu_load_to_nozzle_3()
  917. {
  918. lcd_mmu_load_to_nozzle(3);
  919. }
  920. void mmu_load_to_nozzle_4()
  921. {
  922. lcd_mmu_load_to_nozzle(4);
  923. }
  924. void mmu_eject_fil_0()
  925. {
  926. mmu_eject_filament(0, true);
  927. }
  928. void mmu_eject_fil_1()
  929. {
  930. mmu_eject_filament(1, true);
  931. }
  932. void mmu_eject_fil_2()
  933. {
  934. mmu_eject_filament(2, true);
  935. }
  936. void mmu_eject_fil_3()
  937. {
  938. mmu_eject_filament(3, true);
  939. }
  940. void mmu_eject_fil_4()
  941. {
  942. mmu_eject_filament(4, true);
  943. }
  944. void load_all()
  945. {
  946. #ifndef SNMM
  947. enquecommand_P(PSTR("M701 E0"));
  948. enquecommand_P(PSTR("M701 E1"));
  949. enquecommand_P(PSTR("M701 E2"));
  950. enquecommand_P(PSTR("M701 E3"));
  951. enquecommand_P(PSTR("M701 E4"));
  952. #else
  953. for (int i = 0; i < 4; i++)
  954. {
  955. change_extr(i);
  956. extr_adj(i);
  957. }
  958. #endif
  959. }
  960. //wrapper functions for changing extruders
  961. void extr_change_0()
  962. {
  963. change_extr(0);
  964. lcd_return_to_status();
  965. }
  966. void extr_change_1()
  967. {
  968. change_extr(1);
  969. lcd_return_to_status();
  970. }
  971. void extr_change_2()
  972. {
  973. change_extr(2);
  974. lcd_return_to_status();
  975. }
  976. void extr_change_3()
  977. {
  978. change_extr(3);
  979. lcd_return_to_status();
  980. }
  981. #ifdef SNMM
  982. //wrapper functions for unloading filament
  983. void extr_unload_all()
  984. {
  985. if (degHotend0() > EXTRUDE_MINTEMP)
  986. {
  987. for (int i = 0; i < 4; i++)
  988. {
  989. change_extr(i);
  990. extr_unload();
  991. }
  992. }
  993. else
  994. {
  995. show_preheat_nozzle_warning();
  996. lcd_return_to_status();
  997. }
  998. }
  999. //unloading just used filament (for snmm)
  1000. void extr_unload_used()
  1001. {
  1002. if (degHotend0() > EXTRUDE_MINTEMP) {
  1003. for (int i = 0; i < 4; i++) {
  1004. if (snmm_filaments_used & (1 << i)) {
  1005. change_extr(i);
  1006. extr_unload();
  1007. }
  1008. }
  1009. snmm_filaments_used = 0;
  1010. }
  1011. else {
  1012. show_preheat_nozzle_warning();
  1013. lcd_return_to_status();
  1014. }
  1015. }
  1016. #endif //SNMM
  1017. void extr_unload_0()
  1018. {
  1019. change_extr(0);
  1020. extr_unload();
  1021. }
  1022. void extr_unload_1()
  1023. {
  1024. change_extr(1);
  1025. extr_unload();
  1026. }
  1027. void extr_unload_2()
  1028. {
  1029. change_extr(2);
  1030. extr_unload();
  1031. }
  1032. void extr_unload_3()
  1033. {
  1034. change_extr(3);
  1035. extr_unload();
  1036. }
  1037. void extr_unload_4()
  1038. {
  1039. change_extr(4);
  1040. extr_unload();
  1041. }
  1042. bool mmu_check_version()
  1043. {
  1044. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  1045. }
  1046. void mmu_show_warning()
  1047. {
  1048. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  1049. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  1050. }
  1051. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  1052. {
  1053. if (degHotend0() > EXTRUDE_MINTEMP)
  1054. {
  1055. tmp_extruder = filament_nr;
  1056. lcd_update_enable(false);
  1057. lcd_clear();
  1058. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  1059. lcd_print(" ");
  1060. lcd_print(tmp_extruder + 1);
  1061. mmu_command(MMU_CMD_T0 + tmp_extruder);
  1062. manage_response(true, true, MMU_TCODE_MOVE);
  1063. mmu_continue_loading();
  1064. mmu_extruder = tmp_extruder; //filament change is finished
  1065. mmu_load_to_nozzle();
  1066. load_filament_final_feed();
  1067. st_synchronize();
  1068. custom_message_type = CUSTOM_MSG_TYPE_F_LOAD;
  1069. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  1070. lcd_return_to_status();
  1071. lcd_update_enable(true);
  1072. lcd_load_filament_color_check();
  1073. lcd_setstatuspgm(_T(WELCOME_MSG));
  1074. custom_message_type = CUSTOM_MSG_TYPE_STATUS;
  1075. }
  1076. else
  1077. {
  1078. show_preheat_nozzle_warning();
  1079. }
  1080. }
  1081. void mmu_eject_filament(uint8_t filament, bool recover)
  1082. {
  1083. if (filament < 5)
  1084. {
  1085. if (degHotend0() > EXTRUDE_MINTEMP)
  1086. {
  1087. st_synchronize();
  1088. {
  1089. LcdUpdateDisabler disableLcdUpdate;
  1090. lcd_clear();
  1091. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  1092. current_position[E_AXIS] -= 80;
  1093. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  1094. st_synchronize();
  1095. mmu_command(MMU_CMD_E0 + filament);
  1096. manage_response(false, false, MMU_UNLOAD_MOVE);
  1097. if (recover)
  1098. {
  1099. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  1100. mmu_command(MMU_CMD_R0);
  1101. manage_response(false, false);
  1102. }
  1103. }
  1104. }
  1105. else
  1106. {
  1107. show_preheat_nozzle_warning();
  1108. }
  1109. }
  1110. else
  1111. {
  1112. puts_P(PSTR("Filament nr out of range!"));
  1113. }
  1114. }
  1115. void mmu_continue_loading()
  1116. {
  1117. #ifdef MMU_IDLER_SENSOR_PIN
  1118. for (uint8_t i = 0; i < MMU_IDLER_SENSOR_ATTEMPTS_NR; i++) {
  1119. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0) return;
  1120. #ifdef MMU_DEBUG
  1121. printf_P(PSTR("Additional load attempt nr. %d\n"), i);
  1122. #endif // MMU_DEBUG
  1123. mmu_command(MMU_CMD_C0);
  1124. manage_response(true, true, MMU_LOAD_MOVE);
  1125. }
  1126. if (PIN_GET(MMU_IDLER_SENSOR_PIN) != 0) {
  1127. eeprom_update_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL) + 1);
  1128. eeprom_update_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT) + 1);
  1129. char cmd[3];
  1130. //pause print, show error message and then repeat last T-code
  1131. stop_and_save_print_to_ram(0, 0);
  1132. //lift z
  1133. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  1134. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  1135. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  1136. st_synchronize();
  1137. //Move XY to side
  1138. current_position[X_AXIS] = X_PAUSE_POS;
  1139. current_position[Y_AXIS] = Y_PAUSE_POS;
  1140. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  1141. st_synchronize();
  1142. //set nozzle target temperature to 0
  1143. setAllTargetHotends(0);
  1144. lcd_show_fullscreen_message_and_wait_P(_i("MMU load failed, fix the issue and press the knob."));
  1145. mmu_fil_loaded = false; //so we can retry same T-code again
  1146. restore_print_from_ram_and_continue(0);
  1147. }
  1148. #else
  1149. mmu_command(MMU_CMD_C0);
  1150. #endif //MMU_IDLER_SENSOR_PIN
  1151. }