mmu.cpp 31 KB

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