mmu.cpp 37 KB

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