mmu.cpp 27 KB

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