mmu.cpp 27 KB

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