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 "cmdqueue.h"
  12. #include "ultralcd.h"
  13. #include "sound.h"
  14. #include "printers.h"
  15. #include <avr/pgmspace.h>
  16. #include "AutoDeplete.h"
  17. #include "fastio.h"
  18. #include "pins.h"
  19. //-//
  20. #include "util.h"
  21. #ifdef TMC2130
  22. #include "tmc2130.h"
  23. #endif //TMC2130
  24. #define MMU_TODELAY 100
  25. #define MMU_TIMEOUT 10
  26. #define MMU_CMD_TIMEOUT 45000ul //45s timeout for mmu commands (except P0)
  27. #define MMU_P0_TIMEOUT 3000ul //timeout for P0 command: 3seconds
  28. #define MMU_MAX_RESEND_ATTEMPTS 2
  29. namespace
  30. {
  31. enum class S : uint_least8_t
  32. {
  33. WaitStealthMode,
  34. GetFindaInit,
  35. GetBuildNr,
  36. GetVersion,
  37. Init,
  38. Disabled,
  39. Idle,
  40. GetFinda,
  41. WaitCmd, //!< wait for command response
  42. Pause,
  43. GetDrvError, //!< get power failures count
  44. SwitchMode //switch mmu between stealth and normal mode
  45. };
  46. }
  47. bool mmu_enabled = false;
  48. bool mmu_ready = false;
  49. bool mmu_fil_loaded = false; //if true: blocks execution of duplicit T-codes
  50. static S mmu_state = S::Disabled;
  51. MmuCmd mmu_cmd = MmuCmd::None;
  52. //idler ir sensor
  53. static uint8_t mmu_idl_sens = 0;
  54. bool ir_sensor_detected = false;
  55. 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
  56. uint8_t mmu_extruder = MMU_FILAMENT_UNKNOWN;
  57. //! This variable probably has no meaning and is planed to be removed
  58. uint8_t tmp_extruder = MMU_FILAMENT_UNKNOWN;
  59. int8_t mmu_finda = -1;
  60. uint32_t mmu_last_finda_response = 0;
  61. int16_t mmu_version = -1;
  62. int16_t mmu_buildnr = -1;
  63. uint32_t mmu_last_request = 0;
  64. uint32_t mmu_last_response = 0;
  65. MmuCmd mmu_last_cmd = MmuCmd::None;
  66. uint16_t mmu_power_failures = 0;
  67. #ifdef MMU_DEBUG
  68. static const auto DEBUG_PUTCHAR = putchar;
  69. static const auto DEBUG_PUTS_P = puts_P;
  70. static const auto DEBUG_PRINTF_P = printf_P;
  71. #else //MMU_DEBUG
  72. #define DEBUG_PUTCHAR(c)
  73. #define DEBUG_PUTS_P(str)
  74. #define DEBUG_PRINTF_P( __fmt, ... )
  75. #endif //MMU_DEBUG
  76. #if defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
  77. static const auto FDEBUG_PUTS_P = puts_P;
  78. static const auto FDEBUG_PRINTF_P = printf_P;
  79. #else
  80. #define FDEBUG_PUTS_P(str)
  81. #define FDEBUG_PRINTF_P( __fmt, ... )
  82. #endif //defined(MMU_FINDA_DEBUG) && defined(MMU_DEBUG)
  83. //clear rx buffer
  84. void mmu_clr_rx_buf(void)
  85. {
  86. while (fgetc(uart2io) >= 0);
  87. }
  88. //send command - puts
  89. int mmu_puts_P(const char* str)
  90. {
  91. mmu_clr_rx_buf(); //clear rx buffer
  92. int r = fputs_P(str, uart2io); //send command
  93. mmu_last_request = _millis();
  94. return r;
  95. }
  96. //send command - printf
  97. int mmu_printf_P(const char* format, ...)
  98. {
  99. va_list args;
  100. va_start(args, format);
  101. mmu_clr_rx_buf(); //clear rx buffer
  102. int r = vfprintf_P(uart2io, format, args); //send command
  103. va_end(args);
  104. mmu_last_request = _millis();
  105. return r;
  106. }
  107. //check 'ok' response
  108. int8_t mmu_rx_ok(void)
  109. {
  110. int8_t res = uart2_rx_str_P(PSTR("ok\n"));
  111. if (res == 1) mmu_last_response = _millis();
  112. return res;
  113. }
  114. //check 'start' response
  115. int8_t mmu_rx_start(void)
  116. {
  117. int8_t res = uart2_rx_str_P(PSTR("start\n"));
  118. if (res == 1) mmu_last_response = _millis();
  119. return res;
  120. }
  121. //initialize mmu2 unit - first part - should be done at begining of startup process
  122. void mmu_init(void)
  123. {
  124. #ifdef MMU_HWRESET
  125. digitalWrite(MMU_RST_PIN, HIGH);
  126. pinMode(MMU_RST_PIN, OUTPUT); //setup reset pin
  127. #endif //MMU_HWRESET
  128. uart2_init(); //init uart2
  129. _delay_ms(10); //wait 10ms for sure
  130. mmu_reset(); //reset mmu (HW or SW), do not wait for response
  131. mmu_state = S::Init;
  132. SET_INPUT(IR_SENSOR_PIN); //input mode
  133. WRITE(IR_SENSOR_PIN, 1); //pullup
  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. #ifdef PAT9125
  146. && fsensor_not_responding
  147. #endif //PAT9125
  148. )
  149. {
  150. detected = true;
  151. //printf_P(PSTR("Idler IR sensor detected\n"));
  152. }
  153. else
  154. {
  155. //printf_P(PSTR("Idler IR sensor not detected\n"));
  156. }
  157. return detected;
  158. #endif //IR_SENSOR
  159. }
  160. static bool activate_stealth_mode()
  161. {
  162. #ifdef MMU_FORCE_STEALTH_MODE
  163. return true;
  164. #else
  165. return (eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) == 1);
  166. #endif
  167. }
  168. //mmu main loop - state machine processing
  169. void mmu_loop(void)
  170. {
  171. static uint8_t mmu_attempt_nr = 0;
  172. // printf_P(PSTR("MMU loop, state=%d\n"), mmu_state);
  173. switch (mmu_state)
  174. {
  175. case S::Disabled:
  176. return;
  177. case S::Init:
  178. if (mmu_rx_start() > 0)
  179. {
  180. DEBUG_PUTS_P(PSTR("MMU => 'start'"));
  181. DEBUG_PUTS_P(PSTR("MMU <= 'S1'"));
  182. mmu_puts_P(PSTR("S1\n")); //send 'read version' request
  183. mmu_state = S::GetVersion;
  184. }
  185. else if (_millis() > 30000) //30sec after reset disable mmu
  186. {
  187. puts_P(PSTR("MMU not responding - DISABLED"));
  188. mmu_state = S::Disabled;
  189. }
  190. return;
  191. case S::GetVersion:
  192. if (mmu_rx_ok() > 0)
  193. {
  194. fscanf_P(uart2io, PSTR("%u"), &mmu_version); //scan version from buffer
  195. DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_version);
  196. DEBUG_PUTS_P(PSTR("MMU <= 'S2'"));
  197. mmu_puts_P(PSTR("S2\n")); //send 'read buildnr' request
  198. mmu_state = S::GetBuildNr;
  199. }
  200. return;
  201. case S::GetBuildNr:
  202. if (mmu_rx_ok() > 0)
  203. {
  204. fscanf_P(uart2io, PSTR("%u"), &mmu_buildnr); //scan buildnr from buffer
  205. DEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_buildnr);
  206. bool version_valid = mmu_check_version();
  207. if (!version_valid) mmu_show_warning();
  208. else puts_P(PSTR("MMU version valid"));
  209. if (!activate_stealth_mode())
  210. {
  211. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  212. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  213. mmu_state = S::GetFindaInit;
  214. }
  215. else
  216. {
  217. DEBUG_PUTS_P(PSTR("MMU <= 'M1'"));
  218. mmu_puts_P(PSTR("M1\n")); //set mmu mode to stealth
  219. mmu_state = S::WaitStealthMode;
  220. }
  221. }
  222. return;
  223. case S::WaitStealthMode:
  224. if (mmu_rx_ok() > 0)
  225. {
  226. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  227. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  228. mmu_state = S::GetFindaInit;
  229. }
  230. return;
  231. case S::GetFindaInit:
  232. if (mmu_rx_ok() > 0)
  233. {
  234. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  235. mmu_last_finda_response = _millis();
  236. FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  237. puts_P(PSTR("MMU - ENABLED"));
  238. mmu_enabled = true;
  239. //-//
  240. // ... PrinterType/Name
  241. fSetMmuMode(true);
  242. mmu_state = S::Idle;
  243. }
  244. return;
  245. case S::Idle:
  246. if (mmu_cmd != MmuCmd::None) //command request ?
  247. {
  248. if ((mmu_cmd >= MmuCmd::T0) && (mmu_cmd <= MmuCmd::T4))
  249. {
  250. const uint8_t filament = mmu_cmd - MmuCmd::T0;
  251. DEBUG_PRINTF_P(PSTR("MMU <= 'T%d'\n"), filament);
  252. mmu_printf_P(PSTR("T%d\n"), filament);
  253. mmu_state = S::WaitCmd; // wait for response
  254. mmu_fil_loaded = true;
  255. mmu_idl_sens = 1;
  256. }
  257. else if ((mmu_cmd >= MmuCmd::L0) && (mmu_cmd <= MmuCmd::L4))
  258. {
  259. const uint8_t filament = mmu_cmd - MmuCmd::L0;
  260. DEBUG_PRINTF_P(PSTR("MMU <= 'L%d'\n"), filament);
  261. mmu_printf_P(PSTR("L%d\n"), filament);
  262. mmu_state = S::WaitCmd; // wait for response
  263. }
  264. else if (mmu_cmd == MmuCmd::C0)
  265. {
  266. DEBUG_PRINTF_P(PSTR("MMU <= 'C0'\n"));
  267. mmu_puts_P(PSTR("C0\n")); //send 'continue loading'
  268. mmu_state = S::WaitCmd;
  269. mmu_idl_sens = 1;
  270. }
  271. else if (mmu_cmd == MmuCmd::U0)
  272. {
  273. DEBUG_PRINTF_P(PSTR("MMU <= 'U0'\n"));
  274. mmu_puts_P(PSTR("U0\n")); //send 'unload current filament'
  275. mmu_fil_loaded = false;
  276. mmu_state = S::WaitCmd;
  277. }
  278. else if ((mmu_cmd >= MmuCmd::E0) && (mmu_cmd <= MmuCmd::E4))
  279. {
  280. const uint8_t filament = mmu_cmd - MmuCmd::E0;
  281. DEBUG_PRINTF_P(PSTR("MMU <= 'E%d'\n"), filament);
  282. mmu_printf_P(PSTR("E%d\n"), filament); //send eject filament
  283. mmu_fil_loaded = false;
  284. mmu_state = S::WaitCmd;
  285. }
  286. else if ((mmu_cmd >= MmuCmd::K0) && (mmu_cmd <= MmuCmd::K4))
  287. {
  288. const uint8_t filament = mmu_cmd - MmuCmd::K0;
  289. DEBUG_PRINTF_P(PSTR("MMU <= 'K%d'\n"), filament);
  290. mmu_printf_P(PSTR("K%d\n"), filament); //send eject filament
  291. mmu_fil_loaded = false;
  292. mmu_state = S::WaitCmd;
  293. }
  294. else if (mmu_cmd == MmuCmd::R0)
  295. {
  296. DEBUG_PRINTF_P(PSTR("MMU <= 'R0'\n"));
  297. mmu_puts_P(PSTR("R0\n")); //send recover after eject
  298. mmu_state = S::WaitCmd;
  299. }
  300. else if (mmu_cmd == MmuCmd::S3)
  301. {
  302. DEBUG_PRINTF_P(PSTR("MMU <= 'S3'\n"));
  303. mmu_puts_P(PSTR("S3\n")); //send power failures request
  304. mmu_state = S::GetDrvError;
  305. }
  306. else if (mmu_cmd == MmuCmd::W0)
  307. {
  308. DEBUG_PRINTF_P(PSTR("MMU <= 'W0'\n"));
  309. mmu_puts_P(PSTR("W0\n"));
  310. mmu_state = S::Pause;
  311. }
  312. mmu_last_cmd = mmu_cmd;
  313. mmu_cmd = MmuCmd::None;
  314. }
  315. else if ((eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH) != SilentModeMenu_MMU) && mmu_ready) {
  316. DEBUG_PRINTF_P(PSTR("MMU <= 'M%d'\n"), SilentModeMenu_MMU);
  317. mmu_printf_P(PSTR("M%d\n"), SilentModeMenu_MMU);
  318. mmu_state = S::SwitchMode;
  319. }
  320. else if ((mmu_last_response + 300) < _millis()) //request every 300ms
  321. {
  322. #ifndef IR_SENSOR
  323. if(check_for_ir_sensor()) ir_sensor_detected = true;
  324. #endif //IR_SENSOR not defined
  325. FDEBUG_PUTS_P(PSTR("MMU <= 'P0'"));
  326. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  327. mmu_state = S::GetFinda;
  328. }
  329. return;
  330. case S::GetFinda: //response to command P0
  331. if (mmu_idl_sens)
  332. {
  333. if (READ(IR_SENSOR_PIN) == 0 && mmu_loading_flag)
  334. {
  335. #ifdef MMU_DEBUG
  336. printf_P(PSTR("MMU <= 'A'\n"));
  337. #endif //MMU_DEBUG
  338. mmu_puts_P(PSTR("A\n")); //send 'abort' request
  339. mmu_idl_sens = 0;
  340. //printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
  341. }
  342. //else
  343. //printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
  344. }
  345. if (mmu_rx_ok() > 0)
  346. {
  347. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  348. mmu_last_finda_response = _millis();
  349. FDEBUG_PRINTF_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  350. //printf_P(PSTR("Eact: %d\n"), int(e_active()));
  351. if (!mmu_finda && CHECK_FSENSOR && fsensor_enabled) {
  352. fsensor_checkpoint_print();
  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 + MMU_P0_TIMEOUT) < _millis())
  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 + MMU_CMD_TIMEOUT) < _millis())
  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 + MMU_CMD_TIMEOUT) < _millis())
  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 + MMU_CMD_TIMEOUT) < _millis())
  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. digitalWrite(MMU_RST_PIN, LOW);
  460. _delay_us(100);
  461. digitalWrite(MMU_RST_PIN, HIGH);
  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."));
  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."));////c=20 r=4
  676. else lcd_display_message_fullscreen_P(_i("Fix the issue and then press button on MMU unit."));////c=20 r=4
  677. screen=0;
  678. }
  679. lcd_set_degree();
  680. //5 seconds delay
  681. for (uint8_t i = 0; i < 5; i++) {
  682. if (lcd_clicked()) {
  683. setTargetHotend(hotend_temp_bckp, active_extruder);
  684. /// mmu_cmd = mmu_last_cmd;
  685. break;
  686. }
  687. //Print the hotend temperature (9 chars total) and fill rest of the line with space
  688. lcd_set_cursor(0, 4); //line 4
  689. int chars = lcd_printf_P(_N("%c%3d/%d%c"), LCD_STR_THERMOMETER[0],(int)(degHotend(active_extruder) + 0.5), (int)(degTargetHotend(active_extruder) + 0.5), LCD_STR_DEGREE[0]);
  690. lcd_space(9 - chars);
  691. delay_keep_alive(1000);
  692. }
  693. }
  694. else if (mmu_print_saved) {
  695. puts_P(PSTR("MMU starts responding"));
  696. KEEPALIVE_STATE(IN_HANDLER);
  697. mmu_loading_flag = false;
  698. if (turn_off_nozzle)
  699. {
  700. lcd_clear();
  701. setTargetHotend(hotend_temp_bckp, active_extruder);
  702. if (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
  703. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature..."));
  704. delay_keep_alive(3000);
  705. }
  706. mmu_wait_for_heater_blocking();
  707. }
  708. if (move_axes) {
  709. lcd_clear();
  710. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming position..."));
  711. current_position[X_AXIS] = x_position_bckp;
  712. current_position[Y_AXIS] = y_position_bckp;
  713. plan_buffer_line_curposXYZE(50);
  714. st_synchronize();
  715. current_position[Z_AXIS] = z_position_bckp;
  716. plan_buffer_line_curposXYZE(15);
  717. st_synchronize();
  718. }
  719. else {
  720. lcd_clear();
  721. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming..."));
  722. delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
  723. }
  724. }
  725. }
  726. if (lcd_update_was_enabled) lcd_update_enable(true);
  727. #ifdef TMC2130
  728. //enable extruder motor (disabled in mmu_command, start of T-code processing)
  729. tmc2130_set_pwr(E_AXIS, 1);
  730. //printf_P(PSTR("E-axis enabled\n"));
  731. #endif //TMC2130
  732. }
  733. //! @brief load filament to nozzle of multimaterial printer
  734. //!
  735. //! This function is used only only after T? (user select filament) and M600 (change filament).
  736. //! It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
  737. //! filament to nozzle.
  738. //!
  739. void mmu_load_to_nozzle()
  740. {
  741. st_synchronize();
  742. const bool saved_e_relative_mode = axis_relative_modes & E_AXIS_MASK;
  743. if (!saved_e_relative_mode) axis_relative_modes |= E_AXIS_MASK;
  744. if (ir_sensor_detected)
  745. {
  746. current_position[E_AXIS] += 3.0f;
  747. }
  748. else
  749. {
  750. current_position[E_AXIS] += 7.2f;
  751. }
  752. float feedrate = 562;
  753. plan_buffer_line_curposXYZE(feedrate / 60);
  754. st_synchronize();
  755. current_position[E_AXIS] += 14.4f;
  756. feedrate = 871;
  757. plan_buffer_line_curposXYZE(feedrate / 60);
  758. st_synchronize();
  759. current_position[E_AXIS] += 36.0f;
  760. feedrate = 1393;
  761. plan_buffer_line_curposXYZE(feedrate / 60);
  762. st_synchronize();
  763. current_position[E_AXIS] += 14.4f;
  764. feedrate = 871;
  765. plan_buffer_line_curposXYZE(feedrate / 60);
  766. st_synchronize();
  767. if (!saved_e_relative_mode) axis_relative_modes &= ~E_AXIS_MASK;
  768. }
  769. void mmu_M600_wait_and_beep() {
  770. //Beep and wait for user to remove old filament and prepare new filament for load
  771. KEEPALIVE_STATE(PAUSED_FOR_USER);
  772. int counterBeep = 0;
  773. lcd_display_message_fullscreen_P(_i("Remove old filament and press the knob to start loading new filament.")); ////MSG_REMOVE_OLD_FILAMENT c=20 r=5
  774. bool bFirst=true;
  775. while (!lcd_clicked()){
  776. manage_heater();
  777. manage_inactivity(true);
  778. #if BEEPER > 0
  779. if (counterBeep == 500) {
  780. counterBeep = 0;
  781. }
  782. SET_OUTPUT(BEEPER);
  783. if (counterBeep == 0) {
  784. if((eSoundMode==e_SOUND_MODE_BLIND)|| (eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
  785. {
  786. bFirst=false;
  787. WRITE(BEEPER, HIGH);
  788. }
  789. }
  790. if (counterBeep == 20) {
  791. WRITE(BEEPER, LOW);
  792. }
  793. counterBeep++;
  794. #endif //BEEPER > 0
  795. delay_keep_alive(4);
  796. }
  797. WRITE(BEEPER, LOW);
  798. }
  799. //! @brief load filament for mmu v2
  800. //! @par nozzle_temp nozzle temperature to load filament
  801. void mmu_M600_load_filament(bool automatic, float nozzle_temp)
  802. {
  803. tmp_extruder = mmu_extruder;
  804. if (!automatic)
  805. {
  806. #ifdef MMU_M600_SWITCH_EXTRUDER
  807. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  808. if(yes) tmp_extruder = choose_extruder_menu();
  809. #endif //MMU_M600_SWITCH_EXTRUDER
  810. }
  811. else
  812. {
  813. tmp_extruder = ad_getAlternative(tmp_extruder);
  814. }
  815. lcd_update_enable(false);
  816. lcd_clear();
  817. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  818. lcd_print(' ');
  819. lcd_print(tmp_extruder + 1);
  820. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  821. //printf_P(PSTR("T code: %d \n"), tmp_extruder);
  822. //mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  823. setTargetHotend(nozzle_temp,active_extruder);
  824. mmu_wait_for_heater_blocking();
  825. mmu_command(MmuCmd::T0 + tmp_extruder);
  826. manage_response(false, true, MMU_LOAD_MOVE);
  827. mmu_continue_loading(is_usb_printing || (lcd_commands_type == LcdCommands::Layer1Cal));
  828. mmu_extruder = tmp_extruder; //filament change is finished
  829. mmu_load_to_nozzle();
  830. load_filament_final_feed();
  831. st_synchronize();
  832. }
  833. #ifdef SNMM
  834. void extr_mov(float shift, float feed_rate)
  835. { //move extruder no matter what the current heater temperature is
  836. set_extrude_min_temp(.0);
  837. current_position[E_AXIS] += shift;
  838. plan_buffer_line_curposXYZE(feed_rate, active_extruder);
  839. set_extrude_min_temp(EXTRUDE_MINTEMP);
  840. }
  841. #endif //SNMM
  842. void change_extr(int
  843. #ifdef SNMM
  844. extr
  845. #endif //SNMM
  846. ) { //switches multiplexer for extruders
  847. #ifdef SNMM
  848. st_synchronize();
  849. _delay(100);
  850. disable_e0();
  851. disable_e1();
  852. disable_e2();
  853. mmu_extruder = extr;
  854. pinMode(E_MUX0_PIN, OUTPUT);
  855. pinMode(E_MUX1_PIN, OUTPUT);
  856. switch (extr) {
  857. case 1:
  858. WRITE(E_MUX0_PIN, HIGH);
  859. WRITE(E_MUX1_PIN, LOW);
  860. break;
  861. case 2:
  862. WRITE(E_MUX0_PIN, LOW);
  863. WRITE(E_MUX1_PIN, HIGH);
  864. break;
  865. case 3:
  866. WRITE(E_MUX0_PIN, HIGH);
  867. WRITE(E_MUX1_PIN, HIGH);
  868. break;
  869. default:
  870. WRITE(E_MUX0_PIN, LOW);
  871. WRITE(E_MUX1_PIN, LOW);
  872. break;
  873. }
  874. _delay(100);
  875. #endif
  876. }
  877. int get_ext_nr()
  878. { //reads multiplexer input pins and return current extruder number (counted from 0)
  879. #ifndef SNMM
  880. return(mmu_extruder); //update needed
  881. #else
  882. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  883. #endif
  884. }
  885. void display_loading()
  886. {
  887. switch (mmu_extruder)
  888. {
  889. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  890. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  891. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  892. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  893. }
  894. }
  895. void extr_adj(uint8_t extruder) //loading filament for SNMM
  896. {
  897. #ifndef SNMM
  898. MmuCmd cmd = MmuCmd::L0 + extruder;
  899. if (extruder > (MmuCmd::L4 - MmuCmd::L0))
  900. {
  901. printf_P(PSTR("Filament out of range %d \n"),extruder);
  902. return;
  903. }
  904. mmu_command(cmd);
  905. //show which filament is currently loaded
  906. lcd_update_enable(false);
  907. lcd_clear();
  908. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  909. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  910. //else lcd.print(" ");
  911. lcd_print(' ');
  912. lcd_print(extruder + 1);
  913. // get response
  914. manage_response(false, false);
  915. lcd_update_enable(true);
  916. //lcd_return_to_status();
  917. #else
  918. bool correct;
  919. max_feedrate[E_AXIS] =80;
  920. //max_feedrate[E_AXIS] = 50;
  921. START:
  922. lcd_clear();
  923. lcd_set_cursor(0, 0);
  924. switch (extruder) {
  925. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  926. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  927. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  928. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  929. }
  930. KEEPALIVE_STATE(PAUSED_FOR_USER);
  931. do{
  932. extr_mov(0.001,1000);
  933. delay_keep_alive(2);
  934. } while (!lcd_clicked());
  935. //delay_keep_alive(500);
  936. KEEPALIVE_STATE(IN_HANDLER);
  937. st_synchronize();
  938. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  939. //if (!correct) goto START;
  940. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  941. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  942. extr_mov(bowden_length[extruder], 500);
  943. lcd_clear();
  944. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  945. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  946. else lcd_print(' ');
  947. lcd_print(mmu_extruder + 1);
  948. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  949. st_synchronize();
  950. max_feedrate[E_AXIS] = 50;
  951. lcd_update_enable(true);
  952. lcd_return_to_status();
  953. lcdDrawUpdate = 2;
  954. #endif
  955. }
  956. struct E_step
  957. {
  958. float extrude; //!< extrude distance in mm
  959. float feed_rate; //!< feed rate in mm/s
  960. };
  961. static const E_step ramming_sequence[] PROGMEM =
  962. {
  963. {1.0, 1000.0/60},
  964. {1.0, 1500.0/60},
  965. {2.0, 2000.0/60},
  966. {1.5, 3000.0/60},
  967. {2.5, 4000.0/60},
  968. {-15.0, 5000.0/60},
  969. {-14.0, 1200.0/60},
  970. {-6.0, 600.0/60},
  971. {10.0, 700.0/60},
  972. {-10.0, 400.0/60},
  973. {-50.0, 2000.0/60},
  974. };
  975. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  976. void mmu_filament_ramming()
  977. {
  978. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  979. {
  980. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  981. plan_buffer_line_curposXYZE(pgm_read_float(&(ramming_sequence[i].feed_rate)));
  982. st_synchronize();
  983. }
  984. }
  985. //! @brief show which filament is currently unloaded
  986. void extr_unload_view()
  987. {
  988. lcd_clear();
  989. lcd_puts_at_P(0, 1, _T(MSG_UNLOADING_FILAMENT));
  990. lcd_print(' ');
  991. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(' ');
  992. else lcd_print(mmu_extruder + 1);
  993. }
  994. void extr_unload()
  995. { //unload just current filament for multimaterial printers
  996. #ifdef SNMM
  997. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  998. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  999. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  1000. #endif
  1001. if (degHotend0() > EXTRUDE_MINTEMP)
  1002. {
  1003. #ifndef SNMM
  1004. st_synchronize();
  1005. menu_submenu(extr_unload_view);
  1006. mmu_filament_ramming();
  1007. mmu_command(MmuCmd::U0);
  1008. // get response
  1009. manage_response(false, true, MMU_UNLOAD_MOVE);
  1010. menu_back();
  1011. #else //SNMM
  1012. lcd_clear();
  1013. lcd_display_message_fullscreen_P(PSTR(""));
  1014. max_feedrate[E_AXIS] = 50;
  1015. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  1016. lcd_print(' ');
  1017. lcd_print(mmu_extruder + 1);
  1018. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  1019. if (current_position[Z_AXIS] < 15) {
  1020. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  1021. plan_buffer_line_curposXYZE(25, active_extruder);
  1022. }
  1023. current_position[E_AXIS] += 10; //extrusion
  1024. plan_buffer_line_curposXYZE(10, active_extruder);
  1025. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  1026. if (current_temperature[0] < 230) { //PLA & all other filaments
  1027. current_position[E_AXIS] += 5.4;
  1028. plan_buffer_line_curposXYZE(2800 / 60, active_extruder);
  1029. current_position[E_AXIS] += 3.2;
  1030. plan_buffer_line_curposXYZE(3000 / 60, active_extruder);
  1031. current_position[E_AXIS] += 3;
  1032. plan_buffer_line_curposXYZE(3400 / 60, active_extruder);
  1033. }
  1034. else { //ABS
  1035. current_position[E_AXIS] += 3.1;
  1036. plan_buffer_line_curposXYZE(2000 / 60, active_extruder);
  1037. current_position[E_AXIS] += 3.1;
  1038. plan_buffer_line_curposXYZE(2500 / 60, active_extruder);
  1039. current_position[E_AXIS] += 4;
  1040. plan_buffer_line_curposXYZE(3000 / 60, active_extruder);
  1041. /*current_position[X_AXIS] += 23; //delay
  1042. plan_buffer_line_curposXYZE(600 / 60, active_extruder); //delay
  1043. current_position[X_AXIS] -= 23; //delay
  1044. plan_buffer_line_curposXYZE(600 / 60, active_extruder); //delay*/
  1045. delay_keep_alive(4700);
  1046. }
  1047. max_feedrate[E_AXIS] = 80;
  1048. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1049. plan_buffer_line_curposXYZE(500, active_extruder);
  1050. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1051. plan_buffer_line_curposXYZE(500, active_extruder);
  1052. st_synchronize();
  1053. //st_current_init();
  1054. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  1055. else st_current_set(2, tmp_motor_loud[2]);
  1056. lcd_update_enable(true);
  1057. lcd_return_to_status();
  1058. max_feedrate[E_AXIS] = 50;
  1059. #endif //SNMM
  1060. }
  1061. else
  1062. {
  1063. show_preheat_nozzle_warning();
  1064. }
  1065. }
  1066. //wrapper functions for loading filament
  1067. void extr_adj_0()
  1068. {
  1069. #ifndef SNMM
  1070. enquecommand_P(PSTR("M701 E0"));
  1071. #else
  1072. change_extr(0);
  1073. extr_adj(0);
  1074. #endif
  1075. }
  1076. void extr_adj_1()
  1077. {
  1078. #ifndef SNMM
  1079. enquecommand_P(PSTR("M701 E1"));
  1080. #else
  1081. change_extr(1);
  1082. extr_adj(1);
  1083. #endif
  1084. }
  1085. void extr_adj_2()
  1086. {
  1087. #ifndef SNMM
  1088. enquecommand_P(PSTR("M701 E2"));
  1089. #else
  1090. change_extr(2);
  1091. extr_adj(2);
  1092. #endif
  1093. }
  1094. void extr_adj_3()
  1095. {
  1096. #ifndef SNMM
  1097. enquecommand_P(PSTR("M701 E3"));
  1098. #else
  1099. change_extr(3);
  1100. extr_adj(3);
  1101. #endif
  1102. }
  1103. void extr_adj_4()
  1104. {
  1105. #ifndef SNMM
  1106. enquecommand_P(PSTR("M701 E4"));
  1107. #else
  1108. change_extr(4);
  1109. extr_adj(4);
  1110. #endif
  1111. }
  1112. void load_all()
  1113. {
  1114. #ifndef SNMM
  1115. enquecommand_P(PSTR("M701 E0"));
  1116. enquecommand_P(PSTR("M701 E1"));
  1117. enquecommand_P(PSTR("M701 E2"));
  1118. enquecommand_P(PSTR("M701 E3"));
  1119. enquecommand_P(PSTR("M701 E4"));
  1120. #else
  1121. for (int i = 0; i < 4; i++)
  1122. {
  1123. change_extr(i);
  1124. extr_adj(i);
  1125. }
  1126. #endif
  1127. }
  1128. //wrapper functions for changing extruders
  1129. void extr_change_0()
  1130. {
  1131. change_extr(0);
  1132. lcd_return_to_status();
  1133. }
  1134. void extr_change_1()
  1135. {
  1136. change_extr(1);
  1137. lcd_return_to_status();
  1138. }
  1139. void extr_change_2()
  1140. {
  1141. change_extr(2);
  1142. lcd_return_to_status();
  1143. }
  1144. void extr_change_3()
  1145. {
  1146. change_extr(3);
  1147. lcd_return_to_status();
  1148. }
  1149. #ifdef SNMM
  1150. //wrapper functions for unloading filament
  1151. void extr_unload_all()
  1152. {
  1153. if (degHotend0() > EXTRUDE_MINTEMP)
  1154. {
  1155. for (int i = 0; i < 4; i++)
  1156. {
  1157. change_extr(i);
  1158. extr_unload();
  1159. }
  1160. }
  1161. else
  1162. {
  1163. show_preheat_nozzle_warning();
  1164. lcd_return_to_status();
  1165. }
  1166. }
  1167. //unloading just used filament (for snmm)
  1168. void extr_unload_used()
  1169. {
  1170. if (degHotend0() > EXTRUDE_MINTEMP) {
  1171. for (int i = 0; i < 4; i++) {
  1172. if (snmm_filaments_used & (1 << i)) {
  1173. change_extr(i);
  1174. extr_unload();
  1175. }
  1176. }
  1177. snmm_filaments_used = 0;
  1178. }
  1179. else {
  1180. show_preheat_nozzle_warning();
  1181. lcd_return_to_status();
  1182. }
  1183. }
  1184. #endif //SNMM
  1185. void extr_unload_0()
  1186. {
  1187. change_extr(0);
  1188. extr_unload();
  1189. }
  1190. void extr_unload_1()
  1191. {
  1192. change_extr(1);
  1193. extr_unload();
  1194. }
  1195. void extr_unload_2()
  1196. {
  1197. change_extr(2);
  1198. extr_unload();
  1199. }
  1200. void extr_unload_3()
  1201. {
  1202. change_extr(3);
  1203. extr_unload();
  1204. }
  1205. void extr_unload_4()
  1206. {
  1207. change_extr(4);
  1208. extr_unload();
  1209. }
  1210. bool mmu_check_version()
  1211. {
  1212. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  1213. }
  1214. void mmu_show_warning()
  1215. {
  1216. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  1217. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  1218. }
  1219. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  1220. {
  1221. menu_back();
  1222. bFilamentAction = false; // NOT in "mmu_load_to_nozzle_menu()"
  1223. if (degHotend0() > EXTRUDE_MINTEMP)
  1224. {
  1225. tmp_extruder = filament_nr;
  1226. lcd_update_enable(false);
  1227. lcd_clear();
  1228. lcd_puts_at_P(0, 1, _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_puts_at_P(0, 1, _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_puts_at_P(0, 1, _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. }