mmu.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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 "ultralcd.h"
  12. #include "sound.h"
  13. #include "printers.h"
  14. #include <avr/pgmspace.h>
  15. #include "AutoDeplete.h"
  16. #include "fastio.h"
  17. #include "pins.h"
  18. //-//
  19. #include "util.h"
  20. #ifdef TMC2130
  21. #include "tmc2130.h"
  22. #endif //TMC2130
  23. #define MMU_TODELAY 100
  24. #define MMU_TIMEOUT 10
  25. #define MMU_CMD_TIMEOUT 45000ul //45s timeout for mmu commands (except P0)
  26. #define MMU_P0_TIMEOUT 3000ul //timeout for P0 command: 3seconds
  27. #define MMU_MAX_RESEND_ATTEMPTS 2
  28. namespace
  29. {
  30. enum class S : uint_least8_t
  31. {
  32. WaitStealthMode,
  33. GetFindaInit,
  34. GetBuildNr,
  35. GetVersion,
  36. Init,
  37. Disabled,
  38. Idle,
  39. GetFinda,
  40. WaitCmd, //!< wait for command response
  41. Pause,
  42. GetDrvError, //!< get power failures count
  43. SwitchMode //switch mmu between stealth and normal mode
  44. };
  45. }
  46. bool mmu_enabled = false;
  47. bool mmu_ready = false;
  48. bool mmu_fil_loaded = false; //if true: blocks execution of duplicit T-codes
  49. static S mmu_state = S::Disabled;
  50. MmuCmd mmu_cmd = MmuCmd::None;
  51. //idler ir sensor
  52. static uint8_t mmu_idl_sens = 0;
  53. bool ir_sensor_detected = false;
  54. 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
  55. uint8_t mmu_extruder = MMU_FILAMENT_UNKNOWN;
  56. //! This variable probably has no meaning and is planed to be removed
  57. uint8_t tmp_extruder = MMU_FILAMENT_UNKNOWN;
  58. int8_t mmu_finda = -1;
  59. uint32_t mmu_last_finda_response = 0;
  60. int16_t mmu_version = -1;
  61. int16_t mmu_buildnr = -1;
  62. uint32_t mmu_last_request = 0;
  63. uint32_t mmu_last_response = 0;
  64. MmuCmd mmu_last_cmd = MmuCmd::None;
  65. uint16_t mmu_power_failures = 0;
  66. #ifdef MMU_DEBUG
  67. static const auto DEBUG_PUTCHAR = putchar;
  68. static const auto DEBUG_PUTS_P = puts_P;
  69. static const auto DEBUG_PRINTF_P = printf_P;
  70. #else //MMU_DEBUG
  71. #define DEBUG_PUTCHAR(c)
  72. #define DEBUG_PUTS_P(str)
  73. #define DEBUG_PRINTF_P( __fmt, ... )
  74. #endif //MMU_DEBUG
  75. #if defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
  76. static const auto FDEBUG_PUTS_P = puts_P;
  77. static const auto FDEBUG_PRINTF_P = printf_P;
  78. #else
  79. #define FDEBUG_PUTS_P(str)
  80. #define FDEBUG_PRINTF_P( __fmt, ... )
  81. #endif //defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
  82. //clear rx buffer
  83. void mmu_clr_rx_buf(void)
  84. {
  85. while (fgetc(uart2io) >= 0);
  86. }
  87. //send command - puts
  88. int mmu_puts_P(const char* str)
  89. {
  90. mmu_clr_rx_buf(); //clear rx buffer
  91. int r = fputs_P(str, uart2io); //send command
  92. mmu_last_request = _millis();
  93. return r;
  94. }
  95. //send command - printf
  96. int mmu_printf_P(const char* format, ...)
  97. {
  98. va_list args;
  99. va_start(args, format);
  100. mmu_clr_rx_buf(); //clear rx buffer
  101. int r = vfprintf_P(uart2io, format, args); //send command
  102. va_end(args);
  103. mmu_last_request = _millis();
  104. return r;
  105. }
  106. //check 'ok' response
  107. int8_t mmu_rx_ok(void)
  108. {
  109. int8_t res = uart2_rx_str_P(PSTR("ok\n"));
  110. if (res == 1) mmu_last_response = _millis();
  111. return res;
  112. }
  113. //check 'start' response
  114. int8_t mmu_rx_start(void)
  115. {
  116. int8_t res = uart2_rx_str_P(PSTR("start\n"));
  117. if (res == 1) mmu_last_response = _millis();
  118. return res;
  119. }
  120. //initialize mmu2 unit - first part - should be done at begining of startup process
  121. void mmu_init(void)
  122. {
  123. #ifdef MMU_HWRESET
  124. digitalWrite(MMU_RST_PIN, HIGH);
  125. pinMode(MMU_RST_PIN, OUTPUT); //setup reset pin
  126. #endif //MMU_HWRESET
  127. uart2_init(); //init uart2
  128. _delay_ms(10); //wait 10ms for sure
  129. mmu_reset(); //reset mmu (HW or SW), do not wait for response
  130. mmu_state = S::Init;
  131. SET_INPUT(IR_SENSOR_PIN); //input mode
  132. WRITE(IR_SENSOR_PIN, 1); //pullup
  133. }
  134. //if IR_SENSOR defined, always returns true
  135. //otherwise check for ir sensor and returns true if idler IR sensor was detected, otherwise returns false
  136. bool check_for_ir_sensor()
  137. {
  138. #ifdef IR_SENSOR
  139. return true;
  140. #else //IR_SENSOR
  141. bool detected = false;
  142. //if IR_SENSOR_PIN input is low and pat9125sensor is not present we detected idler sensor
  143. if ((READ(IR_SENSOR_PIN) == 0)
  144. #ifdef PAT9125
  145. && fsensor_not_responding
  146. #endif //PAT9125
  147. )
  148. {
  149. detected = true;
  150. //printf_P(PSTR("Idler IR sensor detected\n"));
  151. }
  152. else
  153. {
  154. //printf_P(PSTR("Idler IR sensor not detected\n"));
  155. }
  156. return detected;
  157. #endif //IR_SENSOR
  158. }
  159. static bool activate_stealth_mode()
  160. {
  161. #ifdef MMU_FORCE_STEALTH_MODE
  162. return true;
  163. #else
  164. return (eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) == 1);
  165. #endif
  166. }
  167. //mmu main loop - state machine processing
  168. void mmu_loop(void)
  169. {
  170. static uint8_t mmu_attempt_nr = 0;
  171. // printf_P(PSTR("MMU loop, state=%d\n"), mmu_state);
  172. switch (mmu_state)
  173. {
  174. case S::Disabled:
  175. return;
  176. case S::Init:
  177. if (mmu_rx_start() > 0)
  178. {
  179. DEBUG_PUTS_P(PSTR("MMU => 'start'"));
  180. DEBUG_PUTS_P(PSTR("MMU <= 'S1'"));
  181. mmu_puts_P(PSTR("S1\n")); //send 'read version' request
  182. mmu_state = S::GetVersion;
  183. }
  184. else if (_millis() > 30000) //30sec after reset disable mmu
  185. {
  186. puts_P(PSTR("MMU not responding - DISABLED"));
  187. mmu_state = S::Disabled;
  188. }
  189. return;
  190. case S::GetVersion:
  191. if (mmu_rx_ok() > 0)
  192. {
  193. fscanf_P(uart2io, PSTR("%u"), &mmu_version); //scan version from buffer
  194. DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_version);
  195. DEBUG_PUTS_P(PSTR("MMU <= 'S2'"));
  196. mmu_puts_P(PSTR("S2\n")); //send 'read buildnr' request
  197. mmu_state = S::GetBuildNr;
  198. }
  199. return;
  200. case S::GetBuildNr:
  201. if (mmu_rx_ok() > 0)
  202. {
  203. fscanf_P(uart2io, PSTR("%u"), &mmu_buildnr); //scan buildnr from buffer
  204. DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_buildnr);
  205. bool version_valid = mmu_check_version();
  206. if (!version_valid) mmu_show_warning();
  207. else puts_P(PSTR("MMU version valid"));
  208. if (!activate_stealth_mode())
  209. {
  210. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  211. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  212. mmu_state = S::GetFindaInit;
  213. }
  214. else
  215. {
  216. DEBUG_PUTS_P(PSTR("MMU <= 'M1'"));
  217. mmu_puts_P(PSTR("M1\n")); //set mmu mode to stealth
  218. mmu_state = S::WaitStealthMode;
  219. }
  220. }
  221. return;
  222. case S::WaitStealthMode:
  223. if (mmu_rx_ok() > 0)
  224. {
  225. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  226. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  227. mmu_state = S::GetFindaInit;
  228. }
  229. return;
  230. case S::GetFindaInit:
  231. if (mmu_rx_ok() > 0)
  232. {
  233. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  234. mmu_last_finda_response = _millis();
  235. FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  236. puts_P(PSTR("MMU - ENABLED"));
  237. mmu_enabled = true;
  238. //-//
  239. // ... PrinterType/Name
  240. fSetMmuMode(true);
  241. mmu_state = S::Idle;
  242. }
  243. return;
  244. case S::Idle:
  245. if (mmu_cmd != MmuCmd::None) //command request ?
  246. {
  247. if ((mmu_cmd >= MmuCmd::T0) && (mmu_cmd <= MmuCmd::T4))
  248. {
  249. const uint8_t filament = mmu_cmd - MmuCmd::T0;
  250. DEBUG_PRINTF_P(PSTR("MMU <= 'T%d'\n"), filament);
  251. mmu_printf_P(PSTR("T%d\n"), filament);
  252. mmu_state = S::WaitCmd; // wait for response
  253. mmu_fil_loaded = true;
  254. mmu_idl_sens = 1;
  255. }
  256. else if ((mmu_cmd >= MmuCmd::L0) && (mmu_cmd <= MmuCmd::L4))
  257. {
  258. const uint8_t filament = mmu_cmd - MmuCmd::L0;
  259. DEBUG_PRINTF_P(PSTR("MMU <= 'L%d'\n"), filament);
  260. mmu_printf_P(PSTR("L%d\n"), filament);
  261. mmu_state = S::WaitCmd; // wait for response
  262. }
  263. else if (mmu_cmd == MmuCmd::C0)
  264. {
  265. DEBUG_PRINTF_P(PSTR("MMU <= 'C0'\n"));
  266. mmu_puts_P(PSTR("C0\n")); //send 'continue loading'
  267. mmu_state = S::WaitCmd;
  268. mmu_idl_sens = 1;
  269. }
  270. else if (mmu_cmd == MmuCmd::U0)
  271. {
  272. DEBUG_PRINTF_P(PSTR("MMU <= 'U0'\n"));
  273. mmu_puts_P(PSTR("U0\n")); //send 'unload current filament'
  274. mmu_fil_loaded = false;
  275. mmu_state = S::WaitCmd;
  276. }
  277. else if ((mmu_cmd >= MmuCmd::E0) && (mmu_cmd <= MmuCmd::E4))
  278. {
  279. const uint8_t filament = mmu_cmd - MmuCmd::E0;
  280. DEBUG_PRINTF_P(PSTR("MMU <= 'E%d'\n"), filament);
  281. mmu_printf_P(PSTR("E%d\n"), filament); //send eject filament
  282. mmu_fil_loaded = false;
  283. mmu_state = S::WaitCmd;
  284. }
  285. else if ((mmu_cmd >= MmuCmd::K0) && (mmu_cmd <= MmuCmd::K4))
  286. {
  287. const uint8_t filament = mmu_cmd - MmuCmd::K0;
  288. DEBUG_PRINTF_P(PSTR("MMU <= 'K%d'\n"), filament);
  289. mmu_printf_P(PSTR("K%d\n"), filament); //send eject filament
  290. mmu_fil_loaded = false;
  291. mmu_state = S::WaitCmd;
  292. }
  293. else if (mmu_cmd == MmuCmd::R0)
  294. {
  295. DEBUG_PRINTF_P(PSTR("MMU <= 'R0'\n"));
  296. mmu_puts_P(PSTR("R0\n")); //send recover after eject
  297. mmu_state = S::WaitCmd;
  298. }
  299. else if (mmu_cmd == MmuCmd::S3)
  300. {
  301. DEBUG_PRINTF_P(PSTR("MMU <= 'S3'\n"));
  302. mmu_puts_P(PSTR("S3\n")); //send power failures request
  303. mmu_state = S::GetDrvError;
  304. }
  305. else if (mmu_cmd == MmuCmd::W0)
  306. {
  307. DEBUG_PRINTF_P(PSTR("MMU <= 'W0'\n"));
  308. mmu_puts_P(PSTR("W0\n"));
  309. mmu_state = S::Pause;
  310. }
  311. mmu_last_cmd = mmu_cmd;
  312. mmu_cmd = MmuCmd::None;
  313. }
  314. else if ((eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) != SilentModeMenu_MMU) && mmu_ready) {
  315. DEBUG_PRINTF_P(PSTR("MMU <= 'M%d'\n"), SilentModeMenu_MMU);
  316. mmu_printf_P(PSTR("M%d\n"), SilentModeMenu_MMU);
  317. mmu_state = S::SwitchMode;
  318. }
  319. else if ((mmu_last_response + 300) < _millis()) //request every 300ms
  320. {
  321. #ifndef IR_SENSOR
  322. if(check_for_ir_sensor()) ir_sensor_detected = true;
  323. #endif //IR_SENSOR not defined
  324. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  325. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  326. mmu_state = S::GetFinda;
  327. }
  328. return;
  329. case S::GetFinda: //response to command P0
  330. if (mmu_idl_sens)
  331. {
  332. if (READ(IR_SENSOR_PIN) == 0 && mmu_loading_flag)
  333. {
  334. #ifdef MMU_DEBUG
  335. printf_P(PSTR("MMU <= 'A'\n"));
  336. #endif //MMU_DEBUG
  337. mmu_puts_P(PSTR("A\n")); //send 'abort' request
  338. mmu_idl_sens = 0;
  339. //printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
  340. }
  341. //else
  342. //printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
  343. }
  344. if (mmu_rx_ok() > 0)
  345. {
  346. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  347. mmu_last_finda_response = _millis();
  348. FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  349. //printf_P(PSTR("Eact: %d\n"), int(e_active()));
  350. if (!mmu_finda && CHECK_FSENSOR && fsensor_enabled) {
  351. fsensor_checkpoint_print();
  352. if (mmu_extruder != MMU_FILAMENT_UNKNOWN) // Can't deplete unknown extruder.
  353. ad_markDepleted(mmu_extruder);
  354. if (lcd_autoDepleteEnabled() && !ad_allDepleted() && mmu_extruder != MMU_FILAMENT_UNKNOWN) // Can't auto if F=?
  355. {
  356. enquecommand_front_P(PSTR("M600 AUTO")); //save print and run M600 command
  357. }
  358. else
  359. {
  360. enquecommand_front_P(PSTR("M600")); //save print and run M600 command
  361. }
  362. }
  363. mmu_state = S::Idle;
  364. if (mmu_cmd == MmuCmd::None)
  365. mmu_ready = true;
  366. }
  367. else if ((mmu_last_request + MMU_P0_TIMEOUT) < _millis())
  368. { //resend request after timeout (30s)
  369. mmu_state = S::Idle;
  370. }
  371. return;
  372. case S::WaitCmd: //response to mmu commands
  373. if (mmu_idl_sens)
  374. {
  375. if (READ(IR_SENSOR_PIN) == 0 && mmu_loading_flag)
  376. {
  377. DEBUG_PRINTF_P(PSTR("MMU <= 'A'\n"));
  378. mmu_puts_P(PSTR("A\n")); //send 'abort' request
  379. mmu_idl_sens = 0;
  380. //printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
  381. }
  382. //else
  383. //printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
  384. }
  385. if (mmu_rx_ok() > 0)
  386. {
  387. DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
  388. mmu_attempt_nr = 0;
  389. mmu_last_cmd = MmuCmd::None;
  390. mmu_ready = true;
  391. mmu_state = S::Idle;
  392. }
  393. else if ((mmu_last_request + MMU_CMD_TIMEOUT) < _millis())
  394. { //resend request after timeout (5 min)
  395. if (mmu_last_cmd != MmuCmd::None)
  396. {
  397. if (mmu_attempt_nr++ < MMU_MAX_RESEND_ATTEMPTS &&
  398. mmu_last_cmd >= MmuCmd::T0 && mmu_last_cmd <= MmuCmd::T4)
  399. {
  400. DEBUG_PRINTF_P(PSTR("MMU retry attempt nr. %d\n"), mmu_attempt_nr - 1);
  401. mmu_cmd = mmu_last_cmd;
  402. }
  403. else {
  404. mmu_cmd = MmuCmd::None;
  405. mmu_last_cmd = MmuCmd::None; //check
  406. mmu_attempt_nr = 0;
  407. }
  408. }
  409. mmu_state = S::Idle;
  410. }
  411. return;
  412. case S::Pause:
  413. if (mmu_rx_ok() > 0)
  414. {
  415. DEBUG_PRINTF_P(PSTR("MMU => 'ok', resume print\n"));
  416. mmu_attempt_nr = 0;
  417. mmu_last_cmd = MmuCmd::None;
  418. mmu_ready = true;
  419. mmu_state = S::Idle;
  420. lcd_resume_print();
  421. }
  422. if (mmu_cmd != MmuCmd::None)
  423. {
  424. mmu_state = S::Idle;
  425. }
  426. return;
  427. case S::GetDrvError:
  428. if (mmu_rx_ok() > 0)
  429. {
  430. fscanf_P(uart2io, PSTR("%d"), &mmu_power_failures); //scan power failures
  431. DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
  432. mmu_last_cmd = MmuCmd::None;
  433. mmu_ready = true;
  434. mmu_state = S::Idle;
  435. }
  436. else if ((mmu_last_request + MMU_CMD_TIMEOUT) < _millis())
  437. { //timeout 45 s
  438. mmu_state = S::Idle;
  439. }
  440. return;
  441. case S::SwitchMode:
  442. if (mmu_rx_ok() > 0)
  443. {
  444. DEBUG_PRINTF_P(PSTR("MMU => 'ok'\n"));
  445. eeprom_update_byte((uint8_t*)EEPROM_MMU_STEALTH, SilentModeMenu_MMU);
  446. mmu_state = S::Idle;
  447. }
  448. else if ((mmu_last_request + MMU_CMD_TIMEOUT) < _millis())
  449. { //timeout 45 s
  450. mmu_state = S::Idle;
  451. }
  452. return;
  453. }
  454. }
  455. void mmu_reset(void)
  456. {
  457. #ifdef MMU_HWRESET //HW - pulse reset pin
  458. digitalWrite(MMU_RST_PIN, LOW);
  459. _delay_us(100);
  460. digitalWrite(MMU_RST_PIN, HIGH);
  461. #else //SW - send X0 command
  462. mmu_puts_P(PSTR("X0\n"));
  463. #endif
  464. }
  465. int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament)
  466. {
  467. printf_P(PSTR("MMU <= 'F%d %d'\n"), extruder, filament);
  468. mmu_printf_P(PSTR("F%d %d\n"), extruder, filament);
  469. unsigned char timeout = MMU_TIMEOUT; //10x100ms
  470. while ((mmu_rx_ok() <= 0) && (--timeout))
  471. delay_keep_alive(MMU_TODELAY);
  472. return timeout?1:0;
  473. }
  474. //! @brief Enqueue MMUv2 command
  475. //!
  476. //! Call manage_response() after enqueuing to process command.
  477. //! If T command is enqueued, it disables current for extruder motor if TMC2130 driver present.
  478. //! If T or L command is enqueued, it marks filament loaded in AutoDeplete module.
  479. void mmu_command(MmuCmd cmd)
  480. {
  481. if ((cmd >= MmuCmd::T0) && (cmd <= MmuCmd::T4))
  482. {
  483. //disable extruder motor
  484. #ifdef TMC2130
  485. tmc2130_set_pwr(E_AXIS, 0);
  486. #endif //TMC2130
  487. //printf_P(PSTR("E-axis disabled\n"));
  488. ad_markLoaded(cmd - MmuCmd::T0);
  489. }
  490. if ((cmd >= MmuCmd::L0) && (cmd <= MmuCmd::L4))
  491. {
  492. ad_markLoaded(cmd - MmuCmd::L0);
  493. }
  494. mmu_cmd = cmd;
  495. mmu_ready = false;
  496. }
  497. //! @brief Rotate extruder idler to catch filament
  498. //! @par synchronize
  499. //! * true blocking call
  500. //! * false non-blocking call
  501. void mmu_load_step(bool synchronize)
  502. {
  503. current_position[E_AXIS] = current_position[E_AXIS] + MMU_LOAD_FEEDRATE * 0.1;
  504. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  505. if (synchronize) st_synchronize();
  506. }
  507. //! @brief Is nozzle hot enough to move extruder wheels and do we have idler sensor?
  508. //!
  509. //! Do load steps only if temperature is higher then min. temp for safe extrusion and
  510. //! idler sensor present.
  511. //! Otherwise "cold extrusion prevented" would be send to serial line periodically
  512. //! and watchdog reset will be triggered by lack of keep_alive processing.
  513. //!
  514. //! @retval true temperature is high enough to move extruder
  515. //! @retval false temperature is not high enough to move extruder, turned
  516. //! off E-stepper to prevent over-heating and allow filament pull-out if necessary
  517. bool can_extrude()
  518. {
  519. if ((degHotend(active_extruder) < EXTRUDE_MINTEMP) || !ir_sensor_detected)
  520. {
  521. disable_e0();
  522. delay_keep_alive(100);
  523. return false;
  524. }
  525. return true;
  526. }
  527. static void get_response_print_info(uint8_t move) {
  528. printf_P(PSTR("mmu_get_response - begin move: "), move);
  529. switch (move) {
  530. case MMU_LOAD_MOVE: printf_P(PSTR("load\n")); break;
  531. case MMU_UNLOAD_MOVE: printf_P(PSTR("unload\n")); break;
  532. case MMU_TCODE_MOVE: printf_P(PSTR("T-code\n")); break;
  533. case MMU_NO_MOVE: printf_P(PSTR("no move\n")); break;
  534. default: printf_P(PSTR("error: unknown move\n")); break;
  535. }
  536. }
  537. bool mmu_get_response(uint8_t move)
  538. {
  539. get_response_print_info(move);
  540. KEEPALIVE_STATE(IN_PROCESS);
  541. while (mmu_cmd != MmuCmd::None)
  542. {
  543. delay_keep_alive(100);
  544. }
  545. while (!mmu_ready)
  546. {
  547. if ((mmu_state != S::WaitCmd) && (mmu_last_cmd == MmuCmd::None))
  548. break;
  549. switch (move) {
  550. case MMU_LOAD_MOVE:
  551. mmu_loading_flag = true;
  552. if (can_extrude()) mmu_load_step();
  553. //don't rely on "ok" signal from mmu unit; if filament detected by idler sensor during loading stop loading movements to prevent infinite loading
  554. if (READ(IR_SENSOR_PIN) == 0) move = MMU_NO_MOVE;
  555. break;
  556. case MMU_UNLOAD_MOVE:
  557. if (READ(IR_SENSOR_PIN) == 0) //filament is still detected by idler sensor, printer helps with unlading
  558. {
  559. if (can_extrude())
  560. {
  561. printf_P(PSTR("Unload 1\n"));
  562. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  563. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  564. st_synchronize();
  565. }
  566. }
  567. else //filament was unloaded from idler, no additional movements needed
  568. {
  569. printf_P(PSTR("Unloading finished 1\n"));
  570. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  571. move = MMU_NO_MOVE;
  572. }
  573. break;
  574. case MMU_TCODE_MOVE: //first do unload and then continue with infinite loading movements
  575. if (READ(IR_SENSOR_PIN) == 0) //filament detected by idler sensor, we must unload first
  576. {
  577. if (can_extrude())
  578. {
  579. printf_P(PSTR("Unload 2\n"));
  580. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  581. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  582. st_synchronize();
  583. }
  584. }
  585. else //delay to allow mmu unit to pull out filament from bondtech gears and then start with infinite loading
  586. {
  587. printf_P(PSTR("Unloading finished 2\n"));
  588. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  589. delay_keep_alive(MMU_LOAD_TIME_MS);
  590. move = MMU_LOAD_MOVE;
  591. get_response_print_info(move);
  592. }
  593. break;
  594. case MMU_NO_MOVE:
  595. default:
  596. delay_keep_alive(100);
  597. break;
  598. }
  599. }
  600. printf_P(PSTR("mmu_get_response() returning: %d\n"), mmu_ready);
  601. bool ret = mmu_ready;
  602. mmu_ready = false;
  603. // printf_P(PSTR("mmu_get_response - end %d\n"), ret?1:0);
  604. return ret;
  605. }
  606. //! @brief Wait for active extruder to reach temperature set
  607. //!
  608. //! This function is blocking and showing lcd_wait_for_heater() screen
  609. //! which is constantly updated with nozzle temperature.
  610. void mmu_wait_for_heater_blocking()
  611. {
  612. while ((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)
  613. {
  614. delay_keep_alive(1000);
  615. lcd_wait_for_heater();
  616. }
  617. }
  618. void manage_response(bool move_axes, bool turn_off_nozzle, uint8_t move)
  619. {
  620. bool response = false;
  621. mmu_print_saved = false;
  622. bool lcd_update_was_enabled = false;
  623. float hotend_temp_bckp = degTargetHotend(active_extruder);
  624. float z_position_bckp = current_position[Z_AXIS];
  625. float x_position_bckp = current_position[X_AXIS];
  626. float y_position_bckp = current_position[Y_AXIS];
  627. uint8_t screen = 0; //used for showing multiscreen messages
  628. mmu_loading_flag = false;
  629. while(!response)
  630. {
  631. response = mmu_get_response(move); //wait for "ok" from mmu
  632. if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit
  633. if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
  634. uint8_t mmu_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL);
  635. uint16_t mmu_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT);
  636. if(mmu_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_FAIL, mmu_fail + 1);
  637. if(mmu_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_FAIL_TOT, mmu_fail_tot + 1);
  638. if (lcd_update_enabled) {
  639. lcd_update_was_enabled = true;
  640. lcd_update_enable(false);
  641. }
  642. st_synchronize();
  643. mmu_print_saved = true;
  644. printf_P(PSTR("MMU not responding\n"));
  645. KEEPALIVE_STATE(PAUSED_FOR_USER);
  646. hotend_temp_bckp = degTargetHotend(active_extruder);
  647. if (move_axes) {
  648. z_position_bckp = current_position[Z_AXIS];
  649. x_position_bckp = current_position[X_AXIS];
  650. y_position_bckp = current_position[Y_AXIS];
  651. //lift z
  652. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  653. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  654. plan_buffer_line_curposXYZE(15);
  655. st_synchronize();
  656. //Move XY to side
  657. current_position[X_AXIS] = X_PAUSE_POS;
  658. current_position[Y_AXIS] = Y_PAUSE_POS;
  659. plan_buffer_line_curposXYZE(50);
  660. st_synchronize();
  661. }
  662. if (turn_off_nozzle) {
  663. //set nozzle target temperature to 0
  664. setAllTargetHotends(0);
  665. }
  666. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  667. }
  668. //first three lines are used for printing multiscreen message; last line contains measured and target nozzle temperature
  669. if (screen == 0) { //screen 0
  670. lcd_display_message_fullscreen_P(_i("MMU needs user attention."));
  671. screen++;
  672. }
  673. else { //screen 1
  674. if((degTargetHotend(active_extruder) == 0) && turn_off_nozzle) lcd_display_message_fullscreen_P(_i("Press the knob to resume nozzle temperature."));
  675. else lcd_display_message_fullscreen_P(_i("Fix the issue and then press button on MMU unit."));
  676. screen=0;
  677. }
  678. lcd_set_degree();
  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. printf_P(PSTR("MMU starts responding\n"));
  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..."));
  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..."));
  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..."));
  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. {
  805. #ifdef MMU_M600_SWITCH_EXTRUDER
  806. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  807. if(yes) tmp_extruder = choose_extruder_menu();
  808. #endif //MMU_M600_SWITCH_EXTRUDER
  809. }
  810. else
  811. {
  812. tmp_extruder = ad_getAlternative(tmp_extruder);
  813. }
  814. lcd_update_enable(false);
  815. lcd_clear();
  816. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  817. lcd_print(" ");
  818. lcd_print(tmp_extruder + 1);
  819. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  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(is_usb_printing || (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. #ifdef SNMM
  833. void extr_mov(float shift, float feed_rate)
  834. { //move extruder no matter what the current heater temperature is
  835. set_extrude_min_temp(.0);
  836. current_position[E_AXIS] += shift;
  837. plan_buffer_line_curposXYZE(feed_rate, active_extruder);
  838. set_extrude_min_temp(EXTRUDE_MINTEMP);
  839. }
  840. #endif //SNMM
  841. void change_extr(int
  842. #ifdef SNMM
  843. extr
  844. #endif //SNMM
  845. ) { //switches multiplexer for extruders
  846. #ifdef SNMM
  847. st_synchronize();
  848. _delay(100);
  849. disable_e0();
  850. disable_e1();
  851. disable_e2();
  852. mmu_extruder = extr;
  853. pinMode(E_MUX0_PIN, OUTPUT);
  854. pinMode(E_MUX1_PIN, OUTPUT);
  855. switch (extr) {
  856. case 1:
  857. WRITE(E_MUX0_PIN, HIGH);
  858. WRITE(E_MUX1_PIN, LOW);
  859. break;
  860. case 2:
  861. WRITE(E_MUX0_PIN, LOW);
  862. WRITE(E_MUX1_PIN, HIGH);
  863. break;
  864. case 3:
  865. WRITE(E_MUX0_PIN, HIGH);
  866. WRITE(E_MUX1_PIN, HIGH);
  867. break;
  868. default:
  869. WRITE(E_MUX0_PIN, LOW);
  870. WRITE(E_MUX1_PIN, LOW);
  871. break;
  872. }
  873. _delay(100);
  874. #endif
  875. }
  876. int get_ext_nr()
  877. { //reads multiplexer input pins and return current extruder number (counted from 0)
  878. #ifndef SNMM
  879. return(mmu_extruder); //update needed
  880. #else
  881. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  882. #endif
  883. }
  884. void display_loading()
  885. {
  886. switch (mmu_extruder)
  887. {
  888. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  889. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  890. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  891. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  892. }
  893. }
  894. void extr_adj(uint8_t extruder) //loading filament for SNMM
  895. {
  896. #ifndef SNMM
  897. MmuCmd cmd = MmuCmd::L0 + extruder;
  898. if (extruder > (MmuCmd::L4 - MmuCmd::L0))
  899. {
  900. printf_P(PSTR("Filament out of range %d \n"),extruder);
  901. return;
  902. }
  903. mmu_command(cmd);
  904. //show which filament is currently loaded
  905. lcd_update_enable(false);
  906. lcd_clear();
  907. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  908. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  909. //else lcd.print(" ");
  910. lcd_print(" ");
  911. lcd_print(extruder + 1);
  912. // get response
  913. manage_response(false, false);
  914. lcd_update_enable(true);
  915. //lcd_return_to_status();
  916. #else
  917. bool correct;
  918. max_feedrate[E_AXIS] =80;
  919. //max_feedrate[E_AXIS] = 50;
  920. START:
  921. lcd_clear();
  922. lcd_set_cursor(0, 0);
  923. switch (extruder) {
  924. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  925. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  926. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  927. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  928. }
  929. KEEPALIVE_STATE(PAUSED_FOR_USER);
  930. do{
  931. extr_mov(0.001,1000);
  932. delay_keep_alive(2);
  933. } while (!lcd_clicked());
  934. //delay_keep_alive(500);
  935. KEEPALIVE_STATE(IN_HANDLER);
  936. st_synchronize();
  937. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  938. //if (!correct) goto START;
  939. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  940. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  941. extr_mov(bowden_length[extruder], 500);
  942. lcd_clear();
  943. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  944. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  945. else lcd_print(" ");
  946. lcd_print(mmu_extruder + 1);
  947. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  948. st_synchronize();
  949. max_feedrate[E_AXIS] = 50;
  950. lcd_update_enable(true);
  951. lcd_return_to_status();
  952. lcdDrawUpdate = 2;
  953. #endif
  954. }
  955. struct E_step
  956. {
  957. float extrude; //!< extrude distance in mm
  958. float feed_rate; //!< feed rate in mm/s
  959. };
  960. static const E_step ramming_sequence[] PROGMEM =
  961. {
  962. {1.0, 1000.0/60},
  963. {1.0, 1500.0/60},
  964. {2.0, 2000.0/60},
  965. {1.5, 3000.0/60},
  966. {2.5, 4000.0/60},
  967. {-15.0, 5000.0/60},
  968. {-14.0, 1200.0/60},
  969. {-6.0, 600.0/60},
  970. {10.0, 700.0/60},
  971. {-10.0, 400.0/60},
  972. {-50.0, 2000.0/60},
  973. };
  974. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  975. void mmu_filament_ramming()
  976. {
  977. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  978. {
  979. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  980. plan_buffer_line_curposXYZE(pgm_read_float(&(ramming_sequence[i].feed_rate)));
  981. st_synchronize();
  982. }
  983. }
  984. //! @brief show which filament is currently unloaded
  985. void extr_unload_view()
  986. {
  987. lcd_clear();
  988. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  989. lcd_print(" ");
  990. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(" ");
  991. else lcd_print(mmu_extruder + 1);
  992. }
  993. void extr_unload()
  994. { //unload just current filament for multimaterial printers
  995. #ifdef SNMM
  996. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  997. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  998. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  999. #endif
  1000. if (degHotend0() > EXTRUDE_MINTEMP)
  1001. {
  1002. #ifndef SNMM
  1003. st_synchronize();
  1004. menu_submenu(extr_unload_view);
  1005. mmu_filament_ramming();
  1006. mmu_command(MmuCmd::U0);
  1007. // get response
  1008. manage_response(false, true, MMU_UNLOAD_MOVE);
  1009. menu_back();
  1010. #else //SNMM
  1011. lcd_clear();
  1012. lcd_display_message_fullscreen_P(PSTR(""));
  1013. max_feedrate[E_AXIS] = 50;
  1014. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  1015. lcd_print(" ");
  1016. lcd_print(mmu_extruder + 1);
  1017. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  1018. if (current_position[Z_AXIS] < 15) {
  1019. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  1020. plan_buffer_line_curposXYZE(25, active_extruder);
  1021. }
  1022. current_position[E_AXIS] += 10; //extrusion
  1023. plan_buffer_line_curposXYZE(10, active_extruder);
  1024. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  1025. if (current_temperature[0] < 230) { //PLA & all other filaments
  1026. current_position[E_AXIS] += 5.4;
  1027. plan_buffer_line_curposXYZE(2800 / 60, active_extruder);
  1028. current_position[E_AXIS] += 3.2;
  1029. plan_buffer_line_curposXYZE(3000 / 60, active_extruder);
  1030. current_position[E_AXIS] += 3;
  1031. plan_buffer_line_curposXYZE(3400 / 60, active_extruder);
  1032. }
  1033. else { //ABS
  1034. current_position[E_AXIS] += 3.1;
  1035. plan_buffer_line_curposXYZE(2000 / 60, active_extruder);
  1036. current_position[E_AXIS] += 3.1;
  1037. plan_buffer_line_curposXYZE(2500 / 60, active_extruder);
  1038. current_position[E_AXIS] += 4;
  1039. plan_buffer_line_curposXYZE(3000 / 60, active_extruder);
  1040. /*current_position[X_AXIS] += 23; //delay
  1041. plan_buffer_line_curposXYZE(600 / 60, active_extruder); //delay
  1042. current_position[X_AXIS] -= 23; //delay
  1043. plan_buffer_line_curposXYZE(600 / 60, active_extruder); //delay*/
  1044. delay_keep_alive(4700);
  1045. }
  1046. max_feedrate[E_AXIS] = 80;
  1047. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1048. plan_buffer_line_curposXYZE(500, active_extruder);
  1049. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1050. plan_buffer_line_curposXYZE(500, active_extruder);
  1051. st_synchronize();
  1052. //st_current_init();
  1053. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  1054. else st_current_set(2, tmp_motor_loud[2]);
  1055. lcd_update_enable(true);
  1056. lcd_return_to_status();
  1057. max_feedrate[E_AXIS] = 50;
  1058. #endif //SNMM
  1059. }
  1060. else
  1061. {
  1062. show_preheat_nozzle_warning();
  1063. }
  1064. }
  1065. //wrapper functions for loading filament
  1066. void extr_adj_0()
  1067. {
  1068. #ifndef SNMM
  1069. enquecommand_P(PSTR("M701 E0"));
  1070. #else
  1071. change_extr(0);
  1072. extr_adj(0);
  1073. #endif
  1074. }
  1075. void extr_adj_1()
  1076. {
  1077. #ifndef SNMM
  1078. enquecommand_P(PSTR("M701 E1"));
  1079. #else
  1080. change_extr(1);
  1081. extr_adj(1);
  1082. #endif
  1083. }
  1084. void extr_adj_2()
  1085. {
  1086. #ifndef SNMM
  1087. enquecommand_P(PSTR("M701 E2"));
  1088. #else
  1089. change_extr(2);
  1090. extr_adj(2);
  1091. #endif
  1092. }
  1093. void extr_adj_3()
  1094. {
  1095. #ifndef SNMM
  1096. enquecommand_P(PSTR("M701 E3"));
  1097. #else
  1098. change_extr(3);
  1099. extr_adj(3);
  1100. #endif
  1101. }
  1102. void extr_adj_4()
  1103. {
  1104. #ifndef SNMM
  1105. enquecommand_P(PSTR("M701 E4"));
  1106. #else
  1107. change_extr(4);
  1108. extr_adj(4);
  1109. #endif
  1110. }
  1111. void load_all()
  1112. {
  1113. #ifndef SNMM
  1114. enquecommand_P(PSTR("M701 E0"));
  1115. enquecommand_P(PSTR("M701 E1"));
  1116. enquecommand_P(PSTR("M701 E2"));
  1117. enquecommand_P(PSTR("M701 E3"));
  1118. enquecommand_P(PSTR("M701 E4"));
  1119. #else
  1120. for (int i = 0; i < 4; i++)
  1121. {
  1122. change_extr(i);
  1123. extr_adj(i);
  1124. }
  1125. #endif
  1126. }
  1127. //wrapper functions for changing extruders
  1128. void extr_change_0()
  1129. {
  1130. change_extr(0);
  1131. lcd_return_to_status();
  1132. }
  1133. void extr_change_1()
  1134. {
  1135. change_extr(1);
  1136. lcd_return_to_status();
  1137. }
  1138. void extr_change_2()
  1139. {
  1140. change_extr(2);
  1141. lcd_return_to_status();
  1142. }
  1143. void extr_change_3()
  1144. {
  1145. change_extr(3);
  1146. lcd_return_to_status();
  1147. }
  1148. #ifdef SNMM
  1149. //wrapper functions for unloading filament
  1150. void extr_unload_all()
  1151. {
  1152. if (degHotend0() > EXTRUDE_MINTEMP)
  1153. {
  1154. for (int i = 0; i < 4; i++)
  1155. {
  1156. change_extr(i);
  1157. extr_unload();
  1158. }
  1159. }
  1160. else
  1161. {
  1162. show_preheat_nozzle_warning();
  1163. lcd_return_to_status();
  1164. }
  1165. }
  1166. //unloading just used filament (for snmm)
  1167. void extr_unload_used()
  1168. {
  1169. if (degHotend0() > EXTRUDE_MINTEMP) {
  1170. for (int i = 0; i < 4; i++) {
  1171. if (snmm_filaments_used & (1 << i)) {
  1172. change_extr(i);
  1173. extr_unload();
  1174. }
  1175. }
  1176. snmm_filaments_used = 0;
  1177. }
  1178. else {
  1179. show_preheat_nozzle_warning();
  1180. lcd_return_to_status();
  1181. }
  1182. }
  1183. #endif //SNMM
  1184. void extr_unload_0()
  1185. {
  1186. change_extr(0);
  1187. extr_unload();
  1188. }
  1189. void extr_unload_1()
  1190. {
  1191. change_extr(1);
  1192. extr_unload();
  1193. }
  1194. void extr_unload_2()
  1195. {
  1196. change_extr(2);
  1197. extr_unload();
  1198. }
  1199. void extr_unload_3()
  1200. {
  1201. change_extr(3);
  1202. extr_unload();
  1203. }
  1204. void extr_unload_4()
  1205. {
  1206. change_extr(4);
  1207. extr_unload();
  1208. }
  1209. bool mmu_check_version()
  1210. {
  1211. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  1212. }
  1213. void mmu_show_warning()
  1214. {
  1215. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  1216. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  1217. }
  1218. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  1219. {
  1220. menu_back();
  1221. bFilamentAction = false; // NOT in "mmu_load_to_nozzle_menu()"
  1222. if (degHotend0() > EXTRUDE_MINTEMP)
  1223. {
  1224. tmp_extruder = filament_nr;
  1225. lcd_update_enable(false);
  1226. lcd_clear();
  1227. lcd_set_cursor(0, 1);
  1228. lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  1229. lcd_print(" ");
  1230. lcd_print(tmp_extruder + 1);
  1231. mmu_command(MmuCmd::T0 + tmp_extruder);
  1232. manage_response(true, true, MMU_TCODE_MOVE);
  1233. mmu_continue_loading(false);
  1234. mmu_extruder = tmp_extruder; //filament change is finished
  1235. raise_z_above(MIN_Z_FOR_LOAD, false);
  1236. mmu_load_to_nozzle();
  1237. load_filament_final_feed();
  1238. st_synchronize();
  1239. custom_message_type = CustomMsg::FilamentLoading;
  1240. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  1241. lcd_return_to_status();
  1242. lcd_update_enable(true);
  1243. lcd_load_filament_color_check();
  1244. lcd_setstatuspgm(_T(WELCOME_MSG));
  1245. custom_message_type = CustomMsg::Status;
  1246. }
  1247. else
  1248. {
  1249. show_preheat_nozzle_warning();
  1250. }
  1251. }
  1252. #ifdef MMU_HAS_CUTTER
  1253. void mmu_cut_filament(uint8_t filament_nr)
  1254. {
  1255. menu_back();
  1256. bFilamentAction=false; // NOT in "mmu_load_to_nozzle_menu()"
  1257. if (degHotend0() > EXTRUDE_MINTEMP)
  1258. {
  1259. LcdUpdateDisabler disableLcdUpdate;
  1260. lcd_clear();
  1261. lcd_set_cursor(0, 1); lcd_puts_P(_i("Cutting filament")); //// c=18
  1262. lcd_print(" ");
  1263. lcd_print(filament_nr + 1);
  1264. mmu_filament_ramming();
  1265. mmu_command(MmuCmd::K0 + filament_nr);
  1266. manage_response(false, false, MMU_UNLOAD_MOVE);
  1267. }
  1268. else
  1269. {
  1270. show_preheat_nozzle_warning();
  1271. }
  1272. }
  1273. #endif //MMU_HAS_CUTTER
  1274. void mmu_eject_filament(uint8_t filament, bool recover)
  1275. {
  1276. //-//
  1277. bFilamentAction=false; // NOT in "mmu_fil_eject_menu()"
  1278. if (filament < 5)
  1279. {
  1280. if (degHotend0() > EXTRUDE_MINTEMP)
  1281. {
  1282. st_synchronize();
  1283. {
  1284. LcdUpdateDisabler disableLcdUpdate;
  1285. lcd_clear();
  1286. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  1287. mmu_filament_ramming();
  1288. mmu_command(MmuCmd::E0 + filament);
  1289. manage_response(false, false, MMU_UNLOAD_MOVE);
  1290. if (recover)
  1291. {
  1292. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  1293. mmu_command(MmuCmd::R0);
  1294. manage_response(false, false);
  1295. }
  1296. }
  1297. }
  1298. else
  1299. {
  1300. show_preheat_nozzle_warning();
  1301. }
  1302. }
  1303. else
  1304. {
  1305. puts_P(PSTR("Filament nr out of range!"));
  1306. }
  1307. }
  1308. //! @brief Fits filament tip into heatbreak?
  1309. //!
  1310. //! If PTFE tube is jammed, this causes filament to be unloaded and no longer
  1311. //! being detected by the pulley IR sensor.
  1312. //! @retval true Fits
  1313. //! @retval false Doesn't fit
  1314. static bool can_load()
  1315. {
  1316. current_position[E_AXIS] += 60;
  1317. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1318. current_position[E_AXIS] -= 52;
  1319. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1320. st_synchronize();
  1321. uint_least8_t filament_detected_count = 0;
  1322. const float e_increment = 0.2;
  1323. const uint_least8_t steps = 6.0 / e_increment;
  1324. DEBUG_PUTS_P(PSTR("MMU can_load:"));
  1325. for(uint_least8_t i = 0; i < steps; ++i)
  1326. {
  1327. current_position[E_AXIS] -= e_increment;
  1328. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1329. st_synchronize();
  1330. if(0 == READ(IR_SENSOR_PIN))
  1331. {
  1332. ++filament_detected_count;
  1333. DEBUG_PUTCHAR('O');
  1334. }
  1335. else
  1336. {
  1337. DEBUG_PUTCHAR('o');
  1338. }
  1339. }
  1340. if (filament_detected_count > steps - 4)
  1341. {
  1342. DEBUG_PUTS_P(PSTR(" succeeded."));
  1343. return true;
  1344. }
  1345. else
  1346. {
  1347. DEBUG_PUTS_P(PSTR(" failed."));
  1348. return false;
  1349. }
  1350. }
  1351. //! @brief load more
  1352. //!
  1353. //! Try to feed more filament from MMU if it is not detected by filament sensor.
  1354. //! @retval true Success, filament detected by IR sensor
  1355. //! @retval false Failed, filament not detected by IR sensor after maximum number of attempts
  1356. static bool load_more()
  1357. {
  1358. for (uint8_t i = 0; i < MMU_IDLER_SENSOR_ATTEMPTS_NR; i++)
  1359. {
  1360. if (READ(IR_SENSOR_PIN) == 0) return true;
  1361. DEBUG_PRINTF_P(PSTR("Additional load attempt nr. %d\n"), i);
  1362. mmu_command(MmuCmd::C0);
  1363. manage_response(true, true, MMU_LOAD_MOVE);
  1364. }
  1365. return false;
  1366. }
  1367. static void increment_load_fail()
  1368. {
  1369. uint8_t mmu_load_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL);
  1370. uint16_t mmu_load_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT);
  1371. if(mmu_load_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, mmu_load_fail + 1);
  1372. if(mmu_load_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, mmu_load_fail_tot + 1);
  1373. }
  1374. //! @brief continue loading filament
  1375. //! @par blocking
  1376. //! * true blocking - do not return until successful load
  1377. //! * false non-blocking - pause print and return on load failure
  1378. //!
  1379. //! @startuml
  1380. //! [*] --> [*] : !ir_sensor_detected /\n send MmuCmd::C0
  1381. //! [*] --> LoadMore
  1382. //! LoadMore --> [*] : filament \ndetected
  1383. //! LoadMore --> Retry : !filament detected /\n increment load fail
  1384. //! Retry --> [*] : filament \ndetected
  1385. //! Retry --> Unload : !filament \ndetected
  1386. //! Unload --> [*] : non-blocking
  1387. //! Unload --> Retry : button \nclicked
  1388. //!
  1389. //! Retry : Cut filament if enabled
  1390. //! Retry : repeat last T-code
  1391. //! Unload : unload filament
  1392. //! Unload : pause print
  1393. //! Unload : show error message
  1394. //!
  1395. //! @enduml
  1396. void mmu_continue_loading(bool blocking)
  1397. {
  1398. if (!ir_sensor_detected)
  1399. {
  1400. mmu_command(MmuCmd::C0);
  1401. return;
  1402. }
  1403. bool success = load_more();
  1404. if (success) success = can_load();
  1405. enum class Ls : uint_least8_t
  1406. {
  1407. Enter,
  1408. Retry,
  1409. Unload,
  1410. };
  1411. Ls state = Ls::Enter;
  1412. const uint_least8_t max_retry = 3;
  1413. uint_least8_t retry = 0;
  1414. while (!success)
  1415. {
  1416. switch (state)
  1417. {
  1418. case Ls::Enter:
  1419. increment_load_fail();
  1420. // no break
  1421. case Ls::Retry:
  1422. ++retry; // overflow not handled, as it is not dangerous.
  1423. if (retry >= max_retry)
  1424. {
  1425. state = Ls::Unload;
  1426. #ifdef MMU_HAS_CUTTER
  1427. if (1 == eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED))
  1428. {
  1429. mmu_command(MmuCmd::K0 + tmp_extruder);
  1430. manage_response(true, true, MMU_UNLOAD_MOVE);
  1431. }
  1432. #endif //MMU_HAS_CUTTER
  1433. }
  1434. mmu_command(MmuCmd::T0 + tmp_extruder);
  1435. manage_response(true, true, MMU_TCODE_MOVE);
  1436. success = load_more();
  1437. if (success) success = can_load();
  1438. break;
  1439. case Ls::Unload:
  1440. stop_and_save_print_to_ram(0, 0);
  1441. long_pause();
  1442. mmu_command(MmuCmd::U0);
  1443. manage_response(false, true, MMU_UNLOAD_MOVE);
  1444. setAllTargetHotends(0);
  1445. lcd_setstatuspgm(_i("MMU load failed "));////c=20 r=1
  1446. if (blocking)
  1447. {
  1448. marlin_wait_for_click();
  1449. st_synchronize();
  1450. restore_print_from_ram_and_continue(0);
  1451. state = Ls::Retry;
  1452. }
  1453. else
  1454. {
  1455. mmu_fil_loaded = false; //so we can retry same T-code again
  1456. isPrintPaused = true;
  1457. mmu_command(MmuCmd::W0);
  1458. return;
  1459. }
  1460. break;
  1461. }
  1462. }
  1463. }