mmu.cpp 44 KB

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