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