mmu.cpp 30 KB

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