mmu.cpp 37 KB

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