mmu.cpp 37 KB

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