mmu.cpp 46 KB

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