mmu.cpp 30 KB

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