mmu.cpp 40 KB

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