mmu.cpp 38 KB

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