mmu.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. //! @file
  2. #include "mmu.h"
  3. #include "planner.h"
  4. #include "language.h"
  5. #include "lcd.h"
  6. #include "uart2.h"
  7. #include "temperature.h"
  8. #include "Configuration_prusa.h"
  9. #include "fsensor.h"
  10. #include "cardreader.h"
  11. #include "cmdqueue.h"
  12. #include "ultralcd.h"
  13. #include "menu.h"
  14. #include "sound.h"
  15. #include "printers.h"
  16. #include <avr/pgmspace.h>
  17. #include "AutoDeplete.h"
  18. #include "fastio.h"
  19. #include "pins.h"
  20. //-//
  21. #include "util.h"
  22. #ifdef TMC2130
  23. #include "tmc2130.h"
  24. #endif //TMC2130
  25. #define MMU_TODELAY 100
  26. #define MMU_TIMEOUT 10
  27. #define MMU_CMD_TIMEOUT 45000ul //45s timeout for mmu commands (except P0)
  28. #define MMU_P0_TIMEOUT 3000ul //timeout for P0 command: 3seconds
  29. #define MMU_MAX_RESEND_ATTEMPTS 2
  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. ShortTimer mmu_last_request;
  64. ShortTimer mmu_last_response;
  65. ShortTimer mmu_last_finda_response;
  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.start();
  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.start();
  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.start();
  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.start();
  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. WRITE(MMU_RST_PIN, 1);
  127. SET_OUTPUT(MMU_RST_PIN); //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. SET_INPUT(IR_SENSOR_PIN); //input mode
  134. WRITE(IR_SENSOR_PIN, 1); //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 ((READ(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. MUST BE %hhu!!!
  236. mmu_last_finda_response.start();
  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.expired(300)) //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 (READ(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. MUST BE %hhu!!!
  349. mmu_last_finda_response.start();
  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_checkpoint_print();
  354. if (mmu_extruder != MMU_FILAMENT_UNKNOWN) // Can't deplete unknown extruder.
  355. ad_markDepleted(mmu_extruder);
  356. if (lcd_autoDepleteEnabled() && !ad_allDepleted() && mmu_extruder != MMU_FILAMENT_UNKNOWN) // Can't auto if F=?
  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.expired(MMU_P0_TIMEOUT))
  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 (READ(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.expired(MMU_CMD_TIMEOUT))
  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.expired(MMU_CMD_TIMEOUT))
  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.expired(MMU_CMD_TIMEOUT))
  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. WRITE(MMU_RST_PIN, 0);
  461. _delay_us(100);
  462. WRITE(MMU_RST_PIN, 1);
  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);
  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: puts_P(PSTR("load")); break;
  533. case MMU_UNLOAD_MOVE: puts_P(PSTR("unload")); break;
  534. case MMU_TCODE_MOVE: puts_P(PSTR("T-code")); break;
  535. case MMU_NO_MOVE: puts_P(PSTR("no move")); break;
  536. default: puts_P(PSTR("error: unknown move")); 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 (READ(IR_SENSOR_PIN) == 0) move = MMU_NO_MOVE;
  557. break;
  558. case MMU_UNLOAD_MOVE:
  559. if (READ(IR_SENSOR_PIN) == 0) //filament is still detected by idler sensor, printer helps with unlading
  560. {
  561. if (can_extrude())
  562. {
  563. puts_P(PSTR("Unload 1"));
  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);
  566. st_synchronize();
  567. }
  568. }
  569. else //filament was unloaded from idler, no additional movements needed
  570. {
  571. puts_P(PSTR("Unloading finished 1"));
  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 (READ(IR_SENSOR_PIN) == 0) //filament detected by idler sensor, we must unload first
  578. {
  579. if (can_extrude())
  580. {
  581. puts_P(PSTR("Unload 2"));
  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);
  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. puts_P(PSTR("Unloading finished 2"));
  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. puts_P(PSTR("MMU not responding"));
  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);
  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);
  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."));////MSG_MMU_USER_ATTENTION c=20 r=3
  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."));////MSG_RESUME_NOZZLE_TEMP c=20 r=4
  677. else lcd_display_message_fullscreen_P(_i("Fix the issue and then press button on MMU unit."));////MSG_MMU_FIX_ISSUE c=20 r=4
  678. screen=0;
  679. }
  680. //5 seconds delay
  681. for (uint8_t i = 0; i < 5; i++) {
  682. if (lcd_clicked()) {
  683. setTargetHotend(hotend_temp_bckp, active_extruder);
  684. /// mmu_cmd = mmu_last_cmd;
  685. break;
  686. }
  687. //Print the hotend temperature (9 chars total) and fill rest of the line with space
  688. lcd_set_cursor(0, 4); //line 4
  689. int chars = lcd_printf_P(_N("%c%3d/%d%c"), LCD_STR_THERMOMETER[0],(int)(degHotend(active_extruder) + 0.5), (int)(degTargetHotend(active_extruder) + 0.5), LCD_STR_DEGREE[0]);
  690. lcd_space(9 - chars);
  691. delay_keep_alive(1000);
  692. }
  693. }
  694. else if (mmu_print_saved) {
  695. puts_P(PSTR("MMU starts responding"));
  696. KEEPALIVE_STATE(IN_HANDLER);
  697. mmu_loading_flag = false;
  698. if (turn_off_nozzle)
  699. {
  700. lcd_clear();
  701. setTargetHotend(hotend_temp_bckp, active_extruder);
  702. if (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
  703. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature..."));
  704. delay_keep_alive(3000);
  705. }
  706. mmu_wait_for_heater_blocking();
  707. }
  708. if (move_axes) {
  709. lcd_clear();
  710. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming position..."));
  711. current_position[X_AXIS] = x_position_bckp;
  712. current_position[Y_AXIS] = y_position_bckp;
  713. plan_buffer_line_curposXYZE(50);
  714. st_synchronize();
  715. current_position[Z_AXIS] = z_position_bckp;
  716. plan_buffer_line_curposXYZE(15);
  717. st_synchronize();
  718. }
  719. else {
  720. lcd_clear();
  721. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming..."));
  722. delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
  723. }
  724. }
  725. }
  726. if (lcd_update_was_enabled) lcd_update_enable(true);
  727. #ifdef TMC2130
  728. //enable extruder motor (disabled in mmu_command, start of T-code processing)
  729. tmc2130_set_pwr(E_AXIS, 1);
  730. //printf_P(PSTR("E-axis enabled\n"));
  731. #endif //TMC2130
  732. }
  733. //! @brief load filament to nozzle of multimaterial printer
  734. //!
  735. //! This function is used only only after T? (user select filament) and M600 (change filament).
  736. //! It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
  737. //! filament to nozzle.
  738. //!
  739. void mmu_load_to_nozzle()
  740. {
  741. st_synchronize();
  742. const bool saved_e_relative_mode = axis_relative_modes & E_AXIS_MASK;
  743. if (!saved_e_relative_mode) axis_relative_modes |= E_AXIS_MASK;
  744. if (ir_sensor_detected)
  745. {
  746. current_position[E_AXIS] += 3.0f;
  747. }
  748. else
  749. {
  750. current_position[E_AXIS] += 7.2f;
  751. }
  752. float feedrate = 562;
  753. plan_buffer_line_curposXYZE(feedrate / 60);
  754. st_synchronize();
  755. current_position[E_AXIS] += 14.4f;
  756. feedrate = 871;
  757. plan_buffer_line_curposXYZE(feedrate / 60);
  758. st_synchronize();
  759. current_position[E_AXIS] += 36.0f;
  760. feedrate = 1393;
  761. plan_buffer_line_curposXYZE(feedrate / 60);
  762. st_synchronize();
  763. current_position[E_AXIS] += 14.4f;
  764. feedrate = 871;
  765. plan_buffer_line_curposXYZE(feedrate / 60);
  766. st_synchronize();
  767. if (!saved_e_relative_mode) axis_relative_modes &= ~E_AXIS_MASK;
  768. }
  769. void mmu_M600_wait_and_beep() {
  770. //Beep and wait for user to remove old filament and prepare new filament for load
  771. KEEPALIVE_STATE(PAUSED_FOR_USER);
  772. int counterBeep = 0;
  773. lcd_display_message_fullscreen_P(_i("Remove old filament and press the knob to start loading new filament.")); ////MSG_REMOVE_OLD_FILAMENT c=20 r=5
  774. bool bFirst=true;
  775. while (!lcd_clicked()){
  776. manage_heater();
  777. manage_inactivity(true);
  778. #if BEEPER > 0
  779. if (counterBeep == 500) {
  780. counterBeep = 0;
  781. }
  782. SET_OUTPUT(BEEPER);
  783. if (counterBeep == 0) {
  784. if((eSoundMode==e_SOUND_MODE_BLIND)|| (eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
  785. {
  786. bFirst=false;
  787. WRITE(BEEPER, HIGH);
  788. }
  789. }
  790. if (counterBeep == 20) {
  791. WRITE(BEEPER, LOW);
  792. }
  793. counterBeep++;
  794. #endif //BEEPER > 0
  795. delay_keep_alive(4);
  796. }
  797. WRITE(BEEPER, LOW);
  798. }
  799. //! @brief load filament for mmu v2
  800. //! @par nozzle_temp nozzle temperature to load filament
  801. void mmu_M600_load_filament(bool automatic, float nozzle_temp)
  802. {
  803. tmp_extruder = mmu_extruder;
  804. if (!automatic)
  805. {
  806. #ifdef MMU_M600_SWITCH_EXTRUDER
  807. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  808. if(yes) tmp_extruder = choose_extruder_menu();
  809. #endif //MMU_M600_SWITCH_EXTRUDER
  810. }
  811. else
  812. {
  813. tmp_extruder = ad_getAlternative(tmp_extruder);
  814. }
  815. lcd_update_enable(false);
  816. lcd_clear();
  817. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  818. lcd_print(' ');
  819. lcd_print(tmp_extruder + 1);
  820. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  821. //printf_P(PSTR("T code: %d \n"), tmp_extruder);
  822. //mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  823. setTargetHotend(nozzle_temp,active_extruder);
  824. mmu_wait_for_heater_blocking();
  825. mmu_command(MmuCmd::T0 + tmp_extruder);
  826. manage_response(false, true, MMU_LOAD_MOVE);
  827. mmu_continue_loading(usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal));
  828. mmu_extruder = tmp_extruder; //filament change is finished
  829. mmu_load_to_nozzle();
  830. load_filament_final_feed();
  831. st_synchronize();
  832. }
  833. #ifdef SNMM
  834. void extr_mov(float shift, float feed_rate)
  835. { //move extruder no matter what the current heater temperature is
  836. set_extrude_min_temp(.0);
  837. current_position[E_AXIS] += shift;
  838. plan_buffer_line_curposXYZE(feed_rate, active_extruder);
  839. set_extrude_min_temp(EXTRUDE_MINTEMP);
  840. }
  841. #endif //SNMM
  842. void change_extr(int
  843. #ifdef SNMM
  844. extr
  845. #endif //SNMM
  846. ) { //switches multiplexer for extruders
  847. #ifdef SNMM
  848. st_synchronize();
  849. _delay(100);
  850. disable_e0();
  851. disable_e1();
  852. disable_e2();
  853. mmu_extruder = extr;
  854. SET_OUTPUT(E_MUX0_PIN);
  855. SET_OUTPUT(E_MUX1_PIN);
  856. switch (extr) {
  857. case 1:
  858. WRITE(E_MUX0_PIN, HIGH);
  859. WRITE(E_MUX1_PIN, LOW);
  860. break;
  861. case 2:
  862. WRITE(E_MUX0_PIN, LOW);
  863. WRITE(E_MUX1_PIN, HIGH);
  864. break;
  865. case 3:
  866. WRITE(E_MUX0_PIN, HIGH);
  867. WRITE(E_MUX1_PIN, HIGH);
  868. break;
  869. default:
  870. WRITE(E_MUX0_PIN, LOW);
  871. WRITE(E_MUX1_PIN, LOW);
  872. break;
  873. }
  874. _delay(100);
  875. #endif
  876. }
  877. int get_ext_nr()
  878. { //reads multiplexer input pins and return current extruder number (counted from 0)
  879. #ifndef SNMM
  880. return(mmu_extruder); //update needed
  881. #else
  882. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  883. #endif
  884. }
  885. void display_loading()
  886. {
  887. switch (mmu_extruder)
  888. {
  889. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  890. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  891. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  892. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  893. }
  894. }
  895. void extr_adj(uint8_t extruder) //loading filament for SNMM
  896. {
  897. #ifndef SNMM
  898. MmuCmd cmd = MmuCmd::L0 + extruder;
  899. if (extruder > (MmuCmd::L4 - MmuCmd::L0))
  900. {
  901. printf_P(PSTR("Filament out of range %d \n"),extruder);
  902. return;
  903. }
  904. mmu_command(cmd);
  905. //show which filament is currently loaded
  906. lcd_update_enable(false);
  907. lcd_clear();
  908. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  909. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  910. //else lcd.print(" ");
  911. lcd_print(' ');
  912. lcd_print(extruder + 1);
  913. // get response
  914. manage_response(false, false);
  915. lcd_update_enable(true);
  916. //lcd_return_to_status();
  917. #else
  918. bool correct;
  919. max_feedrate[E_AXIS] =80;
  920. //max_feedrate[E_AXIS] = 50;
  921. START:
  922. lcd_clear();
  923. lcd_set_cursor(0, 0);
  924. switch (extruder) {
  925. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  926. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  927. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  928. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  929. }
  930. KEEPALIVE_STATE(PAUSED_FOR_USER);
  931. do{
  932. extr_mov(0.001,1000);
  933. delay_keep_alive(2);
  934. } while (!lcd_clicked());
  935. //delay_keep_alive(500);
  936. KEEPALIVE_STATE(IN_HANDLER);
  937. st_synchronize();
  938. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  939. //if (!correct) goto START;
  940. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  941. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  942. extr_mov(bowden_length[extruder], 500);
  943. lcd_clear();
  944. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  945. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  946. else lcd_print(' ');
  947. lcd_print(mmu_extruder + 1);
  948. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  949. st_synchronize();
  950. max_feedrate[E_AXIS] = 50;
  951. lcd_update_enable(true);
  952. lcd_return_to_status();
  953. lcdDrawUpdate = 2;
  954. #endif
  955. }
  956. struct E_step
  957. {
  958. float extrude; //!< extrude distance in mm
  959. float feed_rate; //!< feed rate in mm/s
  960. };
  961. static const E_step ramming_sequence[] PROGMEM =
  962. {
  963. {1.0, 1000.0/60},
  964. {1.0, 1500.0/60},
  965. {2.0, 2000.0/60},
  966. {1.5, 3000.0/60},
  967. {2.5, 4000.0/60},
  968. {-15.0, 5000.0/60},
  969. {-14.0, 1200.0/60},
  970. {-6.0, 600.0/60},
  971. {10.0, 700.0/60},
  972. {-10.0, 400.0/60},
  973. {-50.0, 2000.0/60},
  974. };
  975. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  976. void mmu_filament_ramming()
  977. {
  978. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  979. {
  980. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  981. plan_buffer_line_curposXYZE(pgm_read_float(&(ramming_sequence[i].feed_rate)));
  982. st_synchronize();
  983. }
  984. }
  985. //! @brief show which filament is currently unloaded
  986. void extr_unload_view()
  987. {
  988. lcd_clear();
  989. lcd_puts_at_P(0, 1, _T(MSG_UNLOADING_FILAMENT));
  990. lcd_print(' ');
  991. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(' ');
  992. else lcd_print(mmu_extruder + 1);
  993. }
  994. void extr_unload()
  995. { //unload just current filament for multimaterial printers
  996. #ifdef SNMM
  997. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  998. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  999. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  1000. #endif
  1001. if (degHotend0() > EXTRUDE_MINTEMP)
  1002. {
  1003. #ifndef SNMM
  1004. st_synchronize();
  1005. menu_submenu(extr_unload_view);
  1006. mmu_filament_ramming();
  1007. mmu_command(MmuCmd::U0);
  1008. // get response
  1009. manage_response(false, true, MMU_UNLOAD_MOVE);
  1010. menu_back();
  1011. #else //SNMM
  1012. lcd_clear();
  1013. lcd_display_message_fullscreen_P(PSTR(""));
  1014. max_feedrate[E_AXIS] = 50;
  1015. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  1016. lcd_print(' ');
  1017. lcd_print(mmu_extruder + 1);
  1018. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  1019. if (current_position[Z_AXIS] < 15) {
  1020. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  1021. plan_buffer_line_curposXYZE(25, active_extruder);
  1022. }
  1023. current_position[E_AXIS] += 10; //extrusion
  1024. plan_buffer_line_curposXYZE(10, active_extruder);
  1025. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  1026. if (current_temperature[0] < 230) { //PLA & all other filaments
  1027. current_position[E_AXIS] += 5.4;
  1028. plan_buffer_line_curposXYZE(2800 / 60, active_extruder);
  1029. current_position[E_AXIS] += 3.2;
  1030. plan_buffer_line_curposXYZE(3000 / 60, active_extruder);
  1031. current_position[E_AXIS] += 3;
  1032. plan_buffer_line_curposXYZE(3400 / 60, active_extruder);
  1033. }
  1034. else { //ABS
  1035. current_position[E_AXIS] += 3.1;
  1036. plan_buffer_line_curposXYZE(2000 / 60, active_extruder);
  1037. current_position[E_AXIS] += 3.1;
  1038. plan_buffer_line_curposXYZE(2500 / 60, active_extruder);
  1039. current_position[E_AXIS] += 4;
  1040. plan_buffer_line_curposXYZE(3000 / 60, active_extruder);
  1041. /*current_position[X_AXIS] += 23; //delay
  1042. plan_buffer_line_curposXYZE(600 / 60, active_extruder); //delay
  1043. current_position[X_AXIS] -= 23; //delay
  1044. plan_buffer_line_curposXYZE(600 / 60, active_extruder); //delay*/
  1045. delay_keep_alive(4700);
  1046. }
  1047. max_feedrate[E_AXIS] = 80;
  1048. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1049. plan_buffer_line_curposXYZE(500, active_extruder);
  1050. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1051. plan_buffer_line_curposXYZE(500, active_extruder);
  1052. st_synchronize();
  1053. //st_current_init();
  1054. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  1055. else st_current_set(2, tmp_motor_loud[2]);
  1056. lcd_update_enable(true);
  1057. lcd_return_to_status();
  1058. max_feedrate[E_AXIS] = 50;
  1059. #endif //SNMM
  1060. }
  1061. else
  1062. {
  1063. show_preheat_nozzle_warning();
  1064. }
  1065. }
  1066. //wrapper functions for loading filament
  1067. void extr_adj_0()
  1068. {
  1069. #ifndef SNMM
  1070. enquecommand_P(PSTR("M701 E0"));
  1071. #else
  1072. change_extr(0);
  1073. extr_adj(0);
  1074. #endif
  1075. }
  1076. void extr_adj_1()
  1077. {
  1078. #ifndef SNMM
  1079. enquecommand_P(PSTR("M701 E1"));
  1080. #else
  1081. change_extr(1);
  1082. extr_adj(1);
  1083. #endif
  1084. }
  1085. void extr_adj_2()
  1086. {
  1087. #ifndef SNMM
  1088. enquecommand_P(PSTR("M701 E2"));
  1089. #else
  1090. change_extr(2);
  1091. extr_adj(2);
  1092. #endif
  1093. }
  1094. void extr_adj_3()
  1095. {
  1096. #ifndef SNMM
  1097. enquecommand_P(PSTR("M701 E3"));
  1098. #else
  1099. change_extr(3);
  1100. extr_adj(3);
  1101. #endif
  1102. }
  1103. void extr_adj_4()
  1104. {
  1105. #ifndef SNMM
  1106. enquecommand_P(PSTR("M701 E4"));
  1107. #else
  1108. change_extr(4);
  1109. extr_adj(4);
  1110. #endif
  1111. }
  1112. void load_all()
  1113. {
  1114. #ifndef SNMM
  1115. enquecommand_P(PSTR("M701 E0"));
  1116. enquecommand_P(PSTR("M701 E1"));
  1117. enquecommand_P(PSTR("M701 E2"));
  1118. enquecommand_P(PSTR("M701 E3"));
  1119. enquecommand_P(PSTR("M701 E4"));
  1120. #else
  1121. for (int i = 0; i < 4; i++)
  1122. {
  1123. change_extr(i);
  1124. extr_adj(i);
  1125. }
  1126. #endif
  1127. }
  1128. //wrapper functions for changing extruders
  1129. void extr_change_0()
  1130. {
  1131. change_extr(0);
  1132. lcd_return_to_status();
  1133. }
  1134. void extr_change_1()
  1135. {
  1136. change_extr(1);
  1137. lcd_return_to_status();
  1138. }
  1139. void extr_change_2()
  1140. {
  1141. change_extr(2);
  1142. lcd_return_to_status();
  1143. }
  1144. void extr_change_3()
  1145. {
  1146. change_extr(3);
  1147. lcd_return_to_status();
  1148. }
  1149. #ifdef SNMM
  1150. //wrapper functions for unloading filament
  1151. void extr_unload_all()
  1152. {
  1153. if (degHotend0() > EXTRUDE_MINTEMP)
  1154. {
  1155. for (int i = 0; i < 4; i++)
  1156. {
  1157. change_extr(i);
  1158. extr_unload();
  1159. }
  1160. }
  1161. else
  1162. {
  1163. show_preheat_nozzle_warning();
  1164. lcd_return_to_status();
  1165. }
  1166. }
  1167. //unloading just used filament (for snmm)
  1168. void extr_unload_used()
  1169. {
  1170. if (degHotend0() > EXTRUDE_MINTEMP) {
  1171. for (int i = 0; i < 4; i++) {
  1172. if (snmm_filaments_used & (1 << i)) {
  1173. change_extr(i);
  1174. extr_unload();
  1175. }
  1176. }
  1177. snmm_filaments_used = 0;
  1178. }
  1179. else {
  1180. show_preheat_nozzle_warning();
  1181. lcd_return_to_status();
  1182. }
  1183. }
  1184. #endif //SNMM
  1185. void extr_unload_0()
  1186. {
  1187. change_extr(0);
  1188. extr_unload();
  1189. }
  1190. void extr_unload_1()
  1191. {
  1192. change_extr(1);
  1193. extr_unload();
  1194. }
  1195. void extr_unload_2()
  1196. {
  1197. change_extr(2);
  1198. extr_unload();
  1199. }
  1200. void extr_unload_3()
  1201. {
  1202. change_extr(3);
  1203. extr_unload();
  1204. }
  1205. void extr_unload_4()
  1206. {
  1207. change_extr(4);
  1208. extr_unload();
  1209. }
  1210. bool mmu_check_version()
  1211. {
  1212. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  1213. }
  1214. void mmu_show_warning()
  1215. {
  1216. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  1217. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  1218. }
  1219. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  1220. {
  1221. menu_back();
  1222. bFilamentAction = false; // NOT in "mmu_load_to_nozzle_menu()"
  1223. if (degHotend0() > EXTRUDE_MINTEMP)
  1224. {
  1225. tmp_extruder = filament_nr;
  1226. lcd_update_enable(false);
  1227. lcd_clear();
  1228. lcd_puts_at_P(0, 1, _T(MSG_LOADING_FILAMENT));
  1229. lcd_print(' ');
  1230. lcd_print(tmp_extruder + 1);
  1231. mmu_command(MmuCmd::T0 + tmp_extruder);
  1232. manage_response(true, true, MMU_TCODE_MOVE);
  1233. mmu_continue_loading(false);
  1234. mmu_extruder = tmp_extruder; //filament change is finished
  1235. raise_z_above(MIN_Z_FOR_LOAD, false);
  1236. mmu_load_to_nozzle();
  1237. load_filament_final_feed();
  1238. st_synchronize();
  1239. custom_message_type = CustomMsg::FilamentLoading;
  1240. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  1241. lcd_return_to_status();
  1242. lcd_update_enable(true);
  1243. lcd_load_filament_color_check();
  1244. lcd_setstatuspgm(MSG_WELCOME);
  1245. custom_message_type = CustomMsg::Status;
  1246. }
  1247. else
  1248. {
  1249. show_preheat_nozzle_warning();
  1250. }
  1251. }
  1252. #ifdef MMU_HAS_CUTTER
  1253. void mmu_cut_filament(uint8_t filament_nr)
  1254. {
  1255. menu_back();
  1256. bFilamentAction=false; // NOT in "mmu_load_to_nozzle_menu()"
  1257. if (degHotend0() > EXTRUDE_MINTEMP)
  1258. {
  1259. LcdUpdateDisabler disableLcdUpdate;
  1260. lcd_clear();
  1261. lcd_puts_at_P(0, 1, _i("Cutting filament")); ////MSG_MMU_CUTTING_FIL c=18
  1262. lcd_print(' ');
  1263. lcd_print(filament_nr + 1);
  1264. mmu_filament_ramming();
  1265. mmu_command(MmuCmd::K0 + filament_nr);
  1266. manage_response(false, false, MMU_UNLOAD_MOVE);
  1267. }
  1268. else
  1269. {
  1270. show_preheat_nozzle_warning();
  1271. }
  1272. }
  1273. #endif //MMU_HAS_CUTTER
  1274. void mmu_eject_filament(uint8_t filament, bool recover)
  1275. {
  1276. //-//
  1277. bFilamentAction=false; // NOT in "mmu_fil_eject_menu()"
  1278. if (filament < 5)
  1279. {
  1280. if (degHotend0() > EXTRUDE_MINTEMP)
  1281. {
  1282. st_synchronize();
  1283. {
  1284. LcdUpdateDisabler disableLcdUpdate;
  1285. lcd_clear();
  1286. lcd_puts_at_P(0, 1, _i("Ejecting filament"));
  1287. mmu_filament_ramming();
  1288. mmu_command(MmuCmd::E0 + filament);
  1289. manage_response(false, false, MMU_UNLOAD_MOVE);
  1290. if (recover)
  1291. {
  1292. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  1293. mmu_command(MmuCmd::R0);
  1294. manage_response(false, false);
  1295. }
  1296. }
  1297. }
  1298. else
  1299. {
  1300. show_preheat_nozzle_warning();
  1301. }
  1302. }
  1303. else
  1304. {
  1305. puts_P(PSTR("Filament nr out of range!"));
  1306. }
  1307. }
  1308. //! @brief Fits filament tip into heatbreak?
  1309. //!
  1310. //! If PTFE tube is jammed, this causes filament to be unloaded and no longer
  1311. //! being detected by the pulley IR sensor.
  1312. //! @retval true Fits
  1313. //! @retval false Doesn't fit
  1314. static bool can_load()
  1315. {
  1316. current_position[E_AXIS] += 60;
  1317. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1318. current_position[E_AXIS] -= 52;
  1319. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1320. st_synchronize();
  1321. uint_least8_t filament_detected_count = 0;
  1322. const float e_increment = 0.2;
  1323. const uint_least8_t steps = 6.0 / e_increment;
  1324. DEBUG_PUTS_P(PSTR("MMU can_load:"));
  1325. for(uint_least8_t i = 0; i < steps; ++i)
  1326. {
  1327. current_position[E_AXIS] -= e_increment;
  1328. plan_buffer_line_curposXYZE(MMU_LOAD_FEEDRATE);
  1329. st_synchronize();
  1330. if(0 == READ(IR_SENSOR_PIN))
  1331. {
  1332. ++filament_detected_count;
  1333. DEBUG_PUTCHAR('O');
  1334. }
  1335. else
  1336. {
  1337. DEBUG_PUTCHAR('o');
  1338. }
  1339. }
  1340. if (filament_detected_count > steps - 4)
  1341. {
  1342. DEBUG_PUTS_P(PSTR(" succeeded."));
  1343. return true;
  1344. }
  1345. else
  1346. {
  1347. DEBUG_PUTS_P(PSTR(" failed."));
  1348. return false;
  1349. }
  1350. }
  1351. //! @brief load more
  1352. //!
  1353. //! Try to feed more filament from MMU if it is not detected by filament sensor.
  1354. //! @retval true Success, filament detected by IR sensor
  1355. //! @retval false Failed, filament not detected by IR sensor after maximum number of attempts
  1356. static bool load_more()
  1357. {
  1358. for (uint8_t i = 0; i < MMU_IDLER_SENSOR_ATTEMPTS_NR; i++)
  1359. {
  1360. if (READ(IR_SENSOR_PIN) == 0) return true;
  1361. DEBUG_PRINTF_P(PSTR("Additional load attempt nr. %d\n"), i);
  1362. mmu_command(MmuCmd::C0);
  1363. manage_response(true, true, MMU_LOAD_MOVE);
  1364. }
  1365. return false;
  1366. }
  1367. static void increment_load_fail()
  1368. {
  1369. uint8_t mmu_load_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL);
  1370. uint16_t mmu_load_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT);
  1371. if(mmu_load_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, mmu_load_fail + 1);
  1372. if(mmu_load_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, mmu_load_fail_tot + 1);
  1373. }
  1374. //! @brief continue loading filament
  1375. //! @par blocking
  1376. //! * true blocking - do not return until successful load
  1377. //! * false non-blocking - pause print and return on load failure
  1378. //!
  1379. //! @startuml
  1380. //! [*] --> [*] : !ir_sensor_detected /\n send MmuCmd::C0
  1381. //! [*] --> LoadMore
  1382. //! LoadMore --> [*] : filament \ndetected
  1383. //! LoadMore --> Retry : !filament detected /\n increment load fail
  1384. //! Retry --> [*] : filament \ndetected
  1385. //! Retry --> Unload : !filament \ndetected
  1386. //! Unload --> [*] : non-blocking
  1387. //! Unload --> Retry : button \nclicked
  1388. //!
  1389. //! Retry : Cut filament if enabled
  1390. //! Retry : repeat last T-code
  1391. //! Unload : unload filament
  1392. //! Unload : pause print
  1393. //! Unload : show error message
  1394. //!
  1395. //! @enduml
  1396. void mmu_continue_loading(bool blocking)
  1397. {
  1398. if (!ir_sensor_detected)
  1399. {
  1400. mmu_command(MmuCmd::C0);
  1401. return;
  1402. }
  1403. bool success = load_more();
  1404. if (success) success = can_load();
  1405. enum class Ls : uint_least8_t
  1406. {
  1407. Enter,
  1408. Retry,
  1409. Unload,
  1410. };
  1411. Ls state = Ls::Enter;
  1412. const uint_least8_t max_retry = 3;
  1413. uint_least8_t retry = 0;
  1414. while (!success)
  1415. {
  1416. switch (state)
  1417. {
  1418. case Ls::Enter:
  1419. increment_load_fail();
  1420. // FALLTHRU
  1421. case Ls::Retry:
  1422. ++retry; // overflow not handled, as it is not dangerous.
  1423. if (retry >= max_retry)
  1424. {
  1425. state = Ls::Unload;
  1426. #ifdef MMU_HAS_CUTTER
  1427. if (1 == eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED))
  1428. {
  1429. mmu_command(MmuCmd::K0 + tmp_extruder);
  1430. manage_response(true, true, MMU_UNLOAD_MOVE);
  1431. }
  1432. #endif //MMU_HAS_CUTTER
  1433. }
  1434. mmu_command(MmuCmd::T0 + tmp_extruder);
  1435. manage_response(true, true, MMU_TCODE_MOVE);
  1436. success = load_more();
  1437. if (success) success = can_load();
  1438. break;
  1439. case Ls::Unload:
  1440. stop_and_save_print_to_ram(0, 0);
  1441. long_pause();
  1442. mmu_command(MmuCmd::U0);
  1443. manage_response(false, true, MMU_UNLOAD_MOVE);
  1444. setAllTargetHotends(0);
  1445. lcd_setstatuspgm(_i("MMU load failed"));////MSG_MMU_LOAD_FAILED c=20
  1446. if (blocking)
  1447. {
  1448. marlin_wait_for_click();
  1449. st_synchronize();
  1450. restore_print_from_ram_and_continue(0);
  1451. state = Ls::Retry;
  1452. }
  1453. else
  1454. {
  1455. mmu_fil_loaded = false; //so we can retry same T-code again
  1456. isPrintPaused = true;
  1457. mmu_command(MmuCmd::W0);
  1458. return;
  1459. }
  1460. break;
  1461. }
  1462. }
  1463. }