mmu.cpp 47 KB

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