mmu.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. bool yes = false;
  429. tmp_extruder = mmu_extruder;
  430. if (!automatic) {
  431. #ifdef MMU_M600_SWITCH_EXTRUDER
  432. yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  433. if(yes) tmp_extruder = choose_extruder_menu();
  434. else tmp_extruder = mmu_extruder;
  435. #endif //MMU_M600_SWITCH_EXTRUDER
  436. }
  437. else {
  438. tmp_extruder = (tmp_extruder+1)%5;
  439. }
  440. lcd_update_enable(false);
  441. lcd_clear();
  442. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  443. lcd_print(" ");
  444. lcd_print(tmp_extruder + 1);
  445. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  446. // printf_P(PSTR("T code: %d \n"), tmp_extruder);
  447. // mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  448. mmu_command(MMU_CMD_T0 + tmp_extruder);
  449. manage_response(false, true);
  450. mmu_command(MMU_CMD_C0);
  451. mmu_extruder = tmp_extruder; //filament change is finished
  452. mmu_load_to_nozzle();
  453. st_synchronize();
  454. current_position[E_AXIS]+= FILAMENTCHANGE_FINALFEED ;
  455. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2, active_extruder);
  456. }
  457. void extr_mov(float shift, float feed_rate)
  458. { //move extruder no matter what the current heater temperature is
  459. set_extrude_min_temp(.0);
  460. current_position[E_AXIS] += shift;
  461. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feed_rate, active_extruder);
  462. set_extrude_min_temp(EXTRUDE_MINTEMP);
  463. }
  464. void change_extr(int
  465. #ifdef SNMM
  466. extr
  467. #endif //SNMM
  468. ) { //switches multiplexer for extruders
  469. #ifdef SNMM
  470. st_synchronize();
  471. delay(100);
  472. disable_e0();
  473. disable_e1();
  474. disable_e2();
  475. mmu_extruder = extr;
  476. pinMode(E_MUX0_PIN, OUTPUT);
  477. pinMode(E_MUX1_PIN, OUTPUT);
  478. switch (extr) {
  479. case 1:
  480. WRITE(E_MUX0_PIN, HIGH);
  481. WRITE(E_MUX1_PIN, LOW);
  482. break;
  483. case 2:
  484. WRITE(E_MUX0_PIN, LOW);
  485. WRITE(E_MUX1_PIN, HIGH);
  486. break;
  487. case 3:
  488. WRITE(E_MUX0_PIN, HIGH);
  489. WRITE(E_MUX1_PIN, HIGH);
  490. break;
  491. default:
  492. WRITE(E_MUX0_PIN, LOW);
  493. WRITE(E_MUX1_PIN, LOW);
  494. break;
  495. }
  496. delay(100);
  497. #endif
  498. }
  499. int get_ext_nr()
  500. { //reads multiplexer input pins and return current extruder number (counted from 0)
  501. #ifndef SNMM
  502. return(mmu_extruder); //update needed
  503. #else
  504. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  505. #endif
  506. }
  507. void display_loading()
  508. {
  509. switch (mmu_extruder)
  510. {
  511. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  512. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  513. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  514. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  515. }
  516. }
  517. void extr_adj(int extruder) //loading filament for SNMM
  518. {
  519. #ifndef SNMM
  520. uint8_t cmd = MMU_CMD_L0 + extruder;
  521. if (cmd > MMU_CMD_L4)
  522. {
  523. printf_P(PSTR("Filament out of range %d \n"),extruder);
  524. return;
  525. }
  526. mmu_command(cmd);
  527. //show which filament is currently loaded
  528. lcd_update_enable(false);
  529. lcd_clear();
  530. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  531. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  532. //else lcd.print(" ");
  533. lcd_print(" ");
  534. lcd_print(extruder + 1);
  535. // get response
  536. manage_response(false, false);
  537. lcd_update_enable(true);
  538. //lcd_return_to_status();
  539. #else
  540. bool correct;
  541. max_feedrate[E_AXIS] =80;
  542. //max_feedrate[E_AXIS] = 50;
  543. START:
  544. lcd_clear();
  545. lcd_set_cursor(0, 0);
  546. switch (extruder) {
  547. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  548. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  549. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  550. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  551. }
  552. KEEPALIVE_STATE(PAUSED_FOR_USER);
  553. do{
  554. extr_mov(0.001,1000);
  555. delay_keep_alive(2);
  556. } while (!lcd_clicked());
  557. //delay_keep_alive(500);
  558. KEEPALIVE_STATE(IN_HANDLER);
  559. st_synchronize();
  560. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  561. //if (!correct) goto START;
  562. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  563. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  564. extr_mov(bowden_length[extruder], 500);
  565. lcd_clear();
  566. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  567. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  568. else lcd_print(" ");
  569. lcd_print(mmu_extruder + 1);
  570. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  571. st_synchronize();
  572. max_feedrate[E_AXIS] = 50;
  573. lcd_update_enable(true);
  574. lcd_return_to_status();
  575. lcdDrawUpdate = 2;
  576. #endif
  577. }
  578. struct E_step
  579. {
  580. float extrude; //!< extrude distance in mm
  581. float feed_rate; //!< feed rate in mm/s
  582. };
  583. static const E_step ramming_sequence[] PROGMEM =
  584. {
  585. {1.0, 1000.0/60},
  586. {1.0, 1500.0/60},
  587. {2.0, 2000.0/60},
  588. {1.5, 3000.0/60},
  589. {2.5, 4000.0/60},
  590. {-15.0, 5000.0/60},
  591. {-14.0, 1200.0/60},
  592. {-6.0, 600.0/60},
  593. {10.0, 700.0/60},
  594. {-10.0, 400.0/60},
  595. {-50.0, 2000.0/60},
  596. };
  597. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  598. static void filament_ramming()
  599. {
  600. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  601. {
  602. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  603. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  604. current_position[E_AXIS], pgm_read_float(&(ramming_sequence[i].feed_rate)), active_extruder);
  605. st_synchronize();
  606. }
  607. }
  608. void extr_unload()
  609. { //unload just current filament for multimaterial printers
  610. #ifdef SNMM
  611. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  612. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  613. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  614. #endif
  615. if (degHotend0() > EXTRUDE_MINTEMP)
  616. {
  617. #ifndef SNMM
  618. st_synchronize();
  619. //show which filament is currently unloaded
  620. lcd_update_enable(false);
  621. lcd_clear();
  622. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  623. lcd_print(" ");
  624. lcd_print(mmu_extruder + 1);
  625. filament_ramming();
  626. mmu_command(MMU_CMD_U0);
  627. // get response
  628. manage_response(false, true);
  629. lcd_update_enable(true);
  630. #else //SNMM
  631. lcd_clear();
  632. lcd_display_message_fullscreen_P(PSTR(""));
  633. max_feedrate[E_AXIS] = 50;
  634. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  635. lcd_print(" ");
  636. lcd_print(mmu_extruder + 1);
  637. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  638. if (current_position[Z_AXIS] < 15) {
  639. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  640. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  641. }
  642. current_position[E_AXIS] += 10; //extrusion
  643. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  644. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  645. if (current_temperature[0] < 230) { //PLA & all other filaments
  646. current_position[E_AXIS] += 5.4;
  647. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  648. current_position[E_AXIS] += 3.2;
  649. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  650. current_position[E_AXIS] += 3;
  651. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  652. }
  653. else { //ABS
  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], 2000 / 60, active_extruder);
  656. current_position[E_AXIS] += 3.1;
  657. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  658. current_position[E_AXIS] += 4;
  659. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  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. current_position[X_AXIS] -= 23; //delay
  663. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  664. delay_keep_alive(4700);
  665. }
  666. max_feedrate[E_AXIS] = 80;
  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. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  670. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  671. st_synchronize();
  672. //st_current_init();
  673. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  674. else st_current_set(2, tmp_motor_loud[2]);
  675. lcd_update_enable(true);
  676. lcd_return_to_status();
  677. max_feedrate[E_AXIS] = 50;
  678. #endif //SNMM
  679. }
  680. else
  681. {
  682. lcd_clear();
  683. lcd_set_cursor(0, 0);
  684. lcd_puts_P(_T(MSG_ERROR));
  685. lcd_set_cursor(0, 2);
  686. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  687. delay(2000);
  688. lcd_clear();
  689. }
  690. //lcd_return_to_status();
  691. }
  692. //wrapper functions for loading filament
  693. void extr_adj_0()
  694. {
  695. #ifndef SNMM
  696. enquecommand_P(PSTR("M701 E0"));
  697. #else
  698. change_extr(0);
  699. extr_adj(0);
  700. #endif
  701. }
  702. void extr_adj_1()
  703. {
  704. #ifndef SNMM
  705. enquecommand_P(PSTR("M701 E1"));
  706. #else
  707. change_extr(1);
  708. extr_adj(1);
  709. #endif
  710. }
  711. void extr_adj_2()
  712. {
  713. #ifndef SNMM
  714. enquecommand_P(PSTR("M701 E2"));
  715. #else
  716. change_extr(2);
  717. extr_adj(2);
  718. #endif
  719. }
  720. void extr_adj_3()
  721. {
  722. #ifndef SNMM
  723. enquecommand_P(PSTR("M701 E3"));
  724. #else
  725. change_extr(3);
  726. extr_adj(3);
  727. #endif
  728. }
  729. void extr_adj_4()
  730. {
  731. #ifndef SNMM
  732. enquecommand_P(PSTR("M701 E4"));
  733. #else
  734. change_extr(4);
  735. extr_adj(4);
  736. #endif
  737. }
  738. void mmu_eject_fil_0()
  739. {
  740. mmu_eject_filament(0, true);
  741. }
  742. void mmu_eject_fil_1()
  743. {
  744. mmu_eject_filament(1, true);
  745. }
  746. void mmu_eject_fil_2()
  747. {
  748. mmu_eject_filament(2, true);
  749. }
  750. void mmu_eject_fil_3()
  751. {
  752. mmu_eject_filament(3, true);
  753. }
  754. void mmu_eject_fil_4()
  755. {
  756. mmu_eject_filament(4, true);
  757. }
  758. void load_all()
  759. {
  760. #ifndef SNMM
  761. enquecommand_P(PSTR("M701 E0"));
  762. enquecommand_P(PSTR("M701 E1"));
  763. enquecommand_P(PSTR("M701 E2"));
  764. enquecommand_P(PSTR("M701 E3"));
  765. enquecommand_P(PSTR("M701 E4"));
  766. #else
  767. for (int i = 0; i < 4; i++)
  768. {
  769. change_extr(i);
  770. extr_adj(i);
  771. }
  772. #endif
  773. }
  774. //wrapper functions for changing extruders
  775. void extr_change_0()
  776. {
  777. change_extr(0);
  778. lcd_return_to_status();
  779. }
  780. void extr_change_1()
  781. {
  782. change_extr(1);
  783. lcd_return_to_status();
  784. }
  785. void extr_change_2()
  786. {
  787. change_extr(2);
  788. lcd_return_to_status();
  789. }
  790. void extr_change_3()
  791. {
  792. change_extr(3);
  793. lcd_return_to_status();
  794. }
  795. //wrapper functions for unloading filament
  796. void extr_unload_all()
  797. {
  798. if (degHotend0() > EXTRUDE_MINTEMP)
  799. {
  800. for (int i = 0; i < 4; i++)
  801. {
  802. change_extr(i);
  803. extr_unload();
  804. }
  805. }
  806. else
  807. {
  808. lcd_clear();
  809. lcd_set_cursor(0, 0);
  810. lcd_puts_P(_T(MSG_ERROR));
  811. lcd_set_cursor(0, 2);
  812. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  813. delay(2000);
  814. lcd_clear();
  815. lcd_return_to_status();
  816. }
  817. }
  818. //unloading just used filament (for snmm)
  819. void extr_unload_used()
  820. {
  821. if (degHotend0() > EXTRUDE_MINTEMP) {
  822. for (int i = 0; i < 4; i++) {
  823. if (snmm_filaments_used & (1 << i)) {
  824. change_extr(i);
  825. extr_unload();
  826. }
  827. }
  828. snmm_filaments_used = 0;
  829. }
  830. else {
  831. lcd_clear();
  832. lcd_set_cursor(0, 0);
  833. lcd_puts_P(_T(MSG_ERROR));
  834. lcd_set_cursor(0, 2);
  835. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  836. delay(2000);
  837. lcd_clear();
  838. lcd_return_to_status();
  839. }
  840. }
  841. void extr_unload_0()
  842. {
  843. change_extr(0);
  844. extr_unload();
  845. }
  846. void extr_unload_1()
  847. {
  848. change_extr(1);
  849. extr_unload();
  850. }
  851. void extr_unload_2()
  852. {
  853. change_extr(2);
  854. extr_unload();
  855. }
  856. void extr_unload_3()
  857. {
  858. change_extr(3);
  859. extr_unload();
  860. }
  861. void extr_unload_4()
  862. {
  863. change_extr(4);
  864. extr_unload();
  865. }
  866. bool mmu_check_version()
  867. {
  868. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  869. }
  870. void mmu_show_warning()
  871. {
  872. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  873. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  874. }
  875. void mmu_eject_filament(uint8_t filament, bool recover)
  876. {
  877. if (filament < 5)
  878. {
  879. if (degHotend0() > EXTRUDE_MINTEMP)
  880. {
  881. st_synchronize();
  882. lcd_update_enable(false);
  883. lcd_clear();
  884. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  885. current_position[E_AXIS] -= 80;
  886. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  887. st_synchronize();
  888. lcd_update_enable(true);
  889. mmu_command(MMU_CMD_E0 + filament);
  890. manage_response(false, false);
  891. if (recover)
  892. {
  893. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  894. mmu_command(MMU_CMD_R0);
  895. manage_response(false, false);
  896. }
  897. }
  898. else
  899. {
  900. lcd_clear();
  901. lcd_set_cursor(0, 0);
  902. lcd_puts_P(_T(MSG_ERROR));
  903. lcd_set_cursor(0, 2);
  904. lcd_puts_P(_T(MSG_PREHEAT_NOZZLE));
  905. delay(2000);
  906. lcd_clear();
  907. }
  908. }
  909. else
  910. {
  911. puts_P(PSTR("Filament nr out of range!"));
  912. }
  913. }