mmu.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. //mmu.cpp
  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. #define CHECK_FINDA ((IS_SD_PRINTING || is_usb_printing) && (mcode_in_progress != 600) && !saved_printing && e_active())
  16. #define MMU_TODELAY 100
  17. #define MMU_TIMEOUT 10
  18. #define MMU_CMD_TIMEOUT 300000ul //5min timeout for mmu commands (except P0)
  19. #define MMU_P0_TIMEOUT 3000ul //timeout for P0 command: 3seconds
  20. #ifdef MMU_HWRESET
  21. #define MMU_RST_PIN 76
  22. #endif //MMU_HWRESET
  23. bool mmu_enabled = false;
  24. bool mmu_ready = false;
  25. static int8_t mmu_state = 0;
  26. uint8_t mmu_cmd = 0;
  27. uint8_t mmu_extruder = 0;
  28. //! This variable probably has no meaning and is planed to be removed
  29. uint8_t tmp_extruder = 0;
  30. int8_t mmu_finda = -1;
  31. int16_t mmu_version = -1;
  32. int16_t mmu_buildnr = -1;
  33. uint32_t mmu_last_request = 0;
  34. uint32_t mmu_last_response = 0;
  35. //clear rx buffer
  36. void mmu_clr_rx_buf(void)
  37. {
  38. while (fgetc(uart2io) >= 0);
  39. }
  40. //send command - puts
  41. int mmu_puts_P(const char* str)
  42. {
  43. mmu_clr_rx_buf(); //clear rx buffer
  44. int r = fputs_P(str, uart2io); //send command
  45. mmu_last_request = millis();
  46. return r;
  47. }
  48. //send command - printf
  49. int mmu_printf_P(const char* format, ...)
  50. {
  51. va_list args;
  52. va_start(args, format);
  53. mmu_clr_rx_buf(); //clear rx buffer
  54. int r = vfprintf_P(uart2io, format, args); //send command
  55. va_end(args);
  56. mmu_last_request = millis();
  57. return r;
  58. }
  59. //check 'ok' response
  60. int8_t mmu_rx_ok(void)
  61. {
  62. int8_t res = uart2_rx_str_P(PSTR("ok\n"));
  63. if (res == 1) mmu_last_response = millis();
  64. return res;
  65. }
  66. //check 'start' response
  67. int8_t mmu_rx_start(void)
  68. {
  69. int8_t res = uart2_rx_str_P(PSTR("start\n"));
  70. if (res == 1) mmu_last_response = millis();
  71. return res;
  72. }
  73. //initialize mmu2 unit - first part - should be done at begining of startup process
  74. void mmu_init(void)
  75. {
  76. #ifdef MMU_HWRESET
  77. digitalWrite(MMU_RST_PIN, HIGH);
  78. pinMode(MMU_RST_PIN, OUTPUT); //setup reset pin
  79. #endif //MMU_HWRESET
  80. uart2_init(); //init uart2
  81. _delay_ms(10); //wait 10ms for sure
  82. mmu_reset(); //reset mmu (HW or SW), do not wait for response
  83. mmu_state = -1;
  84. }
  85. //mmu main loop - state machine processing
  86. void mmu_loop(void)
  87. {
  88. int filament = 0;
  89. // printf_P(PSTR("MMU loop, state=%d\n"), mmu_state);
  90. switch (mmu_state)
  91. {
  92. case 0:
  93. return;
  94. case -1:
  95. if (mmu_rx_start() > 0)
  96. {
  97. #ifdef MMU_DEBUG
  98. puts_P(PSTR("MMU => 'start'"));
  99. puts_P(PSTR("MMU <= 'S1'"));
  100. #endif //MMU_DEBUG
  101. mmu_puts_P(PSTR("S1\n")); //send 'read version' request
  102. mmu_state = -2;
  103. }
  104. else if (millis() > 30000) //30sec after reset disable mmu
  105. {
  106. puts_P(PSTR("MMU not responding - DISABLED"));
  107. mmu_state = 0;
  108. }
  109. return;
  110. case -2:
  111. if (mmu_rx_ok() > 0)
  112. {
  113. fscanf_P(uart2io, PSTR("%u"), &mmu_version); //scan version from buffer
  114. #ifdef MMU_DEBUG
  115. printf_P(PSTR("MMU => '%dok'\n"), mmu_version);
  116. puts_P(PSTR("MMU <= 'S2'"));
  117. #endif //MMU_DEBUG
  118. mmu_puts_P(PSTR("S2\n")); //send 'read buildnr' request
  119. mmu_state = -3;
  120. }
  121. return;
  122. case -3:
  123. if (mmu_rx_ok() > 0)
  124. {
  125. fscanf_P(uart2io, PSTR("%u"), &mmu_buildnr); //scan buildnr from buffer
  126. #ifdef MMU_DEBUG
  127. printf_P(PSTR("MMU => '%dok'\n"), mmu_buildnr);
  128. #endif //MMU_DEBUG
  129. bool version_valid = mmu_check_version();
  130. if (!version_valid) mmu_show_warning();
  131. else puts_P(PSTR("MMU version valid"));
  132. if ((PRINTER_TYPE == PRINTER_MK3) || (PRINTER_TYPE == PRINTER_MK3_SNMM))
  133. {
  134. #ifdef MMU_DEBUG
  135. puts_P(PSTR("MMU <= 'P0'"));
  136. #endif //MMU_DEBUG
  137. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  138. mmu_state = -4;
  139. }
  140. else
  141. {
  142. #ifdef MMU_DEBUG
  143. puts_P(PSTR("MMU <= 'M1'"));
  144. #endif //MMU_DEBUG
  145. mmu_puts_P(PSTR("M1\n")); //set mmu mode to stealth
  146. mmu_state = -5;
  147. }
  148. }
  149. return;
  150. case -5:
  151. if (mmu_rx_ok() > 0)
  152. {
  153. #ifdef MMU_DEBUG
  154. puts_P(PSTR("MMU <= 'P0'"));
  155. #endif //MMU_DEBUG
  156. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  157. mmu_state = -4;
  158. }
  159. return;
  160. case -4:
  161. if (mmu_rx_ok() > 0)
  162. {
  163. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  164. #ifdef MMU_DEBUG
  165. printf_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  166. #endif //MMU_DEBUG
  167. puts_P(PSTR("MMU - ENABLED"));
  168. mmu_enabled = true;
  169. mmu_state = 1;
  170. }
  171. return;
  172. case 1:
  173. if (mmu_cmd) //command request ?
  174. {
  175. if ((mmu_cmd >= MMU_CMD_T0) && (mmu_cmd <= MMU_CMD_T4))
  176. {
  177. filament = mmu_cmd - MMU_CMD_T0;
  178. #ifdef MMU_DEBUG
  179. printf_P(PSTR("MMU <= 'T%d'\n"), filament);
  180. #endif //MMU_DEBUG
  181. mmu_printf_P(PSTR("T%d\n"), filament);
  182. mmu_state = 3; // wait for response
  183. }
  184. else if ((mmu_cmd >= MMU_CMD_L0) && (mmu_cmd <= MMU_CMD_L4))
  185. {
  186. filament = mmu_cmd - MMU_CMD_L0;
  187. #ifdef MMU_DEBUG
  188. printf_P(PSTR("MMU <= 'L%d'\n"), filament);
  189. #endif //MMU_DEBUG
  190. mmu_printf_P(PSTR("L%d\n"), filament);
  191. mmu_state = 3; // wait for response
  192. }
  193. else if (mmu_cmd == MMU_CMD_C0)
  194. {
  195. #ifdef MMU_DEBUG
  196. printf_P(PSTR("MMU <= 'C0'\n"));
  197. #endif //MMU_DEBUG
  198. mmu_puts_P(PSTR("C0\n")); //send 'continue loading'
  199. mmu_state = 3;
  200. }
  201. else if (mmu_cmd == MMU_CMD_U0)
  202. {
  203. #ifdef MMU_DEBUG
  204. printf_P(PSTR("MMU <= 'U0'\n"));
  205. #endif //MMU_DEBUG
  206. mmu_puts_P(PSTR("U0\n")); //send 'unload current filament'
  207. mmu_state = 3;
  208. }
  209. else if ((mmu_cmd >= MMU_CMD_E0) && (mmu_cmd <= MMU_CMD_E4))
  210. {
  211. int filament = mmu_cmd - MMU_CMD_E0;
  212. #ifdef MMU_DEBUG
  213. printf_P(PSTR("MMU <= 'E%d'\n"), filament);
  214. #endif //MMU_DEBUG
  215. mmu_printf_P(PSTR("E%d\n"), filament); //send eject filament
  216. mmu_state = 3; // wait for response
  217. }
  218. else if (mmu_cmd == MMU_CMD_R0)
  219. {
  220. #ifdef MMU_DEBUG
  221. printf_P(PSTR("MMU <= 'R0'\n"));
  222. #endif //MMU_DEBUG
  223. mmu_puts_P(PSTR("R0\n")); //send recover after eject
  224. mmu_state = 3; // wait for response
  225. }
  226. mmu_cmd = 0;
  227. }
  228. else if ((mmu_last_response + 300) < millis()) //request every 300ms
  229. {
  230. #ifdef MMU_DEBUG
  231. puts_P(PSTR("MMU <= 'P0'"));
  232. #endif //MMU_DEBUG
  233. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  234. mmu_state = 2;
  235. }
  236. return;
  237. case 2: //response to command P0
  238. if (mmu_rx_ok() > 0)
  239. {
  240. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  241. #ifdef MMU_DEBUG
  242. printf_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  243. #endif //MMU_DEBUG
  244. //printf_P(PSTR("Eact: %d\n"), int(e_active()));
  245. if (!mmu_finda && CHECK_FINDA && fsensor_enabled) {
  246. fsensor_stop_and_save_print();
  247. enquecommand_front_P(PSTR("FSENSOR_RECOVER")); //then recover
  248. if (lcd_autoDeplete) enquecommand_front_P(PSTR("M600 AUTO")); //save print and run M600 command
  249. else enquecommand_front_P(PSTR("M600")); //save print and run M600 command
  250. }
  251. mmu_state = 1;
  252. if (mmu_cmd == 0)
  253. mmu_ready = true;
  254. }
  255. else if ((mmu_last_request + MMU_P0_TIMEOUT) < millis())
  256. { //resend request after timeout (30s)
  257. mmu_state = 1;
  258. }
  259. return;
  260. case 3: //response to mmu commands
  261. if (mmu_rx_ok() > 0)
  262. {
  263. #ifdef MMU_DEBUG
  264. printf_P(PSTR("MMU => 'ok'\n"));
  265. #endif //MMU_DEBUG
  266. mmu_ready = true;
  267. mmu_state = 1;
  268. }
  269. else if ((mmu_last_request + MMU_CMD_TIMEOUT) < millis())
  270. { //resend request after timeout (5 min)
  271. mmu_state = 1;
  272. }
  273. return;
  274. }
  275. }
  276. void mmu_reset(void)
  277. {
  278. #ifdef MMU_HWRESET //HW - pulse reset pin
  279. digitalWrite(MMU_RST_PIN, LOW);
  280. _delay_us(100);
  281. digitalWrite(MMU_RST_PIN, HIGH);
  282. #else //SW - send X0 command
  283. mmu_puts_P(PSTR("X0\n"));
  284. #endif
  285. }
  286. int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament)
  287. {
  288. printf_P(PSTR("MMU <= 'F%d %d'\n"), extruder, filament);
  289. mmu_printf_P(PSTR("F%d %d\n"), extruder, filament);
  290. unsigned char timeout = MMU_TIMEOUT; //10x100ms
  291. while ((mmu_rx_ok() <= 0) && (--timeout))
  292. delay_keep_alive(MMU_TODELAY);
  293. return timeout?1:0;
  294. }
  295. void mmu_command(uint8_t cmd)
  296. {
  297. mmu_cmd = cmd;
  298. mmu_ready = false;
  299. }
  300. bool mmu_get_response(void)
  301. {
  302. // printf_P(PSTR("mmu_get_response - begin\n"));
  303. KEEPALIVE_STATE(IN_PROCESS);
  304. while (mmu_cmd != 0)
  305. {
  306. // mmu_loop();
  307. delay_keep_alive(100);
  308. }
  309. while (!mmu_ready)
  310. {
  311. // mmu_loop();
  312. if (mmu_state != 3)
  313. break;
  314. delay_keep_alive(100);
  315. }
  316. bool ret = mmu_ready;
  317. mmu_ready = false;
  318. // printf_P(PSTR("mmu_get_response - end %d\n"), ret?1:0);
  319. return ret;
  320. /* //waits for "ok" from mmu
  321. //function returns true if "ok" was received
  322. //if timeout is set to true function return false if there is no "ok" received before timeout
  323. bool response = true;
  324. LongTimer mmu_get_reponse_timeout;
  325. KEEPALIVE_STATE(IN_PROCESS);
  326. mmu_get_reponse_timeout.start();
  327. while (mmu_rx_ok() <= 0)
  328. {
  329. delay_keep_alive(100);
  330. if (timeout && mmu_get_reponse_timeout.expired(5 * 60 * 1000ul))
  331. { //5 minutes timeout
  332. response = false;
  333. break;
  334. }
  335. }
  336. printf_P(PSTR("mmu_get_response - end %d\n"), response?1:0);
  337. return response;*/
  338. }
  339. void manage_response(bool move_axes, bool turn_off_nozzle)
  340. {
  341. bool response = false;
  342. mmu_print_saved = false;
  343. bool lcd_update_was_enabled = false;
  344. float hotend_temp_bckp = degTargetHotend(active_extruder);
  345. float z_position_bckp = current_position[Z_AXIS];
  346. float x_position_bckp = current_position[X_AXIS];
  347. float y_position_bckp = current_position[Y_AXIS];
  348. uint8_t screen = 0; //used for showing multiscreen messages
  349. while(!response)
  350. {
  351. response = mmu_get_response(); //wait for "ok" from mmu
  352. if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit
  353. if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
  354. if (lcd_update_enabled) {
  355. lcd_update_was_enabled = true;
  356. lcd_update_enable(false);
  357. }
  358. st_synchronize();
  359. mmu_print_saved = true;
  360. printf_P(PSTR("MMU not responding\n"));
  361. hotend_temp_bckp = degTargetHotend(active_extruder);
  362. if (move_axes) {
  363. z_position_bckp = current_position[Z_AXIS];
  364. x_position_bckp = current_position[X_AXIS];
  365. y_position_bckp = current_position[Y_AXIS];
  366. //lift z
  367. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  368. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  369. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  370. st_synchronize();
  371. //Move XY to side
  372. current_position[X_AXIS] = X_PAUSE_POS;
  373. current_position[Y_AXIS] = Y_PAUSE_POS;
  374. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  375. st_synchronize();
  376. }
  377. if (turn_off_nozzle) {
  378. //set nozzle target temperature to 0
  379. setAllTargetHotends(0);
  380. }
  381. }
  382. //first three lines are used for printing multiscreen message; last line contains measured and target nozzle temperature
  383. if (screen == 0) { //screen 0
  384. lcd_display_message_fullscreen_P(_i("MMU needs user attention."));
  385. screen++;
  386. }
  387. else { //screen 1
  388. if((degTargetHotend(active_extruder) == 0) && turn_off_nozzle) lcd_display_message_fullscreen_P(_i("Press the knob to resume nozzle temperature."));
  389. else lcd_display_message_fullscreen_P(_i("Fix the issue and then press button on MMU unit."));
  390. screen=0;
  391. }
  392. lcd_set_degree();
  393. lcd_set_cursor(0, 4); //line 4
  394. //Print the hotend temperature (9 chars total) and fill rest of the line with space
  395. 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]);
  396. lcd_space(9 - chars);
  397. //5 seconds delay
  398. for (uint8_t i = 0; i < 50; i++) {
  399. if (lcd_clicked()) {
  400. setTargetHotend(hotend_temp_bckp, active_extruder);
  401. break;
  402. }
  403. delay_keep_alive(100);
  404. }
  405. }
  406. else if (mmu_print_saved) {
  407. printf_P(PSTR("MMU starts responding\n"));
  408. if (turn_off_nozzle)
  409. {
  410. lcd_clear();
  411. setTargetHotend(hotend_temp_bckp, active_extruder);
  412. if (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
  413. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature..."));
  414. delay_keep_alive(3000);
  415. }
  416. while ((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)
  417. {
  418. delay_keep_alive(1000);
  419. lcd_wait_for_heater();
  420. }
  421. }
  422. if (move_axes) {
  423. lcd_clear();
  424. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming position..."));
  425. current_position[X_AXIS] = x_position_bckp;
  426. current_position[Y_AXIS] = y_position_bckp;
  427. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  428. st_synchronize();
  429. current_position[Z_AXIS] = z_position_bckp;
  430. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  431. st_synchronize();
  432. }
  433. else {
  434. lcd_clear();
  435. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming..."));
  436. delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
  437. }
  438. }
  439. }
  440. if (lcd_update_was_enabled) lcd_update_enable(true);
  441. }
  442. //! @brief load filament to nozzle of multimaterial printer
  443. //!
  444. //! This function is used only only after T? (user select filament) and M600 (change filament).
  445. //! It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
  446. //! filament to nozzle.
  447. //!
  448. void mmu_load_to_nozzle()
  449. {
  450. st_synchronize();
  451. bool saved_e_relative_mode = axis_relative_modes[E_AXIS];
  452. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = true;
  453. current_position[E_AXIS] += 7.2f;
  454. float feedrate = 562;
  455. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  456. st_synchronize();
  457. current_position[E_AXIS] += 14.4f;
  458. feedrate = 871;
  459. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  460. st_synchronize();
  461. current_position[E_AXIS] += 36.0f;
  462. feedrate = 1393;
  463. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  464. st_synchronize();
  465. current_position[E_AXIS] += 14.4f;
  466. feedrate = 871;
  467. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  468. st_synchronize();
  469. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = false;
  470. }
  471. void mmu_M600_wait_and_beep() {
  472. //Beep and wait for user to remove old filament and prepare new filament for load
  473. KEEPALIVE_STATE(PAUSED_FOR_USER);
  474. int counterBeep = 0;
  475. lcd_display_message_fullscreen_P(_i("Remove old filament and press the knob to start loading new filament."));
  476. bool bFirst=true;
  477. while (!lcd_clicked()){
  478. manage_heater();
  479. manage_inactivity(true);
  480. #if BEEPER > 0
  481. if (counterBeep == 500) {
  482. counterBeep = 0;
  483. }
  484. SET_OUTPUT(BEEPER);
  485. if (counterBeep == 0) {
  486. if((eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
  487. {
  488. bFirst=false;
  489. WRITE(BEEPER, HIGH);
  490. }
  491. }
  492. if (counterBeep == 20) {
  493. WRITE(BEEPER, LOW);
  494. }
  495. counterBeep++;
  496. #endif //BEEPER > 0
  497. delay_keep_alive(4);
  498. }
  499. WRITE(BEEPER, LOW);
  500. }
  501. void mmu_M600_load_filament(bool automatic)
  502. {
  503. //load filament for mmu v2
  504. tmp_extruder = mmu_extruder;
  505. if (!automatic) {
  506. #ifdef MMU_M600_SWITCH_EXTRUDER
  507. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  508. if(yes) tmp_extruder = choose_extruder_menu();
  509. #endif //MMU_M600_SWITCH_EXTRUDER
  510. }
  511. else {
  512. tmp_extruder = (tmp_extruder+1)%5;
  513. }
  514. lcd_update_enable(false);
  515. lcd_clear();
  516. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  517. lcd_print(" ");
  518. lcd_print(tmp_extruder + 1);
  519. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  520. // printf_P(PSTR("T code: %d \n"), tmp_extruder);
  521. // mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  522. mmu_command(MMU_CMD_T0 + tmp_extruder);
  523. manage_response(false, true);
  524. mmu_command(MMU_CMD_C0);
  525. mmu_extruder = tmp_extruder; //filament change is finished
  526. mmu_load_to_nozzle();
  527. st_synchronize();
  528. current_position[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  529. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2, active_extruder);
  530. }
  531. void extr_mov(float shift, float feed_rate)
  532. { //move extruder no matter what the current heater temperature is
  533. set_extrude_min_temp(.0);
  534. current_position[E_AXIS] += shift;
  535. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feed_rate, active_extruder);
  536. set_extrude_min_temp(EXTRUDE_MINTEMP);
  537. }
  538. void change_extr(int
  539. #ifdef SNMM
  540. extr
  541. #endif //SNMM
  542. ) { //switches multiplexer for extruders
  543. #ifdef SNMM
  544. st_synchronize();
  545. delay(100);
  546. disable_e0();
  547. disable_e1();
  548. disable_e2();
  549. mmu_extruder = extr;
  550. pinMode(E_MUX0_PIN, OUTPUT);
  551. pinMode(E_MUX1_PIN, OUTPUT);
  552. switch (extr) {
  553. case 1:
  554. WRITE(E_MUX0_PIN, HIGH);
  555. WRITE(E_MUX1_PIN, LOW);
  556. break;
  557. case 2:
  558. WRITE(E_MUX0_PIN, LOW);
  559. WRITE(E_MUX1_PIN, HIGH);
  560. break;
  561. case 3:
  562. WRITE(E_MUX0_PIN, HIGH);
  563. WRITE(E_MUX1_PIN, HIGH);
  564. break;
  565. default:
  566. WRITE(E_MUX0_PIN, LOW);
  567. WRITE(E_MUX1_PIN, LOW);
  568. break;
  569. }
  570. delay(100);
  571. #endif
  572. }
  573. int get_ext_nr()
  574. { //reads multiplexer input pins and return current extruder number (counted from 0)
  575. #ifndef SNMM
  576. return(mmu_extruder); //update needed
  577. #else
  578. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  579. #endif
  580. }
  581. void display_loading()
  582. {
  583. switch (mmu_extruder)
  584. {
  585. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  586. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  587. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  588. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  589. }
  590. }
  591. void extr_adj(int extruder) //loading filament for SNMM
  592. {
  593. #ifndef SNMM
  594. uint8_t cmd = MMU_CMD_L0 + extruder;
  595. if (cmd > MMU_CMD_L4)
  596. {
  597. printf_P(PSTR("Filament out of range %d \n"),extruder);
  598. return;
  599. }
  600. mmu_command(cmd);
  601. //show which filament is currently loaded
  602. lcd_update_enable(false);
  603. lcd_clear();
  604. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  605. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  606. //else lcd.print(" ");
  607. lcd_print(" ");
  608. lcd_print(extruder + 1);
  609. // get response
  610. manage_response(false, false);
  611. lcd_update_enable(true);
  612. //lcd_return_to_status();
  613. #else
  614. bool correct;
  615. max_feedrate[E_AXIS] =80;
  616. //max_feedrate[E_AXIS] = 50;
  617. START:
  618. lcd_clear();
  619. lcd_set_cursor(0, 0);
  620. switch (extruder) {
  621. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  622. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  623. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  624. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  625. }
  626. KEEPALIVE_STATE(PAUSED_FOR_USER);
  627. do{
  628. extr_mov(0.001,1000);
  629. delay_keep_alive(2);
  630. } while (!lcd_clicked());
  631. //delay_keep_alive(500);
  632. KEEPALIVE_STATE(IN_HANDLER);
  633. st_synchronize();
  634. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  635. //if (!correct) goto START;
  636. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  637. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  638. extr_mov(bowden_length[extruder], 500);
  639. lcd_clear();
  640. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  641. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  642. else lcd_print(" ");
  643. lcd_print(mmu_extruder + 1);
  644. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  645. st_synchronize();
  646. max_feedrate[E_AXIS] = 50;
  647. lcd_update_enable(true);
  648. lcd_return_to_status();
  649. lcdDrawUpdate = 2;
  650. #endif
  651. }
  652. struct E_step
  653. {
  654. float extrude; //!< extrude distance in mm
  655. float feed_rate; //!< feed rate in mm/s
  656. };
  657. static const E_step ramming_sequence[] PROGMEM =
  658. {
  659. {1.0, 1000.0/60},
  660. {1.0, 1500.0/60},
  661. {2.0, 2000.0/60},
  662. {1.5, 3000.0/60},
  663. {2.5, 4000.0/60},
  664. {-15.0, 5000.0/60},
  665. {-14.0, 1200.0/60},
  666. {-6.0, 600.0/60},
  667. {10.0, 700.0/60},
  668. {-10.0, 400.0/60},
  669. {-50.0, 2000.0/60},
  670. };
  671. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  672. static void filament_ramming()
  673. {
  674. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  675. {
  676. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  677. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  678. current_position[E_AXIS], pgm_read_float(&(ramming_sequence[i].feed_rate)), active_extruder);
  679. st_synchronize();
  680. }
  681. }
  682. void extr_unload()
  683. { //unload just current filament for multimaterial printers
  684. #ifdef SNMM
  685. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  686. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  687. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  688. #endif
  689. if (degHotend0() > EXTRUDE_MINTEMP)
  690. {
  691. #ifndef SNMM
  692. st_synchronize();
  693. //show which filament is currently unloaded
  694. lcd_update_enable(false);
  695. lcd_clear();
  696. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  697. lcd_print(" ");
  698. lcd_print(mmu_extruder + 1);
  699. filament_ramming();
  700. mmu_command(MMU_CMD_U0);
  701. // get response
  702. manage_response(false, true);
  703. lcd_update_enable(true);
  704. #else //SNMM
  705. lcd_clear();
  706. lcd_display_message_fullscreen_P(PSTR(""));
  707. max_feedrate[E_AXIS] = 50;
  708. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  709. lcd_print(" ");
  710. lcd_print(mmu_extruder + 1);
  711. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  712. if (current_position[Z_AXIS] < 15) {
  713. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  714. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  715. }
  716. current_position[E_AXIS] += 10; //extrusion
  717. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  718. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  719. if (current_temperature[0] < 230) { //PLA & all other filaments
  720. current_position[E_AXIS] += 5.4;
  721. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  722. current_position[E_AXIS] += 3.2;
  723. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  724. current_position[E_AXIS] += 3;
  725. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  726. }
  727. else { //ABS
  728. current_position[E_AXIS] += 3.1;
  729. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  730. current_position[E_AXIS] += 3.1;
  731. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  732. current_position[E_AXIS] += 4;
  733. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  734. /*current_position[X_AXIS] += 23; //delay
  735. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay
  736. current_position[X_AXIS] -= 23; //delay
  737. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  738. delay_keep_alive(4700);
  739. }
  740. max_feedrate[E_AXIS] = 80;
  741. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  742. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  743. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  744. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  745. st_synchronize();
  746. //st_current_init();
  747. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  748. else st_current_set(2, tmp_motor_loud[2]);
  749. lcd_update_enable(true);
  750. lcd_return_to_status();
  751. max_feedrate[E_AXIS] = 50;
  752. #endif //SNMM
  753. }
  754. else
  755. {
  756. lcd_clear();
  757. lcd_set_cursor(0, 0);
  758. lcd_puts_P(_T(MSG_ERROR));
  759. lcd_set_cursor(0, 2);
  760. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  761. delay(2000);
  762. lcd_clear();
  763. }
  764. //lcd_return_to_status();
  765. }
  766. //wrapper functions for loading filament
  767. void extr_adj_0()
  768. {
  769. #ifndef SNMM
  770. enquecommand_P(PSTR("M701 E0"));
  771. #else
  772. change_extr(0);
  773. extr_adj(0);
  774. #endif
  775. }
  776. void extr_adj_1()
  777. {
  778. #ifndef SNMM
  779. enquecommand_P(PSTR("M701 E1"));
  780. #else
  781. change_extr(1);
  782. extr_adj(1);
  783. #endif
  784. }
  785. void extr_adj_2()
  786. {
  787. #ifndef SNMM
  788. enquecommand_P(PSTR("M701 E2"));
  789. #else
  790. change_extr(2);
  791. extr_adj(2);
  792. #endif
  793. }
  794. void extr_adj_3()
  795. {
  796. #ifndef SNMM
  797. enquecommand_P(PSTR("M701 E3"));
  798. #else
  799. change_extr(3);
  800. extr_adj(3);
  801. #endif
  802. }
  803. void extr_adj_4()
  804. {
  805. #ifndef SNMM
  806. enquecommand_P(PSTR("M701 E4"));
  807. #else
  808. change_extr(4);
  809. extr_adj(4);
  810. #endif
  811. }
  812. void mmu_eject_fil_0()
  813. {
  814. mmu_eject_filament(0, true);
  815. }
  816. void mmu_eject_fil_1()
  817. {
  818. mmu_eject_filament(1, true);
  819. }
  820. void mmu_eject_fil_2()
  821. {
  822. mmu_eject_filament(2, true);
  823. }
  824. void mmu_eject_fil_3()
  825. {
  826. mmu_eject_filament(3, true);
  827. }
  828. void mmu_eject_fil_4()
  829. {
  830. mmu_eject_filament(4, true);
  831. }
  832. void load_all()
  833. {
  834. #ifndef SNMM
  835. enquecommand_P(PSTR("M701 E0"));
  836. enquecommand_P(PSTR("M701 E1"));
  837. enquecommand_P(PSTR("M701 E2"));
  838. enquecommand_P(PSTR("M701 E3"));
  839. enquecommand_P(PSTR("M701 E4"));
  840. #else
  841. for (int i = 0; i < 4; i++)
  842. {
  843. change_extr(i);
  844. extr_adj(i);
  845. }
  846. #endif
  847. }
  848. //wrapper functions for changing extruders
  849. void extr_change_0()
  850. {
  851. change_extr(0);
  852. lcd_return_to_status();
  853. }
  854. void extr_change_1()
  855. {
  856. change_extr(1);
  857. lcd_return_to_status();
  858. }
  859. void extr_change_2()
  860. {
  861. change_extr(2);
  862. lcd_return_to_status();
  863. }
  864. void extr_change_3()
  865. {
  866. change_extr(3);
  867. lcd_return_to_status();
  868. }
  869. //wrapper functions for unloading filament
  870. void extr_unload_all()
  871. {
  872. if (degHotend0() > EXTRUDE_MINTEMP)
  873. {
  874. for (int i = 0; i < 4; i++)
  875. {
  876. change_extr(i);
  877. extr_unload();
  878. }
  879. }
  880. else
  881. {
  882. lcd_clear();
  883. lcd_set_cursor(0, 0);
  884. lcd_puts_P(_T(MSG_ERROR));
  885. lcd_set_cursor(0, 2);
  886. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  887. delay(2000);
  888. lcd_clear();
  889. lcd_return_to_status();
  890. }
  891. }
  892. //unloading just used filament (for snmm)
  893. void extr_unload_used()
  894. {
  895. if (degHotend0() > EXTRUDE_MINTEMP) {
  896. for (int i = 0; i < 4; i++) {
  897. if (snmm_filaments_used & (1 << i)) {
  898. change_extr(i);
  899. extr_unload();
  900. }
  901. }
  902. snmm_filaments_used = 0;
  903. }
  904. else {
  905. lcd_clear();
  906. lcd_set_cursor(0, 0);
  907. lcd_puts_P(_T(MSG_ERROR));
  908. lcd_set_cursor(0, 2);
  909. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  910. delay(2000);
  911. lcd_clear();
  912. lcd_return_to_status();
  913. }
  914. }
  915. void extr_unload_0()
  916. {
  917. change_extr(0);
  918. extr_unload();
  919. }
  920. void extr_unload_1()
  921. {
  922. change_extr(1);
  923. extr_unload();
  924. }
  925. void extr_unload_2()
  926. {
  927. change_extr(2);
  928. extr_unload();
  929. }
  930. void extr_unload_3()
  931. {
  932. change_extr(3);
  933. extr_unload();
  934. }
  935. void extr_unload_4()
  936. {
  937. change_extr(4);
  938. extr_unload();
  939. }
  940. bool mmu_check_version()
  941. {
  942. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  943. }
  944. void mmu_show_warning()
  945. {
  946. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  947. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  948. }
  949. void mmu_eject_filament(uint8_t filament, bool recover)
  950. {
  951. if (filament < 5)
  952. {
  953. if (degHotend0() > EXTRUDE_MINTEMP)
  954. {
  955. st_synchronize();
  956. {
  957. LcdUpdateDisabler disableLcdUpdate;
  958. lcd_clear();
  959. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  960. current_position[E_AXIS] -= 80;
  961. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  962. st_synchronize();
  963. mmu_command(MMU_CMD_E0 + filament);
  964. manage_response(false, false);
  965. if (recover)
  966. {
  967. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  968. mmu_command(MMU_CMD_R0);
  969. manage_response(false, false);
  970. }
  971. }
  972. }
  973. else
  974. {
  975. lcd_clear();
  976. lcd_set_cursor(0, 0);
  977. lcd_puts_P(_T(MSG_ERROR));
  978. lcd_set_cursor(0, 2);
  979. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  980. delay(2000);
  981. lcd_clear();
  982. }
  983. }
  984. else
  985. {
  986. puts_P(PSTR("Filament nr out of range!"));
  987. }
  988. }