mmu.cpp 25 KB

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