mmu.cpp 36 KB

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