mmu.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 && CHECK_FSENSOR && fsensor.isReady()) {
  348. stop_and_save_print_to_ram(0, 0);
  349. restore_print_from_ram_and_continue(0);
  350. if (mmu_extruder != MMU_FILAMENT_UNKNOWN) // Can't deplete unknown extruder.
  351. ad_markDepleted(mmu_extruder);
  352. if (lcd_autoDepleteEnabled() && !ad_allDepleted() && mmu_extruder != MMU_FILAMENT_UNKNOWN) // Can't auto if F=?
  353. {
  354. enquecommand_front_P(PSTR("M600 AUTO")); //save print and run M600 command
  355. }
  356. else
  357. {
  358. enquecommand_front_P(PSTR("M600")); //save print and run M600 command
  359. }
  360. }
  361. mmu_state = S::Idle;
  362. if (mmu_cmd == MmuCmd::None)
  363. mmu_ready = true;
  364. }
  365. else if (mmu_last_request.expired(MMU_P0_TIMEOUT))
  366. { //resend request after timeout (30s)
  367. mmu_state = S::Idle;
  368. }
  369. return;
  370. case S::WaitCmd: //response to mmu commands
  371. if (mmu_idl_sens)
  372. {
  373. if (READ(IR_SENSOR_PIN) == 0 && mmu_loading_flag)
  374. {
  375. DEBUG_PRINTF_P(PSTR("MMU <= 'A'\n"));
  376. mmu_puts_P(PSTR("A\n")); //send 'abort' request
  377. mmu_idl_sens = 0;
  378. //printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
  379. }
  380. //else
  381. //printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
  382. }
  383. if (mmu_rx_ok() > 0)
  384. {
  385. DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
  386. mmu_attempt_nr = 0;
  387. mmu_last_cmd = MmuCmd::None;
  388. mmu_ready = true;
  389. mmu_state = S::Idle;
  390. }
  391. else if (mmu_last_request.expired(MMU_CMD_TIMEOUT))
  392. { //resend request after timeout (5 min)
  393. if (mmu_last_cmd != MmuCmd::None)
  394. {
  395. if (mmu_attempt_nr++ < MMU_MAX_RESEND_ATTEMPTS &&
  396. mmu_last_cmd >= MmuCmd::T0 && mmu_last_cmd <= MmuCmd::T4)
  397. {
  398. DEBUG_PRINTF_P(PSTR("MMU retry attempt nr. %d\n"), mmu_attempt_nr - 1);
  399. mmu_cmd = mmu_last_cmd;
  400. }
  401. else {
  402. mmu_cmd = MmuCmd::None;
  403. mmu_last_cmd = MmuCmd::None; //check
  404. mmu_attempt_nr = 0;
  405. }
  406. }
  407. mmu_state = S::Idle;
  408. }
  409. return;
  410. case S::Pause:
  411. if (mmu_rx_ok() > 0)
  412. {
  413. DEBUG_PRINTF_P(PSTR("MMU => 'ok', resume print\n"));
  414. mmu_attempt_nr = 0;
  415. mmu_last_cmd = MmuCmd::None;
  416. mmu_ready = true;
  417. mmu_state = S::Idle;
  418. lcd_resume_print();
  419. }
  420. if (mmu_cmd != MmuCmd::None)
  421. {
  422. mmu_state = S::Idle;
  423. }
  424. return;
  425. case S::GetDrvError:
  426. if (mmu_rx_ok() > 0)
  427. {
  428. fscanf_P(uart2io, PSTR("%d"), &mmu_power_failures); //scan power failures
  429. DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
  430. mmu_last_cmd = MmuCmd::None;
  431. mmu_ready = true;
  432. mmu_state = S::Idle;
  433. }
  434. else if (mmu_last_request.expired(MMU_CMD_TIMEOUT))
  435. { //timeout 45 s
  436. mmu_state = S::Idle;
  437. }
  438. return;
  439. case S::SwitchMode:
  440. if (mmu_rx_ok() > 0)
  441. {
  442. DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
  443. eeprom_update_byte((uint8_t*)EEPROM_MMU_STEALTH, SilentModeMenu_MMU);
  444. mmu_state = S::Idle;
  445. }
  446. else if (mmu_last_request.expired(MMU_CMD_TIMEOUT))
  447. { //timeout 45 s
  448. mmu_state = S::Idle;
  449. }
  450. return;
  451. }
  452. }
  453. void mmu_reset(void)
  454. {
  455. #ifdef MMU_HWRESET //HW - pulse reset pin
  456. WRITE(MMU_RST_PIN, 0);
  457. _delay_us(100);
  458. WRITE(MMU_RST_PIN, 1);
  459. #else //SW - send X0 command
  460. mmu_puts_P(PSTR("X0\n"));
  461. #endif
  462. }
  463. int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament)
  464. {
  465. printf_P(PSTR("MMU <= 'F%d %d'\n"), extruder, filament);
  466. mmu_printf_P(PSTR("F%d %d\n"), extruder, filament);
  467. unsigned char timeout = MMU_TIMEOUT; //10x100ms
  468. while ((mmu_rx_ok() <= 0) && (--timeout))
  469. delay_keep_alive(MMU_TODELAY);
  470. return timeout?1:0;
  471. }
  472. //! @brief Enqueue MMUv2 command
  473. //!
  474. //! Call manage_response() after enqueuing to process command.
  475. //! If T command is enqueued, it disables current for extruder motor if TMC2130 driver present.
  476. //! If T or L command is enqueued, it marks filament loaded in AutoDeplete module.
  477. void mmu_command(MmuCmd cmd)
  478. {
  479. if ((cmd >= MmuCmd::T0) && (cmd <= MmuCmd::T4))
  480. {
  481. //disable extruder motor
  482. #ifdef TMC2130
  483. tmc2130_set_pwr(E_AXIS, 0);
  484. #endif //TMC2130
  485. //printf_P(PSTR("E-axis disabled\n"));
  486. ad_markLoaded(cmd - MmuCmd::T0);
  487. }
  488. if ((cmd >= MmuCmd::L0) && (cmd <= MmuCmd::L4))
  489. {
  490. ad_markLoaded(cmd - MmuCmd::L0);
  491. }
  492. mmu_cmd = cmd;
  493. mmu_ready = false;
  494. }
  495. //! @brief Rotate extruder idler to catch filament
  496. //! @par synchronize
  497. //! * true blocking call
  498. //! * false non-blocking call
  499. void mmu_load_step(bool synchronize)
  500. {
  501. current_position[E_AXIS] = current_position[E_AXIS] + MMU_LOAD_FEEDRATE * 0.1;
  502. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  503. if (synchronize) st_synchronize();
  504. }
  505. //! @brief Is nozzle hot enough to move extruder wheels and do we have idler sensor?
  506. //!
  507. //! Do load steps only if temperature is higher then min. temp for safe extrusion and
  508. //! idler sensor present.
  509. //! Otherwise "cold extrusion prevented" would be send to serial line periodically
  510. //! and watchdog reset will be triggered by lack of keep_alive processing.
  511. //!
  512. //! @retval true temperature is high enough to move extruder
  513. //! @retval false temperature is not high enough to move extruder, turned
  514. //! off E-stepper to prevent over-heating and allow filament pull-out if necessary
  515. bool can_extrude()
  516. {
  517. if ((degHotend(active_extruder) < EXTRUDE_MINTEMP) || !ir_sensor_detected)
  518. {
  519. disable_e0();
  520. delay_keep_alive(100);
  521. return false;
  522. }
  523. return true;
  524. }
  525. static void get_response_print_info(uint8_t move) {
  526. printf_P(PSTR("mmu_get_response - begin move: "), move);
  527. switch (move) {
  528. case MMU_LOAD_MOVE: puts_P(PSTR("load")); break;
  529. case MMU_UNLOAD_MOVE: puts_P(PSTR("unload")); break;
  530. case MMU_TCODE_MOVE: puts_P(PSTR("T-code")); break;
  531. case MMU_NO_MOVE: puts_P(PSTR("no move")); break;
  532. default: puts_P(PSTR("error: unknown move")); break;
  533. }
  534. }
  535. bool mmu_get_response(uint8_t move)
  536. {
  537. get_response_print_info(move);
  538. KEEPALIVE_STATE(IN_PROCESS);
  539. while (mmu_cmd != MmuCmd::None)
  540. {
  541. delay_keep_alive(100);
  542. }
  543. while (!mmu_ready)
  544. {
  545. if ((mmu_state != S::WaitCmd) && (mmu_last_cmd == MmuCmd::None))
  546. break;
  547. switch (move) {
  548. case MMU_LOAD_MOVE:
  549. mmu_loading_flag = true;
  550. if (can_extrude()) mmu_load_step();
  551. //don't rely on "ok" signal from mmu unit; if filament detected by idler sensor during loading stop loading movements to prevent infinite loading
  552. if (READ(IR_SENSOR_PIN) == 0) move = MMU_NO_MOVE;
  553. break;
  554. case MMU_UNLOAD_MOVE:
  555. if (READ(IR_SENSOR_PIN) == 0) //filament is still detected by idler sensor, printer helps with unlading
  556. {
  557. if (can_extrude())
  558. {
  559. puts_P(PSTR("Unload 1"));
  560. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  561. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  562. st_synchronize();
  563. }
  564. }
  565. else //filament was unloaded from idler, no additional movements needed
  566. {
  567. puts_P(PSTR("Unloading finished 1"));
  568. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  569. move = MMU_NO_MOVE;
  570. }
  571. break;
  572. case MMU_TCODE_MOVE: //first do unload and then continue with infinite loading movements
  573. if (READ(IR_SENSOR_PIN) == 0) //filament detected by idler sensor, we must unload first
  574. {
  575. if (can_extrude())
  576. {
  577. puts_P(PSTR("Unload 2"));
  578. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  579. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  580. st_synchronize();
  581. }
  582. }
  583. else //delay to allow mmu unit to pull out filament from bondtech gears and then start with infinite loading
  584. {
  585. puts_P(PSTR("Unloading finished 2"));
  586. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  587. delay_keep_alive(MMU_LOAD_TIME_MS);
  588. move = MMU_LOAD_MOVE;
  589. get_response_print_info(move);
  590. }
  591. break;
  592. case MMU_NO_MOVE:
  593. default:
  594. delay_keep_alive(100);
  595. break;
  596. }
  597. }
  598. printf_P(PSTR("mmu_get_response() returning: %d\n"), mmu_ready);
  599. bool ret = mmu_ready;
  600. mmu_ready = false;
  601. // printf_P(PSTR("mmu_get_response - end %d\n"), ret?1:0);
  602. return ret;
  603. }
  604. //! @brief Wait for active extruder to reach temperature set
  605. //!
  606. //! This function is blocking and showing lcd_wait_for_heater() screen
  607. //! which is constantly updated with nozzle temperature.
  608. void mmu_wait_for_heater_blocking()
  609. {
  610. while ((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)
  611. {
  612. delay_keep_alive(1000);
  613. lcd_wait_for_heater();
  614. }
  615. }
  616. void manage_response(bool move_axes, bool turn_off_nozzle, uint8_t move)
  617. {
  618. bool response = false;
  619. mmu_print_saved = false;
  620. bool lcd_update_was_enabled = false;
  621. float hotend_temp_bckp = degTargetHotend(active_extruder);
  622. float z_position_bckp = current_position[Z_AXIS];
  623. float x_position_bckp = current_position[X_AXIS];
  624. float y_position_bckp = current_position[Y_AXIS];
  625. uint8_t screen = 0; //used for showing multiscreen messages
  626. mmu_loading_flag = false;
  627. while(!response)
  628. {
  629. response = mmu_get_response(move); //wait for "ok" from mmu
  630. if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit
  631. if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
  632. uint8_t mmu_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL);
  633. uint16_t mmu_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT);
  634. if(mmu_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_FAIL, mmu_fail + 1);
  635. if(mmu_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_FAIL_TOT, mmu_fail_tot + 1);
  636. if (lcd_update_enabled) {
  637. lcd_update_was_enabled = true;
  638. lcd_update_enable(false);
  639. }
  640. st_synchronize();
  641. mmu_print_saved = true;
  642. puts_P(PSTR("MMU not responding"));
  643. KEEPALIVE_STATE(PAUSED_FOR_USER);
  644. hotend_temp_bckp = degTargetHotend(active_extruder);
  645. if (move_axes) {
  646. z_position_bckp = current_position[Z_AXIS];
  647. x_position_bckp = current_position[X_AXIS];
  648. y_position_bckp = current_position[Y_AXIS];
  649. //lift z
  650. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  651. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  652. plan_buffer_line_curposXYZE(15);
  653. st_synchronize();
  654. //Move XY to side
  655. current_position[X_AXIS] = X_PAUSE_POS;
  656. current_position[Y_AXIS] = Y_PAUSE_POS;
  657. plan_buffer_line_curposXYZE(50);
  658. st_synchronize();
  659. }
  660. if (turn_off_nozzle) {
  661. //set nozzle target temperature to 0
  662. setAllTargetHotends(0);
  663. }
  664. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  665. }
  666. //first three lines are used for printing multiscreen message; last line contains measured and target nozzle temperature
  667. if (screen == 0) { //screen 0
  668. lcd_display_message_fullscreen_P(_i("MMU needs user attention."));////MSG_MMU_USER_ATTENTION c=20 r=3
  669. screen++;
  670. }
  671. else { //screen 1
  672. 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
  673. 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
  674. screen=0;
  675. }
  676. //5 seconds delay
  677. for (uint8_t i = 0; i < 5; i++) {
  678. if (lcd_clicked()) {
  679. setTargetHotend(hotend_temp_bckp, active_extruder);
  680. /// mmu_cmd = mmu_last_cmd;
  681. break;
  682. }
  683. //Print the hotend temperature (9 chars total) and fill rest of the line with space
  684. lcd_set_cursor(0, 4); //line 4
  685. 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]);
  686. lcd_space(9 - chars);
  687. delay_keep_alive(1000);
  688. }
  689. }
  690. else if (mmu_print_saved) {
  691. puts_P(PSTR("MMU starts responding"));
  692. KEEPALIVE_STATE(IN_HANDLER);
  693. mmu_loading_flag = false;
  694. if (turn_off_nozzle)
  695. {
  696. lcd_clear();
  697. setTargetHotend(hotend_temp_bckp, active_extruder);
  698. if (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
  699. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature...")); ////MSG_MMU_OK_RESUMING_TEMPERATURE c=20 r=4
  700. delay_keep_alive(3000);
  701. }
  702. mmu_wait_for_heater_blocking();
  703. }
  704. if (move_axes) {
  705. lcd_clear();
  706. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming position...")); ////MSG_MMU_OK_RESUMING_POSITION c=20 r=4
  707. current_position[X_AXIS] = x_position_bckp;
  708. current_position[Y_AXIS] = y_position_bckp;
  709. plan_buffer_line_curposXYZE(50);
  710. st_synchronize();
  711. current_position[Z_AXIS] = z_position_bckp;
  712. plan_buffer_line_curposXYZE(15);
  713. st_synchronize();
  714. }
  715. else {
  716. lcd_clear();
  717. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming...")); ////MSG_MMU_OK_RESUMING c=20 r=4
  718. delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
  719. }
  720. }
  721. }
  722. if (lcd_update_was_enabled) lcd_update_enable(true);
  723. #ifdef TMC2130
  724. //enable extruder motor (disabled in mmu_command, start of T-code processing)
  725. tmc2130_set_pwr(E_AXIS, 1);
  726. //printf_P(PSTR("E-axis enabled\n"));
  727. #endif //TMC2130
  728. }
  729. //! @brief load filament to nozzle of multimaterial printer
  730. //!
  731. //! This function is used only only after T? (user select filament) and M600 (change filament).
  732. //! It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
  733. //! filament to nozzle.
  734. //!
  735. void mmu_load_to_nozzle()
  736. {
  737. st_synchronize();
  738. const bool saved_e_relative_mode = axis_relative_modes & E_AXIS_MASK;
  739. if (!saved_e_relative_mode) axis_relative_modes |= E_AXIS_MASK;
  740. if (ir_sensor_detected)
  741. {
  742. current_position[E_AXIS] += 3.0f;
  743. }
  744. else
  745. {
  746. current_position[E_AXIS] += 7.2f;
  747. }
  748. float feedrate = 562;
  749. plan_buffer_line_curposXYZE(feedrate / 60);
  750. st_synchronize();
  751. current_position[E_AXIS] += 14.4f;
  752. feedrate = 871;
  753. plan_buffer_line_curposXYZE(feedrate / 60);
  754. st_synchronize();
  755. current_position[E_AXIS] += 36.0f;
  756. feedrate = 1393;
  757. plan_buffer_line_curposXYZE(feedrate / 60);
  758. st_synchronize();
  759. current_position[E_AXIS] += 14.4f;
  760. feedrate = 871;
  761. plan_buffer_line_curposXYZE(feedrate / 60);
  762. st_synchronize();
  763. if (!saved_e_relative_mode) axis_relative_modes &= ~E_AXIS_MASK;
  764. }
  765. void mmu_M600_wait_and_beep() {
  766. //Beep and wait for user to remove old filament and prepare new filament for load
  767. KEEPALIVE_STATE(PAUSED_FOR_USER);
  768. int counterBeep = 0;
  769. 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
  770. bool bFirst=true;
  771. while (!lcd_clicked()){
  772. manage_heater();
  773. manage_inactivity(true);
  774. #if BEEPER > 0
  775. if (counterBeep == 500) {
  776. counterBeep = 0;
  777. }
  778. SET_OUTPUT(BEEPER);
  779. if (counterBeep == 0) {
  780. if((eSoundMode==e_SOUND_MODE_BLIND)|| (eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
  781. {
  782. bFirst=false;
  783. WRITE(BEEPER, HIGH);
  784. }
  785. }
  786. if (counterBeep == 20) {
  787. WRITE(BEEPER, LOW);
  788. }
  789. counterBeep++;
  790. #endif //BEEPER > 0
  791. delay_keep_alive(4);
  792. }
  793. WRITE(BEEPER, LOW);
  794. }
  795. //! @brief load filament for mmu v2
  796. //! @par nozzle_temp nozzle temperature to load filament
  797. void mmu_M600_load_filament(bool automatic, float nozzle_temp)
  798. {
  799. tmp_extruder = mmu_extruder;
  800. if (automatic) {
  801. tmp_extruder = ad_getAlternative(tmp_extruder);
  802. }
  803. lcd_update_enable(false);
  804. lcd_clear();
  805. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  806. lcd_print(' ');
  807. lcd_print(tmp_extruder + 1);
  808. //printf_P(PSTR("T code: %d \n"), tmp_extruder);
  809. //mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  810. setTargetHotend(nozzle_temp,active_extruder);
  811. mmu_wait_for_heater_blocking();
  812. mmu_command(MmuCmd::T0 + tmp_extruder);
  813. manage_response(false, true, MMU_LOAD_MOVE);
  814. mmu_continue_loading(usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal));
  815. mmu_extruder = tmp_extruder; //filament change is finished
  816. mmu_load_to_nozzle();
  817. load_filament_final_feed();
  818. st_synchronize();
  819. }
  820. void extr_adj(uint8_t extruder) //loading filament into the MMU unit
  821. {
  822. MmuCmd cmd = MmuCmd::L0 + extruder;
  823. if (extruder > (MmuCmd::L4 - MmuCmd::L0))
  824. {
  825. printf_P(PSTR("Filament out of range %d \n"),extruder);
  826. return;
  827. }
  828. mmu_command(cmd);
  829. //show which filament is currently loaded
  830. lcd_update_enable(false);
  831. lcd_clear();
  832. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  833. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  834. //else lcd.print(" ");
  835. lcd_print(' ');
  836. lcd_print(extruder + 1);
  837. // get response
  838. manage_response(false, false);
  839. lcd_update_enable(true);
  840. //lcd_return_to_status();
  841. }
  842. struct E_step
  843. {
  844. float extrude; //!< extrude distance in mm
  845. float feed_rate; //!< feed rate in mm/s
  846. };
  847. static const E_step ramming_sequence[] PROGMEM =
  848. {
  849. {1.0, 1000.0/60},
  850. {1.0, 1500.0/60},
  851. {2.0, 2000.0/60},
  852. {1.5, 3000.0/60},
  853. {2.5, 4000.0/60},
  854. {-15.0, 5000.0/60},
  855. {-14.0, 1200.0/60},
  856. {-6.0, 600.0/60},
  857. {10.0, 700.0/60},
  858. {-10.0, 400.0/60},
  859. {-50.0, 2000.0/60},
  860. };
  861. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  862. void mmu_filament_ramming()
  863. {
  864. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  865. {
  866. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  867. plan_buffer_line_curposXYZE(pgm_read_float(&(ramming_sequence[i].feed_rate)));
  868. st_synchronize();
  869. }
  870. }
  871. //! @brief show which filament is currently unloaded
  872. void extr_unload_view()
  873. {
  874. lcd_clear();
  875. lcd_puts_at_P(0, 1, _T(MSG_UNLOADING_FILAMENT));
  876. lcd_print(' ');
  877. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(' ');
  878. else lcd_print(mmu_extruder + 1);
  879. }
  880. void extr_unload()
  881. { //unload just current filament for multimaterial printers
  882. if (degHotend0() > EXTRUDE_MINTEMP)
  883. {
  884. st_synchronize();
  885. menu_submenu(extr_unload_view);
  886. mmu_filament_ramming();
  887. mmu_command(MmuCmd::U0);
  888. // get response
  889. manage_response(false, true, MMU_UNLOAD_MOVE);
  890. menu_back();
  891. }
  892. else
  893. {
  894. show_preheat_nozzle_warning();
  895. }
  896. }
  897. void load_all()
  898. {
  899. enquecommand_P(PSTR("M701 E0"));
  900. enquecommand_P(PSTR("M701 E1"));
  901. enquecommand_P(PSTR("M701 E2"));
  902. enquecommand_P(PSTR("M701 E3"));
  903. enquecommand_P(PSTR("M701 E4"));
  904. }
  905. bool mmu_check_version()
  906. {
  907. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  908. }
  909. void mmu_show_warning()
  910. {
  911. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  912. kill(_i("Please update firmware in your MMU2. Waiting for reset.")); ////MSG_UPDATE_MMU2_FW c=20 r=4
  913. }
  914. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  915. {
  916. menu_back();
  917. bFilamentAction = false; // NOT in "mmu_load_to_nozzle_menu()"
  918. if (degHotend0() > EXTRUDE_MINTEMP)
  919. {
  920. tmp_extruder = filament_nr;
  921. lcd_update_enable(false);
  922. lcd_clear();
  923. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  924. lcd_print(' ');
  925. lcd_print(tmp_extruder + 1);
  926. mmu_command(MmuCmd::T0 + tmp_extruder);
  927. manage_response(true, true, MMU_TCODE_MOVE);
  928. mmu_continue_loading(false);
  929. mmu_extruder = tmp_extruder; //filament change is finished
  930. raise_z_above(MIN_Z_FOR_LOAD, false);
  931. mmu_load_to_nozzle();
  932. load_filament_final_feed();
  933. st_synchronize();
  934. custom_message_type = CustomMsg::FilamentLoading;
  935. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  936. lcd_return_to_status();
  937. lcd_update_enable(true);
  938. lcd_load_filament_color_check();
  939. lcd_setstatuspgm(MSG_WELCOME);
  940. custom_message_type = CustomMsg::Status;
  941. }
  942. else
  943. {
  944. show_preheat_nozzle_warning();
  945. }
  946. }
  947. #ifdef MMU_HAS_CUTTER
  948. void mmu_cut_filament(uint8_t filament_nr)
  949. {
  950. menu_back();
  951. bFilamentAction=false; // NOT in "mmu_load_to_nozzle_menu()"
  952. if (degHotend0() > EXTRUDE_MINTEMP)
  953. {
  954. LcdUpdateDisabler disableLcdUpdate;
  955. lcd_clear();
  956. lcd_puts_at_P(0, 1, _i("Cutting filament")); ////MSG_MMU_CUTTING_FIL c=18
  957. lcd_print(' ');
  958. lcd_print(filament_nr + 1);
  959. mmu_filament_ramming();
  960. mmu_command(MmuCmd::K0 + filament_nr);
  961. manage_response(false, false, MMU_UNLOAD_MOVE);
  962. }
  963. else
  964. {
  965. show_preheat_nozzle_warning();
  966. }
  967. }
  968. #endif //MMU_HAS_CUTTER
  969. void mmu_eject_filament(uint8_t filament, bool recover)
  970. {
  971. //-//
  972. bFilamentAction=false; // NOT in "mmu_fil_eject_menu()"
  973. if (filament < 5)
  974. {
  975. if (degHotend0() > EXTRUDE_MINTEMP)
  976. {
  977. st_synchronize();
  978. {
  979. LcdUpdateDisabler disableLcdUpdate;
  980. lcd_clear();
  981. lcd_puts_at_P(0, 1, _i("Ejecting filament")); ////MSG_EJECTING_FILAMENT c=20
  982. mmu_filament_ramming();
  983. mmu_command(MmuCmd::E0 + filament);
  984. manage_response(false, false, MMU_UNLOAD_MOVE);
  985. if (recover)
  986. {
  987. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob.")); ////MSG_EJECT_REMOVE c=20 r=4
  988. mmu_command(MmuCmd::R0);
  989. manage_response(false, false);
  990. }
  991. }
  992. }
  993. else
  994. {
  995. show_preheat_nozzle_warning();
  996. }
  997. }
  998. else
  999. {
  1000. puts_P(PSTR("Filament nr out of range!"));
  1001. }
  1002. }
  1003. //! @brief Fits filament tip into heatbreak?
  1004. //!
  1005. //! If PTFE tube is jammed, this causes filament to be unloaded and no longer
  1006. //! being detected by the pulley IR sensor.
  1007. //! @retval true Fits
  1008. //! @retval false Doesn't fit
  1009. static bool can_load()
  1010. {
  1011. current_position[E_AXIS] += 60;
  1012. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1013. current_position[E_AXIS] -= 52;
  1014. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1015. st_synchronize();
  1016. uint_least8_t filament_detected_count = 0;
  1017. const float e_increment = 0.2;
  1018. const uint_least8_t steps = 6.0 / e_increment;
  1019. DEBUG_PUTS_P(PSTR("MMU can_load:"));
  1020. for(uint_least8_t i = 0; i < steps; ++i)
  1021. {
  1022. current_position[E_AXIS] -= e_increment;
  1023. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1024. st_synchronize();
  1025. if(0 == READ(IR_SENSOR_PIN))
  1026. {
  1027. ++filament_detected_count;
  1028. DEBUG_PUTCHAR('O');
  1029. }
  1030. else
  1031. {
  1032. DEBUG_PUTCHAR('o');
  1033. }
  1034. }
  1035. if (filament_detected_count > steps - 4)
  1036. {
  1037. DEBUG_PUTS_P(PSTR(" succeeded."));
  1038. return true;
  1039. }
  1040. else
  1041. {
  1042. DEBUG_PUTS_P(PSTR(" failed."));
  1043. return false;
  1044. }
  1045. }
  1046. //! @brief load more
  1047. //!
  1048. //! Try to feed more filament from MMU if it is not detected by filament sensor.
  1049. //! @retval true Success, filament detected by IR sensor
  1050. //! @retval false Failed, filament not detected by IR sensor after maximum number of attempts
  1051. static bool load_more()
  1052. {
  1053. for (uint8_t i = 0; i < MMU_IDLER_SENSOR_ATTEMPTS_NR; i++)
  1054. {
  1055. if (READ(IR_SENSOR_PIN) == 0) return true;
  1056. DEBUG_PRINTF_P(PSTR("Additional load attempt nr. %d\n"), i);
  1057. mmu_command(MmuCmd::C0);
  1058. manage_response(true, true, MMU_LOAD_MOVE);
  1059. }
  1060. return false;
  1061. }
  1062. static void increment_load_fail()
  1063. {
  1064. uint8_t mmu_load_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL);
  1065. uint16_t mmu_load_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT);
  1066. if(mmu_load_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, mmu_load_fail + 1);
  1067. if(mmu_load_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, mmu_load_fail_tot + 1);
  1068. }
  1069. //! @brief continue loading filament
  1070. //! @par blocking
  1071. //! * true blocking - do not return until successful load
  1072. //! * false non-blocking - pause print and return on load failure
  1073. //!
  1074. //! @startuml
  1075. //! [*] --> [*] : !ir_sensor_detected /\n send MmuCmd::C0
  1076. //! [*] --> LoadMore
  1077. //! LoadMore --> [*] : filament \ndetected
  1078. //! LoadMore --> Retry : !filament detected /\n increment load fail
  1079. //! Retry --> [*] : filament \ndetected
  1080. //! Retry --> Unload : !filament \ndetected
  1081. //! Unload --> [*] : non-blocking
  1082. //! Unload --> Retry : button \nclicked
  1083. //!
  1084. //! Retry : Cut filament if enabled
  1085. //! Retry : repeat last T-code
  1086. //! Unload : unload filament
  1087. //! Unload : pause print
  1088. //! Unload : show error message
  1089. //!
  1090. //! @enduml
  1091. void mmu_continue_loading(bool blocking)
  1092. {
  1093. if (!ir_sensor_detected)
  1094. {
  1095. mmu_command(MmuCmd::C0);
  1096. return;
  1097. }
  1098. bool success = load_more();
  1099. if (success) success = can_load();
  1100. enum class Ls : uint_least8_t
  1101. {
  1102. Enter,
  1103. Retry,
  1104. Unload,
  1105. };
  1106. Ls state = Ls::Enter;
  1107. const uint_least8_t max_retry = 3;
  1108. uint_least8_t retry = 0;
  1109. while (!success)
  1110. {
  1111. switch (state)
  1112. {
  1113. case Ls::Enter:
  1114. increment_load_fail();
  1115. // FALLTHRU
  1116. case Ls::Retry:
  1117. ++retry; // overflow not handled, as it is not dangerous.
  1118. if (retry >= max_retry)
  1119. {
  1120. state = Ls::Unload;
  1121. #ifdef MMU_HAS_CUTTER
  1122. if (1 == eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED))
  1123. {
  1124. mmu_command(MmuCmd::K0 + tmp_extruder);
  1125. manage_response(true, true, MMU_UNLOAD_MOVE);
  1126. }
  1127. #endif //MMU_HAS_CUTTER
  1128. }
  1129. mmu_command(MmuCmd::T0 + tmp_extruder);
  1130. manage_response(true, true, MMU_TCODE_MOVE);
  1131. success = load_more();
  1132. if (success) success = can_load();
  1133. break;
  1134. case Ls::Unload:
  1135. stop_and_save_print_to_ram(0, 0);
  1136. long_pause();
  1137. mmu_command(MmuCmd::U0);
  1138. manage_response(false, true, MMU_UNLOAD_MOVE);
  1139. setAllTargetHotends(0);
  1140. lcd_setstatuspgm(_i("MMU load failed"));////MSG_MMU_LOAD_FAILED c=20
  1141. if (blocking)
  1142. {
  1143. marlin_wait_for_click();
  1144. st_synchronize();
  1145. restore_print_from_ram_and_continue(0);
  1146. state = Ls::Retry;
  1147. }
  1148. else
  1149. {
  1150. mmu_fil_loaded = false; //so we can retry same T-code again
  1151. isPrintPaused = true;
  1152. mmu_command(MmuCmd::W0);
  1153. return;
  1154. }
  1155. break;
  1156. }
  1157. }
  1158. }