mmu.cpp 30 KB

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