mmu.cpp 36 KB

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