mmu.cpp 37 KB

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