mmu.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. void extr_unload()
  574. { //unload just current filament for multimaterial printers
  575. #ifdef SNMM
  576. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  577. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  578. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  579. #endif
  580. if (degHotend0() > EXTRUDE_MINTEMP)
  581. {
  582. #ifndef SNMM
  583. st_synchronize();
  584. //show which filament is currently unloaded
  585. lcd_update_enable(false);
  586. lcd_clear();
  587. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  588. lcd_print(" ");
  589. lcd_print(mmu_extruder + 1);
  590. current_position[E_AXIS] -= 80;
  591. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  592. st_synchronize();
  593. mmu_command(MMU_CMD_U0);
  594. // get response
  595. manage_response(false, true);
  596. lcd_update_enable(true);
  597. #else //SNMM
  598. lcd_clear();
  599. lcd_display_message_fullscreen_P(PSTR(""));
  600. max_feedrate[E_AXIS] = 50;
  601. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  602. lcd_print(" ");
  603. lcd_print(mmu_extruder + 1);
  604. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  605. if (current_position[Z_AXIS] < 15) {
  606. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  607. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  608. }
  609. current_position[E_AXIS] += 10; //extrusion
  610. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  611. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  612. if (current_temperature[0] < 230) { //PLA & all other filaments
  613. current_position[E_AXIS] += 5.4;
  614. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  615. current_position[E_AXIS] += 3.2;
  616. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  617. current_position[E_AXIS] += 3;
  618. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  619. }
  620. else { //ABS
  621. current_position[E_AXIS] += 3.1;
  622. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  623. current_position[E_AXIS] += 3.1;
  624. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  625. current_position[E_AXIS] += 4;
  626. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  627. /*current_position[X_AXIS] += 23; //delay
  628. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay
  629. current_position[X_AXIS] -= 23; //delay
  630. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  631. delay_keep_alive(4700);
  632. }
  633. max_feedrate[E_AXIS] = 80;
  634. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  635. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  636. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  637. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  638. st_synchronize();
  639. //st_current_init();
  640. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  641. else st_current_set(2, tmp_motor_loud[2]);
  642. lcd_update_enable(true);
  643. lcd_return_to_status();
  644. max_feedrate[E_AXIS] = 50;
  645. #endif //SNMM
  646. }
  647. else
  648. {
  649. lcd_clear();
  650. lcd_set_cursor(0, 0);
  651. lcd_puts_P(_T(MSG_ERROR));
  652. lcd_set_cursor(0, 2);
  653. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  654. delay(2000);
  655. lcd_clear();
  656. }
  657. //lcd_return_to_status();
  658. }
  659. //wrapper functions for loading filament
  660. void extr_adj_0()
  661. {
  662. #ifndef SNMM
  663. enquecommand_P(PSTR("M701 E0"));
  664. #else
  665. change_extr(0);
  666. extr_adj(0);
  667. #endif
  668. }
  669. void extr_adj_1()
  670. {
  671. #ifndef SNMM
  672. enquecommand_P(PSTR("M701 E1"));
  673. #else
  674. change_extr(1);
  675. extr_adj(1);
  676. #endif
  677. }
  678. void extr_adj_2()
  679. {
  680. #ifndef SNMM
  681. enquecommand_P(PSTR("M701 E2"));
  682. #else
  683. change_extr(2);
  684. extr_adj(2);
  685. #endif
  686. }
  687. void extr_adj_3()
  688. {
  689. #ifndef SNMM
  690. enquecommand_P(PSTR("M701 E3"));
  691. #else
  692. change_extr(3);
  693. extr_adj(3);
  694. #endif
  695. }
  696. void extr_adj_4()
  697. {
  698. #ifndef SNMM
  699. enquecommand_P(PSTR("M701 E4"));
  700. #else
  701. change_extr(4);
  702. extr_adj(4);
  703. #endif
  704. }
  705. void mmu_eject_fil_0()
  706. {
  707. mmu_eject_filament(0, true);
  708. }
  709. void mmu_eject_fil_1()
  710. {
  711. mmu_eject_filament(1, true);
  712. }
  713. void mmu_eject_fil_2()
  714. {
  715. mmu_eject_filament(2, true);
  716. }
  717. void mmu_eject_fil_3()
  718. {
  719. mmu_eject_filament(3, true);
  720. }
  721. void mmu_eject_fil_4()
  722. {
  723. mmu_eject_filament(4, true);
  724. }
  725. void load_all()
  726. {
  727. #ifndef SNMM
  728. enquecommand_P(PSTR("M701 E0"));
  729. enquecommand_P(PSTR("M701 E1"));
  730. enquecommand_P(PSTR("M701 E2"));
  731. enquecommand_P(PSTR("M701 E3"));
  732. enquecommand_P(PSTR("M701 E4"));
  733. #else
  734. for (int i = 0; i < 4; i++)
  735. {
  736. change_extr(i);
  737. extr_adj(i);
  738. }
  739. #endif
  740. }
  741. //wrapper functions for changing extruders
  742. void extr_change_0()
  743. {
  744. change_extr(0);
  745. lcd_return_to_status();
  746. }
  747. void extr_change_1()
  748. {
  749. change_extr(1);
  750. lcd_return_to_status();
  751. }
  752. void extr_change_2()
  753. {
  754. change_extr(2);
  755. lcd_return_to_status();
  756. }
  757. void extr_change_3()
  758. {
  759. change_extr(3);
  760. lcd_return_to_status();
  761. }
  762. //wrapper functions for unloading filament
  763. void extr_unload_all()
  764. {
  765. if (degHotend0() > EXTRUDE_MINTEMP)
  766. {
  767. for (int i = 0; i < 4; i++)
  768. {
  769. change_extr(i);
  770. extr_unload();
  771. }
  772. }
  773. else
  774. {
  775. lcd_clear();
  776. lcd_set_cursor(0, 0);
  777. lcd_puts_P(_T(MSG_ERROR));
  778. lcd_set_cursor(0, 2);
  779. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  780. delay(2000);
  781. lcd_clear();
  782. lcd_return_to_status();
  783. }
  784. }
  785. //unloading just used filament (for snmm)
  786. void extr_unload_used()
  787. {
  788. if (degHotend0() > EXTRUDE_MINTEMP) {
  789. for (int i = 0; i < 4; i++) {
  790. if (snmm_filaments_used & (1 << i)) {
  791. change_extr(i);
  792. extr_unload();
  793. }
  794. }
  795. snmm_filaments_used = 0;
  796. }
  797. else {
  798. lcd_clear();
  799. lcd_set_cursor(0, 0);
  800. lcd_puts_P(_T(MSG_ERROR));
  801. lcd_set_cursor(0, 2);
  802. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  803. delay(2000);
  804. lcd_clear();
  805. lcd_return_to_status();
  806. }
  807. }
  808. void extr_unload_0()
  809. {
  810. change_extr(0);
  811. extr_unload();
  812. }
  813. void extr_unload_1()
  814. {
  815. change_extr(1);
  816. extr_unload();
  817. }
  818. void extr_unload_2()
  819. {
  820. change_extr(2);
  821. extr_unload();
  822. }
  823. void extr_unload_3()
  824. {
  825. change_extr(3);
  826. extr_unload();
  827. }
  828. void extr_unload_4()
  829. {
  830. change_extr(4);
  831. extr_unload();
  832. }
  833. bool mmu_check_version()
  834. {
  835. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  836. }
  837. void mmu_show_warning()
  838. {
  839. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  840. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  841. }
  842. void mmu_eject_filament(uint8_t filament, bool recover)
  843. {
  844. if (filament < 5)
  845. {
  846. if (degHotend0() > EXTRUDE_MINTEMP)
  847. {
  848. st_synchronize();
  849. lcd_update_enable(false);
  850. lcd_clear();
  851. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  852. current_position[E_AXIS] -= 80;
  853. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  854. st_synchronize();
  855. lcd_update_enable(true);
  856. mmu_command(MMU_CMD_E0 + filament);
  857. manage_response(false, false);
  858. if (recover)
  859. {
  860. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  861. mmu_command(MMU_CMD_R0);
  862. manage_response(false, false);
  863. }
  864. }
  865. else
  866. {
  867. lcd_clear();
  868. lcd_set_cursor(0, 0);
  869. lcd_puts_P(_T(MSG_ERROR));
  870. lcd_set_cursor(0, 2);
  871. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  872. delay(2000);
  873. lcd_clear();
  874. }
  875. }
  876. else
  877. {
  878. puts_P(PSTR("Filament nr out of range!"));
  879. }
  880. }