mmu.cpp 40 KB

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