mmu.cpp 35 KB

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