mmu.cpp 43 KB

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