mmu.cpp 39 KB

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