mmu.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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 "ultralcd.h"
  13. #include "menu.h"
  14. #include "sound.h"
  15. #include "printers.h"
  16. #include <avr/pgmspace.h>
  17. #include "AutoDeplete.h"
  18. #include "fastio.h"
  19. #include "pins.h"
  20. //-//
  21. #include "util.h"
  22. #ifdef TMC2130
  23. #include "tmc2130.h"
  24. #endif //TMC2130
  25. #define MMU_TODELAY 100
  26. #define MMU_TIMEOUT 10
  27. #define MMU_CMD_TIMEOUT 45000ul //45s timeout for mmu commands (except P0)
  28. #define MMU_P0_TIMEOUT 3000ul //timeout for P0 command: 3seconds
  29. #define MMU_MAX_RESEND_ATTEMPTS 2
  30. namespace
  31. {
  32. enum class S : uint_least8_t
  33. {
  34. WaitStealthMode,
  35. GetFindaInit,
  36. GetBuildNr,
  37. GetVersion,
  38. Init,
  39. Disabled,
  40. Idle,
  41. GetFinda,
  42. WaitCmd, //!< wait for command response
  43. Pause,
  44. GetDrvError, //!< get power failures count
  45. SwitchMode //switch mmu between stealth and normal mode
  46. };
  47. }
  48. bool mmu_enabled = false;
  49. bool mmu_ready = false;
  50. bool mmu_fil_loaded = false; //if true: blocks execution of duplicit T-codes
  51. static S mmu_state = S::Disabled;
  52. MmuCmd mmu_cmd = MmuCmd::None;
  53. //idler ir sensor
  54. static uint8_t mmu_idl_sens = 0;
  55. bool ir_sensor_detected = false;
  56. 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
  57. uint8_t mmu_extruder = MMU_FILAMENT_UNKNOWN;
  58. //! This variable probably has no meaning and is planed to be removed
  59. uint8_t tmp_extruder = MMU_FILAMENT_UNKNOWN;
  60. int8_t mmu_finda = -1;
  61. int16_t mmu_version = -1;
  62. int16_t mmu_buildnr = -1;
  63. ShortTimer mmu_last_request;
  64. ShortTimer mmu_last_response;
  65. ShortTimer mmu_last_finda_response;
  66. MmuCmd mmu_last_cmd = MmuCmd::None;
  67. uint16_t mmu_power_failures = 0;
  68. #ifdef MMU_DEBUG
  69. static const auto DEBUG_PUTCHAR = putchar;
  70. static const auto DEBUG_PUTS_P = puts_P;
  71. static const auto DEBUG_PRINTF_P = printf_P;
  72. #else //MMU_DEBUG
  73. #define DEBUG_PUTCHAR(c)
  74. #define DEBUG_PUTS_P(str)
  75. #define DEBUG_PRINTF_P( __fmt, ... )
  76. #endif //MMU_DEBUG
  77. #if defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
  78. static const auto FDEBUG_PUTS_P = puts_P;
  79. static const auto FDEBUG_PRINTF_P = printf_P;
  80. #else
  81. #define FDEBUG_PUTS_P(str)
  82. #define FDEBUG_PRINTF_P( __fmt, ... )
  83. #endif //defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
  84. //clear rx buffer
  85. void mmu_clr_rx_buf(void)
  86. {
  87. while (fgetc(uart2io) >= 0);
  88. }
  89. //send command - puts
  90. int mmu_puts_P(const char* str)
  91. {
  92. mmu_clr_rx_buf(); //clear rx buffer
  93. int r = fputs_P(str, uart2io); //send command
  94. mmu_last_request.start();
  95. return r;
  96. }
  97. //send command - printf
  98. int mmu_printf_P(const char* format, ...)
  99. {
  100. va_list args;
  101. va_start(args, format);
  102. mmu_clr_rx_buf(); //clear rx buffer
  103. int r = vfprintf_P(uart2io, format, args); //send command
  104. va_end(args);
  105. mmu_last_request.start();
  106. return r;
  107. }
  108. //check 'ok' response
  109. int8_t mmu_rx_ok(void)
  110. {
  111. int8_t res = uart2_rx_str_P(PSTR("ok\n"));
  112. if (res == 1) mmu_last_response.start();
  113. return res;
  114. }
  115. //check 'start' response
  116. int8_t mmu_rx_start(void)
  117. {
  118. int8_t res = uart2_rx_str_P(PSTR("start\n"));
  119. if (res == 1) mmu_last_response.start();
  120. return res;
  121. }
  122. //initialize mmu2 unit - first part - should be done at begining of startup process
  123. void mmu_init(void)
  124. {
  125. #ifdef MMU_HWRESET
  126. WRITE(MMU_RST_PIN, 1);
  127. SET_OUTPUT(MMU_RST_PIN); //setup reset pin
  128. #endif //MMU_HWRESET
  129. uart2_init(); //init uart2
  130. _delay_ms(10); //wait 10ms for sure
  131. mmu_reset(); //reset mmu (HW or SW), do not wait for response
  132. mmu_state = S::Init;
  133. SET_INPUT(IR_SENSOR_PIN); //input mode
  134. WRITE(IR_SENSOR_PIN, 1); //pullup
  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. #ifdef PAT9125
  147. && fsensor_not_responding
  148. #endif //PAT9125
  149. )
  150. {
  151. detected = true;
  152. //printf_P(PSTR("Idler IR sensor detected\n"));
  153. }
  154. else
  155. {
  156. //printf_P(PSTR("Idler IR sensor not detected\n"));
  157. }
  158. return detected;
  159. #endif //IR_SENSOR
  160. }
  161. static bool activate_stealth_mode()
  162. {
  163. #ifdef MMU_FORCE_STEALTH_MODE
  164. return true;
  165. #else
  166. return (eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) == 1);
  167. #endif
  168. }
  169. //mmu main loop - state machine processing
  170. void mmu_loop(void)
  171. {
  172. static uint8_t mmu_attempt_nr = 0;
  173. // printf_P(PSTR("MMU loop, state=%d\n"), mmu_state);
  174. switch (mmu_state)
  175. {
  176. case S::Disabled:
  177. return;
  178. case S::Init:
  179. if (mmu_rx_start() > 0)
  180. {
  181. DEBUG_PUTS_P(PSTR("MMU => 'start'"));
  182. DEBUG_PUTS_P(PSTR("MMU <= 'S1'"));
  183. mmu_puts_P(PSTR("S1\n")); //send 'read version' request
  184. mmu_state = S::GetVersion;
  185. }
  186. else if (_millis() > 30000) //30sec after reset disable mmu
  187. {
  188. puts_P(PSTR("MMU not responding - DISABLED"));
  189. mmu_state = S::Disabled;
  190. }
  191. return;
  192. case S::GetVersion:
  193. if (mmu_rx_ok() > 0)
  194. {
  195. fscanf_P(uart2io, PSTR("%u"), &mmu_version); //scan version from buffer
  196. DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_version);
  197. DEBUG_PUTS_P(PSTR("MMU <= 'S2'"));
  198. mmu_puts_P(PSTR("S2\n")); //send 'read buildnr' request
  199. mmu_state = S::GetBuildNr;
  200. }
  201. return;
  202. case S::GetBuildNr:
  203. if (mmu_rx_ok() > 0)
  204. {
  205. fscanf_P(uart2io, PSTR("%u"), &mmu_buildnr); //scan buildnr from buffer
  206. DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_buildnr);
  207. bool version_valid = mmu_check_version();
  208. if (!version_valid) mmu_show_warning();
  209. else puts_P(PSTR("MMU version valid"));
  210. if (!activate_stealth_mode())
  211. {
  212. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  213. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  214. mmu_state = S::GetFindaInit;
  215. }
  216. else
  217. {
  218. DEBUG_PUTS_P(PSTR("MMU <= 'M1'"));
  219. mmu_puts_P(PSTR("M1\n")); //set mmu mode to stealth
  220. mmu_state = S::WaitStealthMode;
  221. }
  222. }
  223. return;
  224. case S::WaitStealthMode:
  225. if (mmu_rx_ok() > 0)
  226. {
  227. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  228. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  229. mmu_state = S::GetFindaInit;
  230. }
  231. return;
  232. case S::GetFindaInit:
  233. if (mmu_rx_ok() > 0)
  234. {
  235. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer. MUST BE %hhu!!!
  236. mmu_last_finda_response.start();
  237. FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  238. puts_P(PSTR("MMU - ENABLED"));
  239. mmu_enabled = true;
  240. //-//
  241. // ... PrinterType/Name
  242. fSetMmuMode(true);
  243. mmu_state = S::Idle;
  244. }
  245. return;
  246. case S::Idle:
  247. if (mmu_cmd != MmuCmd::None) //command request ?
  248. {
  249. if ((mmu_cmd >= MmuCmd::T0) && (mmu_cmd <= MmuCmd::T4))
  250. {
  251. const uint8_t filament = mmu_cmd - MmuCmd::T0;
  252. DEBUG_PRINTF_P(PSTR("MMU <= 'T%d'\n"), filament);
  253. mmu_printf_P(PSTR("T%d\n"), filament);
  254. mmu_state = S::WaitCmd; // wait for response
  255. mmu_fil_loaded = true;
  256. mmu_idl_sens = 1;
  257. }
  258. else if ((mmu_cmd >= MmuCmd::L0) && (mmu_cmd <= MmuCmd::L4))
  259. {
  260. const uint8_t filament = mmu_cmd - MmuCmd::L0;
  261. DEBUG_PRINTF_P(PSTR("MMU <= 'L%d'\n"), filament);
  262. mmu_printf_P(PSTR("L%d\n"), filament);
  263. mmu_state = S::WaitCmd; // wait for response
  264. }
  265. else if (mmu_cmd == MmuCmd::C0)
  266. {
  267. DEBUG_PRINTF_P(PSTR("MMU <= 'C0'\n"));
  268. mmu_puts_P(PSTR("C0\n")); //send 'continue loading'
  269. mmu_state = S::WaitCmd;
  270. mmu_idl_sens = 1;
  271. }
  272. else if (mmu_cmd == MmuCmd::U0)
  273. {
  274. DEBUG_PRINTF_P(PSTR("MMU <= 'U0'\n"));
  275. mmu_puts_P(PSTR("U0\n")); //send 'unload current filament'
  276. mmu_fil_loaded = false;
  277. mmu_state = S::WaitCmd;
  278. }
  279. else if ((mmu_cmd >= MmuCmd::E0) && (mmu_cmd <= MmuCmd::E4))
  280. {
  281. const uint8_t filament = mmu_cmd - MmuCmd::E0;
  282. DEBUG_PRINTF_P(PSTR("MMU <= 'E%d'\n"), filament);
  283. mmu_printf_P(PSTR("E%d\n"), filament); //send eject filament
  284. mmu_fil_loaded = false;
  285. mmu_state = S::WaitCmd;
  286. }
  287. else if ((mmu_cmd >= MmuCmd::K0) && (mmu_cmd <= MmuCmd::K4))
  288. {
  289. const uint8_t filament = mmu_cmd - MmuCmd::K0;
  290. DEBUG_PRINTF_P(PSTR("MMU <= 'K%d'\n"), filament);
  291. mmu_printf_P(PSTR("K%d\n"), filament); //send eject filament
  292. mmu_fil_loaded = false;
  293. mmu_state = S::WaitCmd;
  294. }
  295. else if (mmu_cmd == MmuCmd::R0)
  296. {
  297. DEBUG_PRINTF_P(PSTR("MMU <= 'R0'\n"));
  298. mmu_puts_P(PSTR("R0\n")); //send recover after eject
  299. mmu_state = S::WaitCmd;
  300. }
  301. else if (mmu_cmd == MmuCmd::S3)
  302. {
  303. DEBUG_PRINTF_P(PSTR("MMU <= 'S3'\n"));
  304. mmu_puts_P(PSTR("S3\n")); //send power failures request
  305. mmu_state = S::GetDrvError;
  306. }
  307. else if (mmu_cmd == MmuCmd::W0)
  308. {
  309. DEBUG_PRINTF_P(PSTR("MMU <= 'W0'\n"));
  310. mmu_puts_P(PSTR("W0\n"));
  311. mmu_state = S::Pause;
  312. }
  313. mmu_last_cmd = mmu_cmd;
  314. mmu_cmd = MmuCmd::None;
  315. }
  316. else if ((eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) != SilentModeMenu_MMU) && mmu_ready) {
  317. DEBUG_PRINTF_P(PSTR("MMU <= 'M%d'\n"), SilentModeMenu_MMU);
  318. mmu_printf_P(PSTR("M%d\n"), SilentModeMenu_MMU);
  319. mmu_state = S::SwitchMode;
  320. }
  321. else if (mmu_last_response.expired(300)) //request every 300ms
  322. {
  323. #ifndef IR_SENSOR
  324. if(check_for_ir_sensor()) ir_sensor_detected = true;
  325. #endif //IR_SENSOR not defined
  326. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  327. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  328. mmu_state = S::GetFinda;
  329. }
  330. return;
  331. case S::GetFinda: //response to command P0
  332. if (mmu_idl_sens)
  333. {
  334. if (READ(IR_SENSOR_PIN) == 0 && mmu_loading_flag)
  335. {
  336. #ifdef MMU_DEBUG
  337. printf_P(PSTR("MMU <= 'A'\n"));
  338. #endif //MMU_DEBUG
  339. mmu_puts_P(PSTR("A\n")); //send 'abort' request
  340. mmu_idl_sens = 0;
  341. //printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
  342. }
  343. //else
  344. //printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
  345. }
  346. if (mmu_rx_ok() > 0)
  347. {
  348. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer. MUST BE %hhu!!!
  349. mmu_last_finda_response.start();
  350. FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  351. //printf_P(PSTR("Eact: %d\n"), int(e_active()));
  352. if (!mmu_finda && CHECK_FSENSOR && fsensor_enabled) {
  353. fsensor_checkpoint_print();
  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..."));
  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..."));
  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..."));
  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. {
  806. #ifdef MMU_M600_SWITCH_EXTRUDER
  807. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  808. if(yes) tmp_extruder = choose_extruder_menu();
  809. #endif //MMU_M600_SWITCH_EXTRUDER
  810. }
  811. else
  812. {
  813. tmp_extruder = ad_getAlternative(tmp_extruder);
  814. }
  815. lcd_update_enable(false);
  816. lcd_clear();
  817. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  818. lcd_print(' ');
  819. lcd_print(tmp_extruder + 1);
  820. //printf_P(PSTR("T code: %d \n"), tmp_extruder);
  821. //mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  822. setTargetHotend(nozzle_temp,active_extruder);
  823. mmu_wait_for_heater_blocking();
  824. mmu_command(MmuCmd::T0 + tmp_extruder);
  825. manage_response(false, true, MMU_LOAD_MOVE);
  826. mmu_continue_loading(usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal));
  827. mmu_extruder = tmp_extruder; //filament change is finished
  828. mmu_load_to_nozzle();
  829. load_filament_final_feed();
  830. st_synchronize();
  831. }
  832. void extr_adj(uint8_t extruder) //loading filament into the MMU unit
  833. {
  834. MmuCmd cmd = MmuCmd::L0 + extruder;
  835. if (extruder > (MmuCmd::L4 - MmuCmd::L0))
  836. {
  837. printf_P(PSTR("Filament out of range %d \n"),extruder);
  838. return;
  839. }
  840. mmu_command(cmd);
  841. //show which filament is currently loaded
  842. lcd_update_enable(false);
  843. lcd_clear();
  844. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  845. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  846. //else lcd.print(" ");
  847. lcd_print(' ');
  848. lcd_print(extruder + 1);
  849. // get response
  850. manage_response(false, false);
  851. lcd_update_enable(true);
  852. //lcd_return_to_status();
  853. }
  854. struct E_step
  855. {
  856. float extrude; //!< extrude distance in mm
  857. float feed_rate; //!< feed rate in mm/s
  858. };
  859. static const E_step ramming_sequence[] PROGMEM =
  860. {
  861. {1.0, 1000.0/60},
  862. {1.0, 1500.0/60},
  863. {2.0, 2000.0/60},
  864. {1.5, 3000.0/60},
  865. {2.5, 4000.0/60},
  866. {-15.0, 5000.0/60},
  867. {-14.0, 1200.0/60},
  868. {-6.0, 600.0/60},
  869. {10.0, 700.0/60},
  870. {-10.0, 400.0/60},
  871. {-50.0, 2000.0/60},
  872. };
  873. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  874. void mmu_filament_ramming()
  875. {
  876. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  877. {
  878. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  879. plan_buffer_line_curposXYZE(pgm_read_float(&(ramming_sequence[i].feed_rate)));
  880. st_synchronize();
  881. }
  882. }
  883. //! @brief show which filament is currently unloaded
  884. void extr_unload_view()
  885. {
  886. lcd_clear();
  887. lcd_puts_at_P(0, 1, _T(MSG_UNLOADING_FILAMENT));
  888. lcd_print(' ');
  889. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(' ');
  890. else lcd_print(mmu_extruder + 1);
  891. }
  892. void extr_unload()
  893. { //unload just current filament for multimaterial printers
  894. if (degHotend0() > EXTRUDE_MINTEMP)
  895. {
  896. st_synchronize();
  897. menu_submenu(extr_unload_view);
  898. mmu_filament_ramming();
  899. mmu_command(MmuCmd::U0);
  900. // get response
  901. manage_response(false, true, MMU_UNLOAD_MOVE);
  902. menu_back();
  903. }
  904. else
  905. {
  906. show_preheat_nozzle_warning();
  907. }
  908. }
  909. void load_all()
  910. {
  911. enquecommand_P(PSTR("M701 E0"));
  912. enquecommand_P(PSTR("M701 E1"));
  913. enquecommand_P(PSTR("M701 E2"));
  914. enquecommand_P(PSTR("M701 E3"));
  915. enquecommand_P(PSTR("M701 E4"));
  916. }
  917. bool mmu_check_version()
  918. {
  919. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  920. }
  921. void mmu_show_warning()
  922. {
  923. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  924. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  925. }
  926. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  927. {
  928. menu_back();
  929. bFilamentAction = false; // NOT in "mmu_load_to_nozzle_menu()"
  930. if (degHotend0() > EXTRUDE_MINTEMP)
  931. {
  932. tmp_extruder = filament_nr;
  933. lcd_update_enable(false);
  934. lcd_clear();
  935. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  936. lcd_print(' ');
  937. lcd_print(tmp_extruder + 1);
  938. mmu_command(MmuCmd::T0 + tmp_extruder);
  939. manage_response(true, true, MMU_TCODE_MOVE);
  940. mmu_continue_loading(false);
  941. mmu_extruder = tmp_extruder; //filament change is finished
  942. raise_z_above(MIN_Z_FOR_LOAD, false);
  943. mmu_load_to_nozzle();
  944. load_filament_final_feed();
  945. st_synchronize();
  946. custom_message_type = CustomMsg::FilamentLoading;
  947. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  948. lcd_return_to_status();
  949. lcd_update_enable(true);
  950. lcd_load_filament_color_check();
  951. lcd_setstatuspgm(MSG_WELCOME);
  952. custom_message_type = CustomMsg::Status;
  953. }
  954. else
  955. {
  956. show_preheat_nozzle_warning();
  957. }
  958. }
  959. #ifdef MMU_HAS_CUTTER
  960. void mmu_cut_filament(uint8_t filament_nr)
  961. {
  962. menu_back();
  963. bFilamentAction=false; // NOT in "mmu_load_to_nozzle_menu()"
  964. if (degHotend0() > EXTRUDE_MINTEMP)
  965. {
  966. LcdUpdateDisabler disableLcdUpdate;
  967. lcd_clear();
  968. lcd_puts_at_P(0, 1, _i("Cutting filament")); ////MSG_MMU_CUTTING_FIL c=18
  969. lcd_print(' ');
  970. lcd_print(filament_nr + 1);
  971. mmu_filament_ramming();
  972. mmu_command(MmuCmd::K0 + filament_nr);
  973. manage_response(false, false, MMU_UNLOAD_MOVE);
  974. }
  975. else
  976. {
  977. show_preheat_nozzle_warning();
  978. }
  979. }
  980. #endif //MMU_HAS_CUTTER
  981. void mmu_eject_filament(uint8_t filament, bool recover)
  982. {
  983. //-//
  984. bFilamentAction=false; // NOT in "mmu_fil_eject_menu()"
  985. if (filament < 5)
  986. {
  987. if (degHotend0() > EXTRUDE_MINTEMP)
  988. {
  989. st_synchronize();
  990. {
  991. LcdUpdateDisabler disableLcdUpdate;
  992. lcd_clear();
  993. lcd_puts_at_P(0, 1, _i("Ejecting filament"));
  994. mmu_filament_ramming();
  995. mmu_command(MmuCmd::E0 + filament);
  996. manage_response(false, false, MMU_UNLOAD_MOVE);
  997. if (recover)
  998. {
  999. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  1000. mmu_command(MmuCmd::R0);
  1001. manage_response(false, false);
  1002. }
  1003. }
  1004. }
  1005. else
  1006. {
  1007. show_preheat_nozzle_warning();
  1008. }
  1009. }
  1010. else
  1011. {
  1012. puts_P(PSTR("Filament nr out of range!"));
  1013. }
  1014. }
  1015. //! @brief Fits filament tip into heatbreak?
  1016. //!
  1017. //! If PTFE tube is jammed, this causes filament to be unloaded and no longer
  1018. //! being detected by the pulley IR sensor.
  1019. //! @retval true Fits
  1020. //! @retval false Doesn't fit
  1021. static bool can_load()
  1022. {
  1023. current_position[E_AXIS] += 60;
  1024. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1025. current_position[E_AXIS] -= 52;
  1026. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1027. st_synchronize();
  1028. uint_least8_t filament_detected_count = 0;
  1029. const float e_increment = 0.2;
  1030. const uint_least8_t steps = 6.0 / e_increment;
  1031. DEBUG_PUTS_P(PSTR("MMU can_load:"));
  1032. for(uint_least8_t i = 0; i < steps; ++i)
  1033. {
  1034. current_position[E_AXIS] -= e_increment;
  1035. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1036. st_synchronize();
  1037. if(0 == READ(IR_SENSOR_PIN))
  1038. {
  1039. ++filament_detected_count;
  1040. DEBUG_PUTCHAR('O');
  1041. }
  1042. else
  1043. {
  1044. DEBUG_PUTCHAR('o');
  1045. }
  1046. }
  1047. if (filament_detected_count > steps - 4)
  1048. {
  1049. DEBUG_PUTS_P(PSTR(" succeeded."));
  1050. return true;
  1051. }
  1052. else
  1053. {
  1054. DEBUG_PUTS_P(PSTR(" failed."));
  1055. return false;
  1056. }
  1057. }
  1058. //! @brief load more
  1059. //!
  1060. //! Try to feed more filament from MMU if it is not detected by filament sensor.
  1061. //! @retval true Success, filament detected by IR sensor
  1062. //! @retval false Failed, filament not detected by IR sensor after maximum number of attempts
  1063. static bool load_more()
  1064. {
  1065. for (uint8_t i = 0; i < MMU_IDLER_SENSOR_ATTEMPTS_NR; i++)
  1066. {
  1067. if (READ(IR_SENSOR_PIN) == 0) return true;
  1068. DEBUG_PRINTF_P(PSTR("Additional load attempt nr. %d\n"), i);
  1069. mmu_command(MmuCmd::C0);
  1070. manage_response(true, true, MMU_LOAD_MOVE);
  1071. }
  1072. return false;
  1073. }
  1074. static void increment_load_fail()
  1075. {
  1076. uint8_t mmu_load_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL);
  1077. uint16_t mmu_load_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT);
  1078. if(mmu_load_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, mmu_load_fail + 1);
  1079. if(mmu_load_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, mmu_load_fail_tot + 1);
  1080. }
  1081. //! @brief continue loading filament
  1082. //! @par blocking
  1083. //! * true blocking - do not return until successful load
  1084. //! * false non-blocking - pause print and return on load failure
  1085. //!
  1086. //! @startuml
  1087. //! [*] --> [*] : !ir_sensor_detected /\n send MmuCmd::C0
  1088. //! [*] --> LoadMore
  1089. //! LoadMore --> [*] : filament \ndetected
  1090. //! LoadMore --> Retry : !filament detected /\n increment load fail
  1091. //! Retry --> [*] : filament \ndetected
  1092. //! Retry --> Unload : !filament \ndetected
  1093. //! Unload --> [*] : non-blocking
  1094. //! Unload --> Retry : button \nclicked
  1095. //!
  1096. //! Retry : Cut filament if enabled
  1097. //! Retry : repeat last T-code
  1098. //! Unload : unload filament
  1099. //! Unload : pause print
  1100. //! Unload : show error message
  1101. //!
  1102. //! @enduml
  1103. void mmu_continue_loading(bool blocking)
  1104. {
  1105. if (!ir_sensor_detected)
  1106. {
  1107. mmu_command(MmuCmd::C0);
  1108. return;
  1109. }
  1110. bool success = load_more();
  1111. if (success) success = can_load();
  1112. enum class Ls : uint_least8_t
  1113. {
  1114. Enter,
  1115. Retry,
  1116. Unload,
  1117. };
  1118. Ls state = Ls::Enter;
  1119. const uint_least8_t max_retry = 3;
  1120. uint_least8_t retry = 0;
  1121. while (!success)
  1122. {
  1123. switch (state)
  1124. {
  1125. case Ls::Enter:
  1126. increment_load_fail();
  1127. // FALLTHRU
  1128. case Ls::Retry:
  1129. ++retry; // overflow not handled, as it is not dangerous.
  1130. if (retry >= max_retry)
  1131. {
  1132. state = Ls::Unload;
  1133. #ifdef MMU_HAS_CUTTER
  1134. if (1 == eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED))
  1135. {
  1136. mmu_command(MmuCmd::K0 + tmp_extruder);
  1137. manage_response(true, true, MMU_UNLOAD_MOVE);
  1138. }
  1139. #endif //MMU_HAS_CUTTER
  1140. }
  1141. mmu_command(MmuCmd::T0 + tmp_extruder);
  1142. manage_response(true, true, MMU_TCODE_MOVE);
  1143. success = load_more();
  1144. if (success) success = can_load();
  1145. break;
  1146. case Ls::Unload:
  1147. stop_and_save_print_to_ram(0, 0);
  1148. long_pause();
  1149. mmu_command(MmuCmd::U0);
  1150. manage_response(false, true, MMU_UNLOAD_MOVE);
  1151. setAllTargetHotends(0);
  1152. lcd_setstatuspgm(_i("MMU load failed"));////MSG_MMU_LOAD_FAILED c=20
  1153. if (blocking)
  1154. {
  1155. marlin_wait_for_click();
  1156. st_synchronize();
  1157. restore_print_from_ram_and_continue(0);
  1158. state = Ls::Retry;
  1159. }
  1160. else
  1161. {
  1162. mmu_fil_loaded = false; //so we can retry same T-code again
  1163. isPrintPaused = true;
  1164. mmu_command(MmuCmd::W0);
  1165. return;
  1166. }
  1167. break;
  1168. }
  1169. }
  1170. }