mmu.cpp 48 KB

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