mmu.cpp 28 KB

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