mmu.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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. void mmu_M600_load_filament(bool automatic)
  770. {
  771. //load filament for mmu v2
  772. tmp_extruder = mmu_extruder;
  773. if (!automatic) {
  774. #ifdef MMU_M600_SWITCH_EXTRUDER
  775. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  776. if(yes) tmp_extruder = choose_extruder_menu();
  777. #endif //MMU_M600_SWITCH_EXTRUDER
  778. }
  779. else {
  780. tmp_extruder = ad_getAlternative(tmp_extruder);
  781. }
  782. lcd_update_enable(false);
  783. lcd_clear();
  784. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  785. lcd_print(" ");
  786. lcd_print(tmp_extruder + 1);
  787. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  788. // printf_P(PSTR("T code: %d \n"), tmp_extruder);
  789. // mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  790. mmu_command(MmuCmd::T0 + tmp_extruder);
  791. manage_response(false, true, MMU_LOAD_MOVE);
  792. mmu_continue_loading();
  793. mmu_extruder = tmp_extruder; //filament change is finished
  794. mmu_load_to_nozzle();
  795. load_filament_final_feed();
  796. st_synchronize();
  797. }
  798. #ifdef SNMM
  799. void extr_mov(float shift, float feed_rate)
  800. { //move extruder no matter what the current heater temperature is
  801. set_extrude_min_temp(.0);
  802. current_position[E_AXIS] += shift;
  803. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feed_rate, active_extruder);
  804. set_extrude_min_temp(EXTRUDE_MINTEMP);
  805. }
  806. #endif //SNMM
  807. void change_extr(int
  808. #ifdef SNMM
  809. extr
  810. #endif //SNMM
  811. ) { //switches multiplexer for extruders
  812. #ifdef SNMM
  813. st_synchronize();
  814. _delay(100);
  815. disable_e0();
  816. disable_e1();
  817. disable_e2();
  818. mmu_extruder = extr;
  819. pinMode(E_MUX0_PIN, OUTPUT);
  820. pinMode(E_MUX1_PIN, OUTPUT);
  821. switch (extr) {
  822. case 1:
  823. WRITE(E_MUX0_PIN, HIGH);
  824. WRITE(E_MUX1_PIN, LOW);
  825. break;
  826. case 2:
  827. WRITE(E_MUX0_PIN, LOW);
  828. WRITE(E_MUX1_PIN, HIGH);
  829. break;
  830. case 3:
  831. WRITE(E_MUX0_PIN, HIGH);
  832. WRITE(E_MUX1_PIN, HIGH);
  833. break;
  834. default:
  835. WRITE(E_MUX0_PIN, LOW);
  836. WRITE(E_MUX1_PIN, LOW);
  837. break;
  838. }
  839. _delay(100);
  840. #endif
  841. }
  842. int get_ext_nr()
  843. { //reads multiplexer input pins and return current extruder number (counted from 0)
  844. #ifndef SNMM
  845. return(mmu_extruder); //update needed
  846. #else
  847. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  848. #endif
  849. }
  850. void display_loading()
  851. {
  852. switch (mmu_extruder)
  853. {
  854. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  855. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  856. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  857. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  858. }
  859. }
  860. void extr_adj(int extruder) //loading filament for SNMM
  861. {
  862. #ifndef SNMM
  863. MmuCmd cmd = MmuCmd::L0 + extruder;
  864. if (cmd > MmuCmd::L4)
  865. {
  866. printf_P(PSTR("Filament out of range %d \n"),extruder);
  867. return;
  868. }
  869. mmu_command(cmd);
  870. //show which filament is currently loaded
  871. lcd_update_enable(false);
  872. lcd_clear();
  873. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  874. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  875. //else lcd.print(" ");
  876. lcd_print(" ");
  877. lcd_print(extruder + 1);
  878. // get response
  879. manage_response(false, false);
  880. lcd_update_enable(true);
  881. //lcd_return_to_status();
  882. #else
  883. bool correct;
  884. max_feedrate[E_AXIS] =80;
  885. //max_feedrate[E_AXIS] = 50;
  886. START:
  887. lcd_clear();
  888. lcd_set_cursor(0, 0);
  889. switch (extruder) {
  890. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  891. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  892. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  893. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  894. }
  895. KEEPALIVE_STATE(PAUSED_FOR_USER);
  896. do{
  897. extr_mov(0.001,1000);
  898. delay_keep_alive(2);
  899. } while (!lcd_clicked());
  900. //delay_keep_alive(500);
  901. KEEPALIVE_STATE(IN_HANDLER);
  902. st_synchronize();
  903. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  904. //if (!correct) goto START;
  905. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  906. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  907. extr_mov(bowden_length[extruder], 500);
  908. lcd_clear();
  909. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  910. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  911. else lcd_print(" ");
  912. lcd_print(mmu_extruder + 1);
  913. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  914. st_synchronize();
  915. max_feedrate[E_AXIS] = 50;
  916. lcd_update_enable(true);
  917. lcd_return_to_status();
  918. lcdDrawUpdate = 2;
  919. #endif
  920. }
  921. struct E_step
  922. {
  923. float extrude; //!< extrude distance in mm
  924. float feed_rate; //!< feed rate in mm/s
  925. };
  926. static const E_step ramming_sequence[] PROGMEM =
  927. {
  928. {1.0, 1000.0/60},
  929. {1.0, 1500.0/60},
  930. {2.0, 2000.0/60},
  931. {1.5, 3000.0/60},
  932. {2.5, 4000.0/60},
  933. {-15.0, 5000.0/60},
  934. {-14.0, 1200.0/60},
  935. {-6.0, 600.0/60},
  936. {10.0, 700.0/60},
  937. {-10.0, 400.0/60},
  938. {-50.0, 2000.0/60},
  939. };
  940. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  941. void mmu_filament_ramming()
  942. {
  943. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  944. {
  945. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  946. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  947. current_position[E_AXIS], pgm_read_float(&(ramming_sequence[i].feed_rate)), active_extruder);
  948. st_synchronize();
  949. }
  950. }
  951. //-//
  952. void extr_unload_()
  953. {
  954. //if(bFilamentAction)
  955. if(0)
  956. {
  957. bFilamentAction=false;
  958. extr_unload();
  959. }
  960. else {
  961. eFilamentAction=e_FILAMENT_ACTION_mmuUnLoad;
  962. bFilamentFirstRun=false;
  963. if(target_temperature[0]>=EXTRUDE_MINTEMP)
  964. {
  965. bFilamentPreheatState=true;
  966. mFilamentItem(target_temperature[0],target_temperature_bed);
  967. }
  968. // else menu_submenu(mFilamentMenu);
  969. else mFilamentMenu();
  970. }
  971. }
  972. //! @brief show which filament is currently unloaded
  973. void extr_unload_view()
  974. {
  975. lcd_clear();
  976. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  977. lcd_print(" ");
  978. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(" ");
  979. else lcd_print(mmu_extruder + 1);
  980. }
  981. void extr_unload()
  982. { //unload just current filament for multimaterial printers
  983. #ifdef SNMM
  984. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  985. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  986. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  987. #endif
  988. if (degHotend0() > EXTRUDE_MINTEMP)
  989. {
  990. #ifndef SNMM
  991. st_synchronize();
  992. menu_submenu(extr_unload_view);
  993. mmu_filament_ramming();
  994. mmu_command(MmuCmd::U0);
  995. // get response
  996. manage_response(false, true, MMU_UNLOAD_MOVE);
  997. menu_back();
  998. #else //SNMM
  999. lcd_clear();
  1000. lcd_display_message_fullscreen_P(PSTR(""));
  1001. max_feedrate[E_AXIS] = 50;
  1002. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  1003. lcd_print(" ");
  1004. lcd_print(mmu_extruder + 1);
  1005. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  1006. if (current_position[Z_AXIS] < 15) {
  1007. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  1008. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  1009. }
  1010. current_position[E_AXIS] += 10; //extrusion
  1011. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  1012. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  1013. if (current_temperature[0] < 230) { //PLA & all other filaments
  1014. current_position[E_AXIS] += 5.4;
  1015. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  1016. current_position[E_AXIS] += 3.2;
  1017. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  1018. current_position[E_AXIS] += 3;
  1019. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  1020. }
  1021. else { //ABS
  1022. current_position[E_AXIS] += 3.1;
  1023. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  1024. current_position[E_AXIS] += 3.1;
  1025. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  1026. current_position[E_AXIS] += 4;
  1027. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  1028. /*current_position[X_AXIS] += 23; //delay
  1029. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay
  1030. current_position[X_AXIS] -= 23; //delay
  1031. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  1032. delay_keep_alive(4700);
  1033. }
  1034. max_feedrate[E_AXIS] = 80;
  1035. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1036. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  1037. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  1038. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  1039. st_synchronize();
  1040. //st_current_init();
  1041. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  1042. else st_current_set(2, tmp_motor_loud[2]);
  1043. lcd_update_enable(true);
  1044. lcd_return_to_status();
  1045. max_feedrate[E_AXIS] = 50;
  1046. #endif //SNMM
  1047. }
  1048. else
  1049. {
  1050. show_preheat_nozzle_warning();
  1051. }
  1052. }
  1053. //wrapper functions for loading filament
  1054. void extr_adj_0()
  1055. {
  1056. #ifndef SNMM
  1057. enquecommand_P(PSTR("M701 E0"));
  1058. #else
  1059. change_extr(0);
  1060. extr_adj(0);
  1061. #endif
  1062. }
  1063. void extr_adj_1()
  1064. {
  1065. #ifndef SNMM
  1066. enquecommand_P(PSTR("M701 E1"));
  1067. #else
  1068. change_extr(1);
  1069. extr_adj(1);
  1070. #endif
  1071. }
  1072. void extr_adj_2()
  1073. {
  1074. #ifndef SNMM
  1075. enquecommand_P(PSTR("M701 E2"));
  1076. #else
  1077. change_extr(2);
  1078. extr_adj(2);
  1079. #endif
  1080. }
  1081. void extr_adj_3()
  1082. {
  1083. #ifndef SNMM
  1084. enquecommand_P(PSTR("M701 E3"));
  1085. #else
  1086. change_extr(3);
  1087. extr_adj(3);
  1088. #endif
  1089. }
  1090. void extr_adj_4()
  1091. {
  1092. #ifndef SNMM
  1093. enquecommand_P(PSTR("M701 E4"));
  1094. #else
  1095. change_extr(4);
  1096. extr_adj(4);
  1097. #endif
  1098. }
  1099. void load_all()
  1100. {
  1101. #ifndef SNMM
  1102. enquecommand_P(PSTR("M701 E0"));
  1103. enquecommand_P(PSTR("M701 E1"));
  1104. enquecommand_P(PSTR("M701 E2"));
  1105. enquecommand_P(PSTR("M701 E3"));
  1106. enquecommand_P(PSTR("M701 E4"));
  1107. #else
  1108. for (int i = 0; i < 4; i++)
  1109. {
  1110. change_extr(i);
  1111. extr_adj(i);
  1112. }
  1113. #endif
  1114. }
  1115. //wrapper functions for changing extruders
  1116. void extr_change_0()
  1117. {
  1118. change_extr(0);
  1119. lcd_return_to_status();
  1120. }
  1121. void extr_change_1()
  1122. {
  1123. change_extr(1);
  1124. lcd_return_to_status();
  1125. }
  1126. void extr_change_2()
  1127. {
  1128. change_extr(2);
  1129. lcd_return_to_status();
  1130. }
  1131. void extr_change_3()
  1132. {
  1133. change_extr(3);
  1134. lcd_return_to_status();
  1135. }
  1136. #ifdef SNMM
  1137. //wrapper functions for unloading filament
  1138. void extr_unload_all()
  1139. {
  1140. if (degHotend0() > EXTRUDE_MINTEMP)
  1141. {
  1142. for (int i = 0; i < 4; i++)
  1143. {
  1144. change_extr(i);
  1145. extr_unload();
  1146. }
  1147. }
  1148. else
  1149. {
  1150. show_preheat_nozzle_warning();
  1151. lcd_return_to_status();
  1152. }
  1153. }
  1154. //unloading just used filament (for snmm)
  1155. void extr_unload_used()
  1156. {
  1157. if (degHotend0() > EXTRUDE_MINTEMP) {
  1158. for (int i = 0; i < 4; i++) {
  1159. if (snmm_filaments_used & (1 << i)) {
  1160. change_extr(i);
  1161. extr_unload();
  1162. }
  1163. }
  1164. snmm_filaments_used = 0;
  1165. }
  1166. else {
  1167. show_preheat_nozzle_warning();
  1168. lcd_return_to_status();
  1169. }
  1170. }
  1171. #endif //SNMM
  1172. void extr_unload_0()
  1173. {
  1174. change_extr(0);
  1175. extr_unload();
  1176. }
  1177. void extr_unload_1()
  1178. {
  1179. change_extr(1);
  1180. extr_unload();
  1181. }
  1182. void extr_unload_2()
  1183. {
  1184. change_extr(2);
  1185. extr_unload();
  1186. }
  1187. void extr_unload_3()
  1188. {
  1189. change_extr(3);
  1190. extr_unload();
  1191. }
  1192. void extr_unload_4()
  1193. {
  1194. change_extr(4);
  1195. extr_unload();
  1196. }
  1197. bool mmu_check_version()
  1198. {
  1199. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  1200. }
  1201. void mmu_show_warning()
  1202. {
  1203. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  1204. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  1205. }
  1206. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  1207. {
  1208. //-//
  1209. bFilamentAction=false; // NOT in "mmu_load_to_nozzle_menu()"
  1210. if (degHotend0() > EXTRUDE_MINTEMP)
  1211. {
  1212. tmp_extruder = filament_nr;
  1213. lcd_update_enable(false);
  1214. lcd_clear();
  1215. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  1216. lcd_print(" ");
  1217. lcd_print(tmp_extruder + 1);
  1218. mmu_command(MmuCmd::T0 + tmp_extruder);
  1219. manage_response(true, true, MMU_TCODE_MOVE);
  1220. mmu_continue_loading();
  1221. mmu_extruder = tmp_extruder; //filament change is finished
  1222. mmu_load_to_nozzle();
  1223. load_filament_final_feed();
  1224. st_synchronize();
  1225. custom_message_type = CUSTOM_MSG_TYPE_F_LOAD;
  1226. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  1227. lcd_return_to_status();
  1228. lcd_update_enable(true);
  1229. lcd_load_filament_color_check();
  1230. lcd_setstatuspgm(_T(WELCOME_MSG));
  1231. custom_message_type = CUSTOM_MSG_TYPE_STATUS;
  1232. }
  1233. else
  1234. {
  1235. show_preheat_nozzle_warning();
  1236. }
  1237. }
  1238. void mmu_cut_filament(uint8_t filament_nr)
  1239. {
  1240. bFilamentAction=false; // NOT in "mmu_load_to_nozzle_menu()"
  1241. if (degHotend0() > EXTRUDE_MINTEMP)
  1242. {
  1243. LcdUpdateDisabler disableLcdUpdate;
  1244. lcd_clear();
  1245. lcd_set_cursor(0, 1); lcd_puts_P(_i("Cutting filament")); //// c=18 r=1
  1246. lcd_print(" ");
  1247. lcd_print(filament_nr + 1);
  1248. mmu_filament_ramming();
  1249. mmu_command(MmuCmd::K0 + filament_nr);
  1250. manage_response(false, false, MMU_UNLOAD_MOVE);
  1251. }
  1252. else
  1253. {
  1254. show_preheat_nozzle_warning();
  1255. }
  1256. }
  1257. void mmu_eject_filament(uint8_t filament, bool recover)
  1258. {
  1259. //-//
  1260. bFilamentAction=false; // NOT in "mmu_fil_eject_menu()"
  1261. if (filament < 5)
  1262. {
  1263. if (degHotend0() > EXTRUDE_MINTEMP)
  1264. {
  1265. st_synchronize();
  1266. {
  1267. LcdUpdateDisabler disableLcdUpdate;
  1268. lcd_clear();
  1269. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  1270. mmu_filament_ramming();
  1271. mmu_command(MmuCmd::E0 + filament);
  1272. manage_response(false, false, MMU_UNLOAD_MOVE);
  1273. if (recover)
  1274. {
  1275. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  1276. mmu_command(MmuCmd::R0);
  1277. manage_response(false, false);
  1278. }
  1279. }
  1280. }
  1281. else
  1282. {
  1283. show_preheat_nozzle_warning();
  1284. }
  1285. }
  1286. else
  1287. {
  1288. puts_P(PSTR("Filament nr out of range!"));
  1289. }
  1290. }
  1291. //! @brief load more
  1292. //!
  1293. //! Try to feed more filament from MMU if it is not detected by filament sensor.
  1294. //! Move filament back and forth to nozzle in order to detect jam.
  1295. //! If PTFE tube is jammed, this cause filament to be unloaded and no longer
  1296. //! detected by pulley IR sensor in next step.
  1297. static void load_more()
  1298. {
  1299. for (uint8_t i = 0; i < MMU_IDLER_SENSOR_ATTEMPTS_NR; i++)
  1300. {
  1301. if (PIN_GET(IR_SENSOR_PIN) == 0) break;
  1302. DEBUG_PRINTF_P(PSTR("Additional load attempt nr. %d\n"), i);
  1303. mmu_command(MmuCmd::C0);
  1304. manage_response(true, true, MMU_LOAD_MOVE);
  1305. }
  1306. current_position[E_AXIS] += 60;
  1307. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  1308. current_position[E_AXIS] -= 58;
  1309. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  1310. st_synchronize();
  1311. }
  1312. void mmu_continue_loading()
  1313. {
  1314. if (ir_sensor_detected)
  1315. {
  1316. load_more();
  1317. if (PIN_GET(IR_SENSOR_PIN) != 0) {
  1318. uint8_t mmu_load_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL);
  1319. uint16_t mmu_load_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT);
  1320. if(mmu_load_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, mmu_load_fail + 1);
  1321. if(mmu_load_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, mmu_load_fail_tot + 1);
  1322. if (1 == eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED))
  1323. {
  1324. mmu_command(MmuCmd::K0 + tmp_extruder);
  1325. manage_response(true, true, MMU_UNLOAD_MOVE);
  1326. }
  1327. mmu_command(MmuCmd::T0 + tmp_extruder);
  1328. manage_response(true, true, MMU_TCODE_MOVE);
  1329. load_more();
  1330. if (PIN_GET(IR_SENSOR_PIN) != 0)
  1331. {
  1332. //pause print, show error message and then repeat last T-code
  1333. stop_and_save_print_to_ram(0, 0);
  1334. //lift z
  1335. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  1336. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  1337. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  1338. st_synchronize();
  1339. //Move XY to side
  1340. current_position[X_AXIS] = X_PAUSE_POS;
  1341. current_position[Y_AXIS] = Y_PAUSE_POS;
  1342. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  1343. st_synchronize();
  1344. mmu_command(MmuCmd::U0);
  1345. manage_response(false, true, MMU_UNLOAD_MOVE);
  1346. setAllTargetHotends(0);
  1347. lcd_setstatuspgm(_i("MMU load failed "));////MSG_RECOVERING_PRINT c=20 r=1
  1348. mmu_fil_loaded = false; //so we can retry same T-code again
  1349. isPrintPaused = true;
  1350. mmu_command(MmuCmd::W0);
  1351. }
  1352. }
  1353. }
  1354. else { //mmu_ir_sensor_detected == false
  1355. mmu_command(MmuCmd::C0);
  1356. }
  1357. }