mmu.cpp 46 KB

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