mmu.cpp 28 KB

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