mmu.cpp 45 KB

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