mmu.cpp 44 KB

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