mmu.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. extern const char* lcd_display_message_fullscreen_P(const char *msg);
  10. extern void lcd_show_fullscreen_message_and_wait_P(const char *msg);
  11. extern int8_t lcd_show_fullscreen_message_yes_no_and_wait_P(const char *msg, bool allow_timeouting = true, bool default_yes = false);
  12. extern void lcd_return_to_status();
  13. extern void lcd_wait_for_heater();
  14. extern char choose_extruder_menu();
  15. #define MMU_TODELAY 100
  16. #define MMU_TIMEOUT 10
  17. #define MMU_HWRESET
  18. #define MMU_RST_PIN 76
  19. bool mmu_enabled = false;
  20. bool mmu_ready = false;
  21. static int8_t mmu_state = 0;
  22. uint8_t mmu_cmd = 0;
  23. uint8_t mmu_extruder = 0;
  24. //! This variable probably has no meaning and is planed to be removed
  25. uint8_t tmp_extruder = 0;
  26. int8_t mmu_finda = -1;
  27. int16_t mmu_version = -1;
  28. int16_t mmu_buildnr = -1;
  29. uint32_t mmu_last_request = 0;
  30. uint32_t mmu_last_response = 0;
  31. //clear rx buffer
  32. void mmu_clr_rx_buf(void)
  33. {
  34. while (fgetc(uart2io) >= 0);
  35. }
  36. //send command - puts
  37. int mmu_puts_P(const char* str)
  38. {
  39. mmu_clr_rx_buf(); //clear rx buffer
  40. int r = fputs_P(str, uart2io); //send command
  41. mmu_last_request = millis();
  42. return r;
  43. }
  44. //send command - printf
  45. int mmu_printf_P(const char* format, ...)
  46. {
  47. va_list args;
  48. va_start(args, format);
  49. mmu_clr_rx_buf(); //clear rx buffer
  50. int r = vfprintf_P(uart2io, format, args); //send command
  51. va_end(args);
  52. mmu_last_request = millis();
  53. return r;
  54. }
  55. //check 'ok' response
  56. int8_t mmu_rx_ok(void)
  57. {
  58. int8_t res = uart2_rx_str_P(PSTR("ok\n"));
  59. if (res == 1) mmu_last_response = millis();
  60. return res;
  61. }
  62. //check 'start' response
  63. int8_t mmu_rx_start(void)
  64. {
  65. int8_t res = uart2_rx_str_P(PSTR("start\n"));
  66. if (res == 1) mmu_last_response = millis();
  67. return res;
  68. }
  69. //initialize mmu2 unit - first part - should be done at begining of startup process
  70. void mmu_init(void)
  71. {
  72. digitalWrite(MMU_RST_PIN, HIGH);
  73. pinMode(MMU_RST_PIN, OUTPUT); //setup reset pin
  74. uart2_init(); //init uart2
  75. _delay_ms(10); //wait 10ms for sure
  76. mmu_reset(); //reset mmu (HW or SW), do not wait for response
  77. mmu_state = -1;
  78. }
  79. //mmu main loop - state machine processing
  80. void mmu_loop(void)
  81. {
  82. // printf_P(PSTR("MMU loop, state=%d\n"), mmu_state);
  83. switch (mmu_state)
  84. {
  85. case 0:
  86. return;
  87. case -1:
  88. if (mmu_rx_start() > 0)
  89. {
  90. puts_P(PSTR("MMU => 'start'"));
  91. puts_P(PSTR("MMU <= 'S1'"));
  92. mmu_puts_P(PSTR("S1\n")); //send 'read version' request
  93. mmu_state = -2;
  94. }
  95. else if (millis() > 30000) //30sec after reset disable mmu
  96. {
  97. puts_P(PSTR("MMU not responding - DISABLED"));
  98. mmu_state = 0;
  99. }
  100. return;
  101. case -2:
  102. if (mmu_rx_ok() > 0)
  103. {
  104. fscanf_P(uart2io, PSTR("%u"), &mmu_version); //scan version from buffer
  105. printf_P(PSTR("MMU => '%dok'\n"), mmu_version);
  106. puts_P(PSTR("MMU <= 'S2'"));
  107. mmu_puts_P(PSTR("S2\n")); //send 'read buildnr' request
  108. mmu_state = -3;
  109. }
  110. return;
  111. case -3:
  112. if (mmu_rx_ok() > 0)
  113. {
  114. fscanf_P(uart2io, PSTR("%u"), &mmu_buildnr); //scan buildnr from buffer
  115. printf_P(PSTR("MMU => '%dok'\n"), mmu_buildnr);
  116. puts_P(PSTR("MMU <= 'P0'"));
  117. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  118. mmu_state = -4;
  119. }
  120. return;
  121. case -4:
  122. if (mmu_rx_ok() > 0)
  123. {
  124. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  125. printf_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  126. puts_P(PSTR("MMU - ENABLED"));
  127. mmu_enabled = true;
  128. mmu_state = 1;
  129. }
  130. return;
  131. case 1:
  132. if (mmu_cmd) //command request ?
  133. {
  134. if ((mmu_cmd >= MMU_CMD_T0) && (mmu_cmd <= MMU_CMD_T4))
  135. {
  136. int extruder = mmu_cmd - MMU_CMD_T0;
  137. printf_P(PSTR("MMU <= 'T%d'\n"), extruder);
  138. mmu_printf_P(PSTR("T%d\n"), extruder);
  139. mmu_state = 3; // wait for response
  140. }
  141. else if ((mmu_cmd >= MMU_CMD_L0) && (mmu_cmd <= MMU_CMD_L4))
  142. {
  143. int filament = mmu_cmd - MMU_CMD_L0;
  144. printf_P(PSTR("MMU <= 'L%d'\n"), filament);
  145. mmu_printf_P(PSTR("L%d\n"), filament);
  146. mmu_state = 3; // wait for response
  147. }
  148. mmu_cmd = 0;
  149. }
  150. else if ((mmu_last_response + 1000) < millis()) //request every 1s
  151. {
  152. puts_P(PSTR("MMU <= 'P0'"));
  153. mmu_puts_P(PSTR("P0\n")); //send 'read finda' request
  154. mmu_state = 2;
  155. }
  156. return;
  157. case 2: //response to command P0
  158. if (mmu_rx_ok() > 0)
  159. {
  160. fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer
  161. printf_P(PSTR("MMU => '%dok'\n"), mmu_finda);
  162. mmu_state = 1;
  163. if (mmu_cmd == 0)
  164. mmu_ready = true;
  165. }
  166. else if ((mmu_last_request + 30000) < millis())
  167. { //resend request after timeout (30s)
  168. mmu_state = 1;
  169. }
  170. return;
  171. case 3: //response to commands T0-T4
  172. if (mmu_rx_ok() > 0)
  173. {
  174. printf_P(PSTR("MMU => 'ok'\n"), mmu_finda);
  175. mmu_ready = true;
  176. mmu_state = 1;
  177. }
  178. else if ((mmu_last_request + 30000) < millis())
  179. { //resend request after timeout (30s)
  180. mmu_state = 1;
  181. }
  182. return;
  183. }
  184. }
  185. void mmu_reset(void)
  186. {
  187. #ifdef MMU_HWRESET //HW - pulse reset pin
  188. digitalWrite(MMU_RST_PIN, LOW);
  189. _delay_us(100);
  190. digitalWrite(MMU_RST_PIN, HIGH);
  191. #else //SW - send X0 command
  192. mmu_puts_P(PSTR("X0\n"));
  193. #endif
  194. }
  195. int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament)
  196. {
  197. printf_P(PSTR("MMU <= 'F%d %d'\n"), extruder, filament);
  198. mmu_printf_P(PSTR("F%d %d\n"), extruder, filament);
  199. unsigned char timeout = MMU_TIMEOUT; //10x100ms
  200. while ((mmu_rx_ok() <= 0) && (--timeout))
  201. delay_keep_alive(MMU_TODELAY);
  202. return timeout?1:0;
  203. }
  204. void mmu_command(uint8_t cmd)
  205. {
  206. mmu_cmd = cmd;
  207. mmu_ready = false;
  208. }
  209. bool mmu_get_response(void)
  210. {
  211. // printf_P(PSTR("mmu_get_response - begin\n"));
  212. KEEPALIVE_STATE(IN_PROCESS);
  213. while (mmu_cmd != 0)
  214. {
  215. // mmu_loop();
  216. delay_keep_alive(100);
  217. }
  218. while (!mmu_ready)
  219. {
  220. // mmu_loop();
  221. if (mmu_state != 3)
  222. break;
  223. delay_keep_alive(100);
  224. }
  225. bool ret = mmu_ready;
  226. mmu_ready = false;
  227. // printf_P(PSTR("mmu_get_response - end %d\n"), ret?1:0);
  228. return ret;
  229. /* //waits for "ok" from mmu
  230. //function returns true if "ok" was received
  231. //if timeout is set to true function return false if there is no "ok" received before timeout
  232. bool response = true;
  233. LongTimer mmu_get_reponse_timeout;
  234. KEEPALIVE_STATE(IN_PROCESS);
  235. mmu_get_reponse_timeout.start();
  236. while (mmu_rx_ok() <= 0)
  237. {
  238. delay_keep_alive(100);
  239. if (timeout && mmu_get_reponse_timeout.expired(5 * 60 * 1000ul))
  240. { //5 minutes timeout
  241. response = false;
  242. break;
  243. }
  244. }
  245. printf_P(PSTR("mmu_get_response - end %d\n"), response?1:0);
  246. return response;*/
  247. }
  248. void manage_response(bool move_axes, bool turn_off_nozzle)
  249. {
  250. bool response = false;
  251. mmu_print_saved = false;
  252. bool lcd_update_was_enabled = false;
  253. float hotend_temp_bckp = degTargetHotend(active_extruder);
  254. float z_position_bckp = current_position[Z_AXIS];
  255. float x_position_bckp = current_position[X_AXIS];
  256. float y_position_bckp = current_position[Y_AXIS];
  257. while(!response)
  258. {
  259. response = mmu_get_response(); //wait for "ok" from mmu
  260. if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit
  261. if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
  262. if (lcd_update_enabled) {
  263. lcd_update_was_enabled = true;
  264. lcd_update_enable(false);
  265. }
  266. st_synchronize();
  267. mmu_print_saved = true;
  268. hotend_temp_bckp = degTargetHotend(active_extruder);
  269. if (move_axes) {
  270. z_position_bckp = current_position[Z_AXIS];
  271. x_position_bckp = current_position[X_AXIS];
  272. y_position_bckp = current_position[Y_AXIS];
  273. //lift z
  274. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  275. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  276. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  277. st_synchronize();
  278. //Move XY to side
  279. current_position[X_AXIS] = X_PAUSE_POS;
  280. current_position[Y_AXIS] = Y_PAUSE_POS;
  281. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  282. st_synchronize();
  283. }
  284. if (turn_off_nozzle) {
  285. //set nozzle target temperature to 0
  286. setAllTargetHotends(0);
  287. printf_P(PSTR("MMU not responding\n"));
  288. lcd_show_fullscreen_message_and_wait_P(_i("MMU needs user attention. Please press knob to resume nozzle target temperature."));
  289. setTargetHotend(hotend_temp_bckp, active_extruder);
  290. while ((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5) {
  291. delay_keep_alive(1000);
  292. lcd_wait_for_heater();
  293. }
  294. }
  295. }
  296. lcd_display_message_fullscreen_P(_i("Check MMU. Fix the issue and then press button on MMU unit."));
  297. delay_keep_alive(1000);
  298. }
  299. else if (mmu_print_saved) {
  300. printf_P(PSTR("MMU start responding\n"));
  301. lcd_clear();
  302. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming..."));
  303. if (move_axes) {
  304. current_position[X_AXIS] = x_position_bckp;
  305. current_position[Y_AXIS] = y_position_bckp;
  306. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  307. st_synchronize();
  308. current_position[Z_AXIS] = z_position_bckp;
  309. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  310. st_synchronize();
  311. }
  312. else {
  313. delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
  314. }
  315. }
  316. }
  317. if (lcd_update_was_enabled) lcd_update_enable(true);
  318. }
  319. void mmu_load_to_nozzle()
  320. {
  321. st_synchronize();
  322. bool saved_e_relative_mode = axis_relative_modes[E_AXIS];
  323. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = true;
  324. current_position[E_AXIS] += 7.2f;
  325. float feedrate = 562;
  326. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  327. st_synchronize();
  328. current_position[E_AXIS] += 14.4f;
  329. feedrate = 871;
  330. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  331. st_synchronize();
  332. current_position[E_AXIS] += 36.0f;
  333. feedrate = 1393;
  334. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  335. st_synchronize();
  336. current_position[E_AXIS] += 14.4f;
  337. feedrate = 871;
  338. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  339. st_synchronize();
  340. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = false;
  341. }
  342. void mmu_M600_load_filament(bool automatic)
  343. {
  344. //load filament for mmu v2
  345. uint8_t filament = mmu_extruder;
  346. if (!automatic) {
  347. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  348. if(yes) filament = choose_extruder_menu();
  349. }
  350. else {
  351. filament = (filament+1)%5;
  352. }
  353. lcd_update_enable(false);
  354. lcd_clear();
  355. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  356. lcd_print(" ");
  357. lcd_print(filament + 1);
  358. snmm_filaments_used |= (1 << filament); //for stop print
  359. // printf_P(PSTR("T code: %d \n"), filament);
  360. // mmu_printf_P(PSTR("T%d\n"), filament);
  361. mmu_command(MMU_CMD_T0 + filament);
  362. manage_response(false, true);
  363. mmu_extruder = filament; //filament change is finished
  364. mmu_load_to_nozzle();
  365. st_synchronize();
  366. current_position[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  367. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2, active_extruder);
  368. }
  369. void extr_mov(float shift, float feed_rate)
  370. { //move extruder no matter what the current heater temperature is
  371. set_extrude_min_temp(.0);
  372. current_position[E_AXIS] += shift;
  373. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feed_rate, active_extruder);
  374. set_extrude_min_temp(EXTRUDE_MINTEMP);
  375. }
  376. void change_extr(int
  377. #ifdef SNMM
  378. extr
  379. #endif //SNMM
  380. ) { //switches multiplexer for extruders
  381. #ifdef SNMM
  382. st_synchronize();
  383. delay(100);
  384. disable_e0();
  385. disable_e1();
  386. disable_e2();
  387. mmu_extruder = extr;
  388. pinMode(E_MUX0_PIN, OUTPUT);
  389. pinMode(E_MUX1_PIN, OUTPUT);
  390. switch (extr) {
  391. case 1:
  392. WRITE(E_MUX0_PIN, HIGH);
  393. WRITE(E_MUX1_PIN, LOW);
  394. break;
  395. case 2:
  396. WRITE(E_MUX0_PIN, LOW);
  397. WRITE(E_MUX1_PIN, HIGH);
  398. break;
  399. case 3:
  400. WRITE(E_MUX0_PIN, HIGH);
  401. WRITE(E_MUX1_PIN, HIGH);
  402. break;
  403. default:
  404. WRITE(E_MUX0_PIN, LOW);
  405. WRITE(E_MUX1_PIN, LOW);
  406. break;
  407. }
  408. delay(100);
  409. #endif
  410. }
  411. int get_ext_nr()
  412. { //reads multiplexer input pins and return current extruder number (counted from 0)
  413. #ifndef SNMM
  414. return(mmu_extruder); //update needed
  415. #else
  416. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  417. #endif
  418. }
  419. void display_loading()
  420. {
  421. switch (mmu_extruder)
  422. {
  423. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  424. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  425. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  426. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  427. }
  428. }
  429. void extr_adj(int extruder) //loading filament for SNMM
  430. {
  431. #ifndef SNMM
  432. uint8_t cmd = MMU_CMD_L0 + extruder;
  433. if (cmd > MMU_CMD_L4)
  434. {
  435. printf_P(PSTR("Filament out of range %d \n"),extruder);
  436. return;
  437. }
  438. mmu_command(cmd);
  439. //show which filament is currently loaded
  440. lcd_update_enable(false);
  441. lcd_clear();
  442. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  443. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  444. //else lcd.print(" ");
  445. lcd_print(" ");
  446. lcd_print(extruder + 1);
  447. // get response
  448. manage_response(false, false);
  449. lcd_update_enable(true);
  450. //lcd_return_to_status();
  451. #else
  452. bool correct;
  453. max_feedrate[E_AXIS] =80;
  454. //max_feedrate[E_AXIS] = 50;
  455. START:
  456. lcd_clear();
  457. lcd_set_cursor(0, 0);
  458. switch (extruder) {
  459. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  460. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  461. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  462. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  463. }
  464. KEEPALIVE_STATE(PAUSED_FOR_USER);
  465. do{
  466. extr_mov(0.001,1000);
  467. delay_keep_alive(2);
  468. } while (!lcd_clicked());
  469. //delay_keep_alive(500);
  470. KEEPALIVE_STATE(IN_HANDLER);
  471. st_synchronize();
  472. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  473. //if (!correct) goto START;
  474. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  475. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  476. extr_mov(bowden_length[extruder], 500);
  477. lcd_clear();
  478. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  479. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  480. else lcd_print(" ");
  481. lcd_print(mmu_extruder + 1);
  482. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  483. st_synchronize();
  484. max_feedrate[E_AXIS] = 50;
  485. lcd_update_enable(true);
  486. lcd_return_to_status();
  487. lcdDrawUpdate = 2;
  488. #endif
  489. }
  490. void extr_unload()
  491. { //unload just current filament for multimaterial printers
  492. #ifdef SNMM
  493. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  494. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  495. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  496. #endif
  497. if (degHotend0() > EXTRUDE_MINTEMP)
  498. {
  499. #ifndef SNMM
  500. st_synchronize();
  501. //show which filament is currently unloaded
  502. lcd_update_enable(false);
  503. lcd_clear();
  504. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  505. lcd_print(" ");
  506. lcd_print(mmu_extruder + 1);
  507. current_position[E_AXIS] -= 80;
  508. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  509. st_synchronize();
  510. printf_P(PSTR("U0\n"));
  511. fprintf_P(uart2io, PSTR("U0\n"));
  512. // get response
  513. manage_response(false, true);
  514. lcd_update_enable(true);
  515. #else //SNMM
  516. lcd_clear();
  517. lcd_display_message_fullscreen_P(PSTR(""));
  518. max_feedrate[E_AXIS] = 50;
  519. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  520. lcd_print(" ");
  521. lcd_print(mmu_extruder + 1);
  522. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  523. if (current_position[Z_AXIS] < 15) {
  524. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  525. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  526. }
  527. current_position[E_AXIS] += 10; //extrusion
  528. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  529. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  530. if (current_temperature[0] < 230) { //PLA & all other filaments
  531. current_position[E_AXIS] += 5.4;
  532. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  533. current_position[E_AXIS] += 3.2;
  534. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  535. current_position[E_AXIS] += 3;
  536. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  537. }
  538. else { //ABS
  539. current_position[E_AXIS] += 3.1;
  540. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  541. current_position[E_AXIS] += 3.1;
  542. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  543. current_position[E_AXIS] += 4;
  544. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  545. /*current_position[X_AXIS] += 23; //delay
  546. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay
  547. current_position[X_AXIS] -= 23; //delay
  548. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  549. delay_keep_alive(4700);
  550. }
  551. max_feedrate[E_AXIS] = 80;
  552. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  553. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  554. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  555. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  556. st_synchronize();
  557. //st_current_init();
  558. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  559. else st_current_set(2, tmp_motor_loud[2]);
  560. lcd_update_enable(true);
  561. lcd_return_to_status();
  562. max_feedrate[E_AXIS] = 50;
  563. #endif //SNMM
  564. }
  565. else
  566. {
  567. lcd_clear();
  568. lcd_set_cursor(0, 0);
  569. lcd_puts_P(_T(MSG_ERROR));
  570. lcd_set_cursor(0, 2);
  571. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  572. delay(2000);
  573. lcd_clear();
  574. }
  575. //lcd_return_to_status();
  576. }
  577. //wrapper functions for loading filament
  578. void extr_adj_0()
  579. {
  580. #ifndef SNMM
  581. enquecommand_P(PSTR("M701 E0"));
  582. #else
  583. change_extr(0);
  584. extr_adj(0);
  585. #endif
  586. }
  587. void extr_adj_1()
  588. {
  589. #ifndef SNMM
  590. enquecommand_P(PSTR("M701 E1"));
  591. #else
  592. change_extr(1);
  593. extr_adj(1);
  594. #endif
  595. }
  596. void extr_adj_2()
  597. {
  598. #ifndef SNMM
  599. enquecommand_P(PSTR("M701 E2"));
  600. #else
  601. change_extr(2);
  602. extr_adj(2);
  603. #endif
  604. }
  605. void extr_adj_3()
  606. {
  607. #ifndef SNMM
  608. enquecommand_P(PSTR("M701 E3"));
  609. #else
  610. change_extr(3);
  611. extr_adj(3);
  612. #endif
  613. }
  614. void extr_adj_4()
  615. {
  616. #ifndef SNMM
  617. enquecommand_P(PSTR("M701 E4"));
  618. #else
  619. change_extr(4);
  620. extr_adj(4);
  621. #endif
  622. }
  623. void load_all()
  624. {
  625. #ifndef SNMM
  626. enquecommand_P(PSTR("M701 E0"));
  627. enquecommand_P(PSTR("M701 E1"));
  628. enquecommand_P(PSTR("M701 E2"));
  629. enquecommand_P(PSTR("M701 E3"));
  630. enquecommand_P(PSTR("M701 E4"));
  631. #else
  632. for (int i = 0; i < 4; i++)
  633. {
  634. change_extr(i);
  635. extr_adj(i);
  636. }
  637. #endif
  638. }
  639. //wrapper functions for changing extruders
  640. void extr_change_0()
  641. {
  642. change_extr(0);
  643. lcd_return_to_status();
  644. }
  645. void extr_change_1()
  646. {
  647. change_extr(1);
  648. lcd_return_to_status();
  649. }
  650. void extr_change_2()
  651. {
  652. change_extr(2);
  653. lcd_return_to_status();
  654. }
  655. void extr_change_3()
  656. {
  657. change_extr(3);
  658. lcd_return_to_status();
  659. }
  660. //wrapper functions for unloading filament
  661. void extr_unload_all()
  662. {
  663. if (degHotend0() > EXTRUDE_MINTEMP)
  664. {
  665. for (int i = 0; i < 4; i++)
  666. {
  667. change_extr(i);
  668. extr_unload();
  669. }
  670. }
  671. else
  672. {
  673. lcd_clear();
  674. lcd_set_cursor(0, 0);
  675. lcd_puts_P(_T(MSG_ERROR));
  676. lcd_set_cursor(0, 2);
  677. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  678. delay(2000);
  679. lcd_clear();
  680. lcd_return_to_status();
  681. }
  682. }
  683. //unloading just used filament (for snmm)
  684. void extr_unload_used()
  685. {
  686. if (degHotend0() > EXTRUDE_MINTEMP) {
  687. for (int i = 0; i < 4; i++) {
  688. if (snmm_filaments_used & (1 << i)) {
  689. change_extr(i);
  690. extr_unload();
  691. }
  692. }
  693. snmm_filaments_used = 0;
  694. }
  695. else {
  696. lcd_clear();
  697. lcd_set_cursor(0, 0);
  698. lcd_puts_P(_T(MSG_ERROR));
  699. lcd_set_cursor(0, 2);
  700. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  701. delay(2000);
  702. lcd_clear();
  703. lcd_return_to_status();
  704. }
  705. }
  706. void extr_unload_0()
  707. {
  708. change_extr(0);
  709. extr_unload();
  710. }
  711. void extr_unload_1()
  712. {
  713. change_extr(1);
  714. extr_unload();
  715. }
  716. void extr_unload_2()
  717. {
  718. change_extr(2);
  719. extr_unload();
  720. }
  721. void extr_unload_3()
  722. {
  723. change_extr(3);
  724. extr_unload();
  725. }
  726. void extr_unload_4()
  727. {
  728. change_extr(4);
  729. extr_unload();
  730. }