mmu.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 83
  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. #ifdef MMU_M600_SWITCH_EXTRUDER
  431. yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  432. if(yes) tmp_extruder = choose_extruder_menu();
  433. else tmp_extruder = mmu_extruder;
  434. #endif //MMU_M600_SWITCH_EXTRUDER
  435. }
  436. else {
  437. tmp_extruder = (tmp_extruder+1)%5;
  438. }
  439. lcd_update_enable(false);
  440. lcd_clear();
  441. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  442. lcd_print(" ");
  443. lcd_print(tmp_extruder + 1);
  444. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  445. // printf_P(PSTR("T code: %d \n"), tmp_extruder);
  446. // mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  447. mmu_command(MMU_CMD_T0 + tmp_extruder);
  448. manage_response(false, true);
  449. mmu_command(MMU_CMD_C0);
  450. mmu_extruder = tmp_extruder; //filament change is finished
  451. mmu_load_to_nozzle();
  452. st_synchronize();
  453. current_position[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  454. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2, active_extruder);
  455. }
  456. void extr_mov(float shift, float feed_rate)
  457. { //move extruder no matter what the current heater temperature is
  458. set_extrude_min_temp(.0);
  459. current_position[E_AXIS] += shift;
  460. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feed_rate, active_extruder);
  461. set_extrude_min_temp(EXTRUDE_MINTEMP);
  462. }
  463. void change_extr(int extr) { //switches multiplexer for extruders
  464. #ifdef SNMM
  465. st_synchronize();
  466. delay(100);
  467. disable_e0();
  468. disable_e1();
  469. disable_e2();
  470. mmu_extruder = extr;
  471. pinMode(E_MUX0_PIN, OUTPUT);
  472. pinMode(E_MUX1_PIN, OUTPUT);
  473. switch (extr) {
  474. case 1:
  475. WRITE(E_MUX0_PIN, HIGH);
  476. WRITE(E_MUX1_PIN, LOW);
  477. break;
  478. case 2:
  479. WRITE(E_MUX0_PIN, LOW);
  480. WRITE(E_MUX1_PIN, HIGH);
  481. break;
  482. case 3:
  483. WRITE(E_MUX0_PIN, HIGH);
  484. WRITE(E_MUX1_PIN, HIGH);
  485. break;
  486. default:
  487. WRITE(E_MUX0_PIN, LOW);
  488. WRITE(E_MUX1_PIN, LOW);
  489. break;
  490. }
  491. delay(100);
  492. #endif
  493. }
  494. int get_ext_nr()
  495. { //reads multiplexer input pins and return current extruder number (counted from 0)
  496. #ifndef SNMM
  497. return(mmu_extruder); //update needed
  498. #else
  499. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  500. #endif
  501. }
  502. void display_loading()
  503. {
  504. switch (mmu_extruder)
  505. {
  506. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  507. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  508. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  509. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  510. }
  511. }
  512. void extr_adj(int extruder) //loading filament for SNMM
  513. {
  514. #ifndef SNMM
  515. uint8_t cmd = MMU_CMD_L0 + extruder;
  516. if (cmd > MMU_CMD_L4)
  517. {
  518. printf_P(PSTR("Filament out of range %d \n"),extruder);
  519. return;
  520. }
  521. mmu_command(cmd);
  522. //show which filament is currently loaded
  523. lcd_update_enable(false);
  524. lcd_clear();
  525. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  526. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  527. //else lcd.print(" ");
  528. lcd_print(" ");
  529. lcd_print(extruder + 1);
  530. // get response
  531. manage_response(false, false);
  532. lcd_update_enable(true);
  533. //lcd_return_to_status();
  534. #else
  535. bool correct;
  536. max_feedrate[E_AXIS] =80;
  537. //max_feedrate[E_AXIS] = 50;
  538. START:
  539. lcd_clear();
  540. lcd_set_cursor(0, 0);
  541. switch (extruder) {
  542. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  543. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  544. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  545. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  546. }
  547. KEEPALIVE_STATE(PAUSED_FOR_USER);
  548. do{
  549. extr_mov(0.001,1000);
  550. delay_keep_alive(2);
  551. } while (!lcd_clicked());
  552. //delay_keep_alive(500);
  553. KEEPALIVE_STATE(IN_HANDLER);
  554. st_synchronize();
  555. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  556. //if (!correct) goto START;
  557. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  558. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  559. extr_mov(bowden_length[extruder], 500);
  560. lcd_clear();
  561. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  562. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  563. else lcd_print(" ");
  564. lcd_print(mmu_extruder + 1);
  565. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  566. st_synchronize();
  567. max_feedrate[E_AXIS] = 50;
  568. lcd_update_enable(true);
  569. lcd_return_to_status();
  570. lcdDrawUpdate = 2;
  571. #endif
  572. }
  573. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  574. //!
  575. //! Ideas to minimize flash consumption of this code:
  576. //!
  577. //! Create const array of extrude and feed_rate on stack, call increment current_position, plan_buffer_line() and st_synchronize()
  578. //! in loop iterating over array.
  579. //!
  580. //! Same as previous, but create array in PROGMEM and call PGM_read instructions in a loop.
  581. //!
  582. static void filament_ramming()
  583. {
  584. current_position[E_AXIS] += 1;
  585. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 1000 / 60, active_extruder);
  586. st_synchronize();
  587. current_position[E_AXIS] += 1;
  588. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 1500 / 60, active_extruder);
  589. st_synchronize();
  590. current_position[E_AXIS] += 2;
  591. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  592. st_synchronize();
  593. current_position[E_AXIS] += 1.5;
  594. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  595. st_synchronize();
  596. current_position[E_AXIS] += 2.5;
  597. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 4000 / 60, active_extruder);
  598. st_synchronize();
  599. current_position[E_AXIS] -= 15;
  600. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 5000 / 60, active_extruder);
  601. st_synchronize();
  602. current_position[E_AXIS] -= 14;
  603. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 1200 / 60, active_extruder);
  604. st_synchronize();
  605. current_position[E_AXIS] -= 6;
  606. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder);
  607. st_synchronize();
  608. current_position[E_AXIS] += 10;
  609. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 700 / 60, active_extruder);
  610. st_synchronize();
  611. current_position[E_AXIS] -= 10;
  612. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder);
  613. st_synchronize();
  614. current_position[E_AXIS] -= 50;
  615. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  616. st_synchronize();
  617. }
  618. void extr_unload()
  619. { //unload just current filament for multimaterial printers
  620. #ifdef SNMM
  621. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  622. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  623. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  624. #endif
  625. if (degHotend0() > EXTRUDE_MINTEMP)
  626. {
  627. #ifndef SNMM
  628. st_synchronize();
  629. //show which filament is currently unloaded
  630. lcd_update_enable(false);
  631. lcd_clear();
  632. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  633. lcd_print(" ");
  634. lcd_print(mmu_extruder + 1);
  635. filament_ramming();
  636. mmu_command(MMU_CMD_U0);
  637. // get response
  638. manage_response(false, true);
  639. lcd_update_enable(true);
  640. #else //SNMM
  641. lcd_clear();
  642. lcd_display_message_fullscreen_P(PSTR(""));
  643. max_feedrate[E_AXIS] = 50;
  644. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  645. lcd_print(" ");
  646. lcd_print(mmu_extruder + 1);
  647. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  648. if (current_position[Z_AXIS] < 15) {
  649. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  650. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  651. }
  652. current_position[E_AXIS] += 10; //extrusion
  653. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  654. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  655. if (current_temperature[0] < 230) { //PLA & all other filaments
  656. current_position[E_AXIS] += 5.4;
  657. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  658. current_position[E_AXIS] += 3.2;
  659. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  660. current_position[E_AXIS] += 3;
  661. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  662. }
  663. else { //ABS
  664. current_position[E_AXIS] += 3.1;
  665. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  666. current_position[E_AXIS] += 3.1;
  667. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  668. current_position[E_AXIS] += 4;
  669. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  670. /*current_position[X_AXIS] += 23; //delay
  671. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay
  672. current_position[X_AXIS] -= 23; //delay
  673. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  674. delay_keep_alive(4700);
  675. }
  676. max_feedrate[E_AXIS] = 80;
  677. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  678. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  679. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  680. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  681. st_synchronize();
  682. //st_current_init();
  683. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  684. else st_current_set(2, tmp_motor_loud[2]);
  685. lcd_update_enable(true);
  686. lcd_return_to_status();
  687. max_feedrate[E_AXIS] = 50;
  688. #endif //SNMM
  689. }
  690. else
  691. {
  692. lcd_clear();
  693. lcd_set_cursor(0, 0);
  694. lcd_puts_P(_T(MSG_ERROR));
  695. lcd_set_cursor(0, 2);
  696. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  697. delay(2000);
  698. lcd_clear();
  699. }
  700. //lcd_return_to_status();
  701. }
  702. //wrapper functions for loading filament
  703. void extr_adj_0()
  704. {
  705. #ifndef SNMM
  706. enquecommand_P(PSTR("M701 E0"));
  707. #else
  708. change_extr(0);
  709. extr_adj(0);
  710. #endif
  711. }
  712. void extr_adj_1()
  713. {
  714. #ifndef SNMM
  715. enquecommand_P(PSTR("M701 E1"));
  716. #else
  717. change_extr(1);
  718. extr_adj(1);
  719. #endif
  720. }
  721. void extr_adj_2()
  722. {
  723. #ifndef SNMM
  724. enquecommand_P(PSTR("M701 E2"));
  725. #else
  726. change_extr(2);
  727. extr_adj(2);
  728. #endif
  729. }
  730. void extr_adj_3()
  731. {
  732. #ifndef SNMM
  733. enquecommand_P(PSTR("M701 E3"));
  734. #else
  735. change_extr(3);
  736. extr_adj(3);
  737. #endif
  738. }
  739. void extr_adj_4()
  740. {
  741. #ifndef SNMM
  742. enquecommand_P(PSTR("M701 E4"));
  743. #else
  744. change_extr(4);
  745. extr_adj(4);
  746. #endif
  747. }
  748. void mmu_eject_fil_0()
  749. {
  750. mmu_eject_filament(0, true);
  751. }
  752. void mmu_eject_fil_1()
  753. {
  754. mmu_eject_filament(1, true);
  755. }
  756. void mmu_eject_fil_2()
  757. {
  758. mmu_eject_filament(2, true);
  759. }
  760. void mmu_eject_fil_3()
  761. {
  762. mmu_eject_filament(3, true);
  763. }
  764. void mmu_eject_fil_4()
  765. {
  766. mmu_eject_filament(4, true);
  767. }
  768. void load_all()
  769. {
  770. #ifndef SNMM
  771. enquecommand_P(PSTR("M701 E0"));
  772. enquecommand_P(PSTR("M701 E1"));
  773. enquecommand_P(PSTR("M701 E2"));
  774. enquecommand_P(PSTR("M701 E3"));
  775. enquecommand_P(PSTR("M701 E4"));
  776. #else
  777. for (int i = 0; i < 4; i++)
  778. {
  779. change_extr(i);
  780. extr_adj(i);
  781. }
  782. #endif
  783. }
  784. //wrapper functions for changing extruders
  785. void extr_change_0()
  786. {
  787. change_extr(0);
  788. lcd_return_to_status();
  789. }
  790. void extr_change_1()
  791. {
  792. change_extr(1);
  793. lcd_return_to_status();
  794. }
  795. void extr_change_2()
  796. {
  797. change_extr(2);
  798. lcd_return_to_status();
  799. }
  800. void extr_change_3()
  801. {
  802. change_extr(3);
  803. lcd_return_to_status();
  804. }
  805. //wrapper functions for unloading filament
  806. void extr_unload_all()
  807. {
  808. if (degHotend0() > EXTRUDE_MINTEMP)
  809. {
  810. for (int i = 0; i < 4; i++)
  811. {
  812. change_extr(i);
  813. extr_unload();
  814. }
  815. }
  816. else
  817. {
  818. lcd_clear();
  819. lcd_set_cursor(0, 0);
  820. lcd_puts_P(_T(MSG_ERROR));
  821. lcd_set_cursor(0, 2);
  822. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  823. delay(2000);
  824. lcd_clear();
  825. lcd_return_to_status();
  826. }
  827. }
  828. //unloading just used filament (for snmm)
  829. void extr_unload_used()
  830. {
  831. if (degHotend0() > EXTRUDE_MINTEMP) {
  832. for (int i = 0; i < 4; i++) {
  833. if (snmm_filaments_used & (1 << i)) {
  834. change_extr(i);
  835. extr_unload();
  836. }
  837. }
  838. snmm_filaments_used = 0;
  839. }
  840. else {
  841. lcd_clear();
  842. lcd_set_cursor(0, 0);
  843. lcd_puts_P(_T(MSG_ERROR));
  844. lcd_set_cursor(0, 2);
  845. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  846. delay(2000);
  847. lcd_clear();
  848. lcd_return_to_status();
  849. }
  850. }
  851. void extr_unload_0()
  852. {
  853. change_extr(0);
  854. extr_unload();
  855. }
  856. void extr_unload_1()
  857. {
  858. change_extr(1);
  859. extr_unload();
  860. }
  861. void extr_unload_2()
  862. {
  863. change_extr(2);
  864. extr_unload();
  865. }
  866. void extr_unload_3()
  867. {
  868. change_extr(3);
  869. extr_unload();
  870. }
  871. void extr_unload_4()
  872. {
  873. change_extr(4);
  874. extr_unload();
  875. }
  876. bool mmu_check_version()
  877. {
  878. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  879. }
  880. void mmu_show_warning()
  881. {
  882. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  883. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  884. }
  885. void mmu_eject_filament(uint8_t filament, bool recover)
  886. {
  887. if (filament < 5)
  888. {
  889. if (degHotend0() > EXTRUDE_MINTEMP)
  890. {
  891. st_synchronize();
  892. lcd_update_enable(false);
  893. lcd_clear();
  894. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  895. current_position[E_AXIS] -= 80;
  896. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  897. st_synchronize();
  898. lcd_update_enable(true);
  899. mmu_command(MMU_CMD_E0 + filament);
  900. manage_response(false, false);
  901. if (recover)
  902. {
  903. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  904. mmu_command(MMU_CMD_R0);
  905. manage_response(false, false);
  906. }
  907. }
  908. else
  909. {
  910. lcd_clear();
  911. lcd_set_cursor(0, 0);
  912. lcd_puts_P(_T(MSG_ERROR));
  913. lcd_set_cursor(0, 2);
  914. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  915. delay(2000);
  916. lcd_clear();
  917. }
  918. }
  919. else
  920. {
  921. puts_P(PSTR("Filament nr out of range!"));
  922. }
  923. }