mmu.cpp 44 KB

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