mmu.cpp 28 KB

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