mmu.cpp 37 KB

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