mmu.cpp 39 KB

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