mmu.cpp 30 KB

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