mmu.cpp 24 KB

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