mmu.cpp 21 KB

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