mmu.cpp 43 KB

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