mmu.cpp 28 KB

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