mmu.cpp 31 KB

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