mmu.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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. mmu_idl_sens = 1;
  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. mmu_idl_sens = 1;
  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_idl_sens)
  318. {
  319. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0 && mmu_loading_flag)
  320. {
  321. #ifdef MMU_DEBUG
  322. printf_P(PSTR("MMU <= 'A'\n"));
  323. #endif //MMU_DEBUG
  324. mmu_puts_P(PSTR("A\n")); //send 'abort' request
  325. mmu_idl_sens = 0;
  326. //printf_P(PSTR("MMU IDLER_SENSOR = 0 - ABORT\n"));
  327. }
  328. //else
  329. //printf_P(PSTR("MMU IDLER_SENSOR = 1 - WAIT\n"));
  330. }
  331. if (mmu_rx_ok() > 0)
  332. {
  333. #ifdef MMU_DEBUG
  334. printf_P(PSTR("MMU => 'ok'\n"));
  335. #endif //MMU_DEBUG
  336. mmu_attempt_nr = 0;
  337. mmu_last_cmd = 0;
  338. mmu_ready = true;
  339. mmu_state = 1;
  340. }
  341. else if ((mmu_last_request + MMU_CMD_TIMEOUT) < millis())
  342. { //resend request after timeout (5 min)
  343. if (mmu_last_cmd)
  344. {
  345. if (mmu_attempt_nr++ < MMU_MAX_RESEND_ATTEMPTS) {
  346. #ifdef MMU_DEBUG
  347. printf_P(PSTR("MMU retry attempt nr. %d\n"), mmu_attempt_nr - 1);
  348. #endif //MMU_DEBUG
  349. mmu_cmd = mmu_last_cmd;
  350. }
  351. else {
  352. mmu_cmd = 0;
  353. mmu_last_cmd = 0; //check
  354. mmu_attempt_nr = 0;
  355. }
  356. }
  357. mmu_state = 1;
  358. }
  359. return;
  360. case 4:
  361. if (mmu_rx_ok() > 0)
  362. {
  363. fscanf_P(uart2io, PSTR("%d"), &mmu_power_failures); //scan power failures
  364. #ifdef MMU_DEBUG
  365. printf_P(PSTR("MMU => 'ok'\n"));
  366. #endif //MMU_DEBUG
  367. mmu_last_cmd = 0;
  368. mmu_ready = true;
  369. mmu_state = 1;
  370. }
  371. else if ((mmu_last_request + MMU_CMD_TIMEOUT) < millis())
  372. { //resend request after timeout (5 min)
  373. mmu_state = 1;
  374. }
  375. }
  376. }
  377. void mmu_reset(void)
  378. {
  379. #ifdef MMU_HWRESET //HW - pulse reset pin
  380. digitalWrite(MMU_RST_PIN, LOW);
  381. _delay_us(100);
  382. digitalWrite(MMU_RST_PIN, HIGH);
  383. #else //SW - send X0 command
  384. mmu_puts_P(PSTR("X0\n"));
  385. #endif
  386. }
  387. int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament)
  388. {
  389. printf_P(PSTR("MMU <= 'F%d %d'\n"), extruder, filament);
  390. mmu_printf_P(PSTR("F%d %d\n"), extruder, filament);
  391. unsigned char timeout = MMU_TIMEOUT; //10x100ms
  392. while ((mmu_rx_ok() <= 0) && (--timeout))
  393. delay_keep_alive(MMU_TODELAY);
  394. return timeout?1:0;
  395. }
  396. //! @brief Enqueue MMUv2 command
  397. //!
  398. //! Call manage_response() after enqueuing to process command.
  399. //! If T command is enqueued, it disables current for extruder motor if TMC2130 driver present.
  400. //! If T or L command is enqueued, it marks filament loaded in AutoDeplete module.
  401. void mmu_command(uint8_t cmd)
  402. {
  403. if ((cmd >= MMU_CMD_T0) && (cmd <= MMU_CMD_T4))
  404. {
  405. //disable extruder motor
  406. #ifdef TMC2130
  407. tmc2130_set_pwr(E_AXIS, 0);
  408. #endif //TMC2130
  409. //printf_P(PSTR("E-axis disabled\n"));
  410. ad_markLoaded(cmd - MMU_CMD_T0);
  411. }
  412. if ((cmd >= MMU_CMD_L0) && (cmd <= MMU_CMD_L4))
  413. {
  414. ad_markLoaded(cmd - MMU_CMD_L0);
  415. }
  416. mmu_cmd = cmd;
  417. mmu_ready = false;
  418. }
  419. void mmu_load_step() {
  420. current_position[E_AXIS] = current_position[E_AXIS] + MMU_LOAD_FEEDRATE * 0.1;
  421. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  422. st_synchronize();
  423. }
  424. //! @brief Is nozzle hot enough to move extruder wheels and do we have idler sensor?
  425. //!
  426. //! Do load steps only if temperature is higher then min. temp for safe extrusion and
  427. //! idler sensor present.
  428. //! Otherwise "cold extrusion prevented" would be send to serial line periodically
  429. //! and watchdog reset will be triggered by lack of keep_alive processing.
  430. //!
  431. //! @retval true temperature is high enough to move extruder
  432. //! @retval false temperature is not high enough to move extruder, turned
  433. //! off E-stepper to prevent over-heating and allow filament pull-out if necessary
  434. bool can_extrude()
  435. {
  436. if ((degHotend(active_extruder) < EXTRUDE_MINTEMP) || !mmu_idler_sensor_detected)
  437. {
  438. disable_e0();
  439. delay_keep_alive(100);
  440. return false;
  441. }
  442. return true;
  443. }
  444. bool mmu_get_response(uint8_t move)
  445. {
  446. mmu_loading_flag = false;
  447. printf_P(PSTR("mmu_get_response - begin move:%d\n"), move);
  448. KEEPALIVE_STATE(IN_PROCESS);
  449. while (mmu_cmd != 0)
  450. {
  451. delay_keep_alive(100);
  452. }
  453. while (!mmu_ready)
  454. {
  455. if ((mmu_state != 3) && (mmu_last_cmd == 0))
  456. break;
  457. switch (move) {
  458. case MMU_LOAD_MOVE:
  459. mmu_loading_flag = true;
  460. if (can_extrude()) mmu_load_step();
  461. //don't rely on "ok" signal from mmu unit; if filament detected by idler sensor during loading stop loading movements to prevent infinite loading
  462. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0) move = MMU_NO_MOVE;
  463. break;
  464. case MMU_UNLOAD_MOVE:
  465. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0) //filament is still detected by idler sensor, printer helps with unlading
  466. {
  467. if (can_extrude())
  468. {
  469. printf_P(PSTR("Unload 1\n"));
  470. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  471. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  472. st_synchronize();
  473. }
  474. }
  475. else //filament was unloaded from idler, no additional movements needed
  476. {
  477. printf_P(PSTR("Unloading finished 1\n"));
  478. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  479. move = MMU_NO_MOVE;
  480. }
  481. break;
  482. case MMU_TCODE_MOVE: //first do unload and then continue with infinite loading movements
  483. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0) //filament detected by idler sensor, we must unload first
  484. {
  485. if (can_extrude())
  486. {
  487. printf_P(PSTR("Unload 2\n"));
  488. current_position[E_AXIS] = current_position[E_AXIS] - MMU_LOAD_FEEDRATE * MMU_LOAD_TIME_MS*0.001;
  489. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], MMU_LOAD_FEEDRATE, active_extruder);
  490. st_synchronize();
  491. }
  492. }
  493. else //delay to allow mmu unit to pull out filament from bondtech gears and then start with infinite loading
  494. {
  495. printf_P(PSTR("Unloading finished 2\n"));
  496. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  497. delay_keep_alive(MMU_LOAD_TIME_MS);
  498. move = MMU_LOAD_MOVE;
  499. printf_P(PSTR("mmu_get_response - begin move:%d\n"), move);
  500. }
  501. break;
  502. case MMU_NO_MOVE:
  503. default:
  504. delay_keep_alive(100);
  505. break;
  506. }
  507. }
  508. printf_P(PSTR("mmu_get_response() returning: %d\n"), mmu_ready);
  509. bool ret = mmu_ready;
  510. mmu_ready = false;
  511. // printf_P(PSTR("mmu_get_response - end %d\n"), ret?1:0);
  512. return ret;
  513. }
  514. void manage_response(bool move_axes, bool turn_off_nozzle, uint8_t move)
  515. {
  516. bool response = false;
  517. mmu_print_saved = false;
  518. bool lcd_update_was_enabled = false;
  519. float hotend_temp_bckp = degTargetHotend(active_extruder);
  520. float z_position_bckp = current_position[Z_AXIS];
  521. float x_position_bckp = current_position[X_AXIS];
  522. float y_position_bckp = current_position[Y_AXIS];
  523. uint8_t screen = 0; //used for showing multiscreen messages
  524. while(!response)
  525. {
  526. response = mmu_get_response(move); //wait for "ok" from mmu
  527. if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit
  528. if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
  529. uint8_t mmu_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL);
  530. uint16_t mmu_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT);
  531. if(mmu_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_FAIL, mmu_fail + 1);
  532. if(mmu_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_FAIL_TOT, mmu_fail_tot + 1);
  533. if (lcd_update_enabled) {
  534. lcd_update_was_enabled = true;
  535. lcd_update_enable(false);
  536. }
  537. st_synchronize();
  538. mmu_print_saved = true;
  539. printf_P(PSTR("MMU not responding\n"));
  540. hotend_temp_bckp = degTargetHotend(active_extruder);
  541. if (move_axes) {
  542. z_position_bckp = current_position[Z_AXIS];
  543. x_position_bckp = current_position[X_AXIS];
  544. y_position_bckp = current_position[Y_AXIS];
  545. //lift z
  546. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  547. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  548. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  549. st_synchronize();
  550. //Move XY to side
  551. current_position[X_AXIS] = X_PAUSE_POS;
  552. current_position[Y_AXIS] = Y_PAUSE_POS;
  553. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  554. st_synchronize();
  555. }
  556. if (turn_off_nozzle) {
  557. //set nozzle target temperature to 0
  558. setAllTargetHotends(0);
  559. }
  560. disable_e0(); //turn off E-stepper to prevent overheating and alow filament pull-out if necessary
  561. }
  562. //first three lines are used for printing multiscreen message; last line contains measured and target nozzle temperature
  563. if (screen == 0) { //screen 0
  564. lcd_display_message_fullscreen_P(_i("MMU needs user attention."));
  565. screen++;
  566. }
  567. else { //screen 1
  568. if((degTargetHotend(active_extruder) == 0) && turn_off_nozzle) lcd_display_message_fullscreen_P(_i("Press the knob to resume nozzle temperature."));
  569. else lcd_display_message_fullscreen_P(_i("Fix the issue and then press button on MMU unit."));
  570. screen=0;
  571. }
  572. lcd_set_degree();
  573. //5 seconds delay
  574. for (uint8_t i = 0; i < 5; i++) {
  575. if (lcd_clicked()) {
  576. setTargetHotend(hotend_temp_bckp, active_extruder);
  577. /// mmu_cmd = mmu_last_cmd;
  578. break;
  579. }
  580. //Print the hotend temperature (9 chars total) and fill rest of the line with space
  581. lcd_set_cursor(0, 4); //line 4
  582. 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]);
  583. lcd_space(9 - chars);
  584. delay_keep_alive(1000);
  585. }
  586. }
  587. else if (mmu_print_saved) {
  588. printf_P(PSTR("MMU starts responding\n"));
  589. if (turn_off_nozzle)
  590. {
  591. lcd_clear();
  592. setTargetHotend(hotend_temp_bckp, active_extruder);
  593. if (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
  594. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature..."));
  595. delay_keep_alive(3000);
  596. }
  597. while ((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)
  598. {
  599. delay_keep_alive(1000);
  600. lcd_wait_for_heater();
  601. }
  602. }
  603. if (move_axes) {
  604. lcd_clear();
  605. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming position..."));
  606. current_position[X_AXIS] = x_position_bckp;
  607. current_position[Y_AXIS] = y_position_bckp;
  608. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  609. st_synchronize();
  610. current_position[Z_AXIS] = z_position_bckp;
  611. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  612. st_synchronize();
  613. }
  614. else {
  615. lcd_clear();
  616. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming..."));
  617. delay_keep_alive(1000); //delay just for showing MMU OK message for a while in case that there are no xyz movements
  618. }
  619. }
  620. }
  621. if (lcd_update_was_enabled) lcd_update_enable(true);
  622. #ifdef TMC2130
  623. //enable extruder motor (disabled in mmu_command, start of T-code processing)
  624. tmc2130_set_pwr(E_AXIS, 1);
  625. //printf_P(PSTR("E-axis enabled\n"));
  626. #endif //TMC2130
  627. }
  628. //! @brief load filament to nozzle of multimaterial printer
  629. //!
  630. //! This function is used only only after T? (user select filament) and M600 (change filament).
  631. //! It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
  632. //! filament to nozzle.
  633. //!
  634. void mmu_load_to_nozzle()
  635. {
  636. st_synchronize();
  637. bool saved_e_relative_mode = axis_relative_modes[E_AXIS];
  638. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = true;
  639. if (mmu_idler_sensor_detected)
  640. {
  641. current_position[E_AXIS] += 3.0f;
  642. }
  643. else
  644. {
  645. current_position[E_AXIS] += 7.2f;
  646. }
  647. float feedrate = 562;
  648. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  649. st_synchronize();
  650. current_position[E_AXIS] += 14.4f;
  651. feedrate = 871;
  652. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  653. st_synchronize();
  654. current_position[E_AXIS] += 36.0f;
  655. feedrate = 1393;
  656. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  657. st_synchronize();
  658. current_position[E_AXIS] += 14.4f;
  659. feedrate = 871;
  660. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
  661. st_synchronize();
  662. if (!saved_e_relative_mode) axis_relative_modes[E_AXIS] = false;
  663. }
  664. void mmu_M600_wait_and_beep() {
  665. //Beep and wait for user to remove old filament and prepare new filament for load
  666. KEEPALIVE_STATE(PAUSED_FOR_USER);
  667. int counterBeep = 0;
  668. lcd_display_message_fullscreen_P(_i("Remove old filament and press the knob to start loading new filament."));
  669. bool bFirst=true;
  670. while (!lcd_clicked()){
  671. manage_heater();
  672. manage_inactivity(true);
  673. #if BEEPER > 0
  674. if (counterBeep == 500) {
  675. counterBeep = 0;
  676. }
  677. SET_OUTPUT(BEEPER);
  678. if (counterBeep == 0) {
  679. if((eSoundMode==e_SOUND_MODE_LOUD)||((eSoundMode==e_SOUND_MODE_ONCE)&&bFirst))
  680. {
  681. bFirst=false;
  682. WRITE(BEEPER, HIGH);
  683. }
  684. }
  685. if (counterBeep == 20) {
  686. WRITE(BEEPER, LOW);
  687. }
  688. counterBeep++;
  689. #endif //BEEPER > 0
  690. delay_keep_alive(4);
  691. }
  692. WRITE(BEEPER, LOW);
  693. }
  694. void mmu_M600_load_filament(bool automatic)
  695. {
  696. //load filament for mmu v2
  697. tmp_extruder = mmu_extruder;
  698. if (!automatic) {
  699. #ifdef MMU_M600_SWITCH_EXTRUDER
  700. bool yes = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Do you want to switch extruder?"), false);
  701. if(yes) tmp_extruder = choose_extruder_menu();
  702. #endif //MMU_M600_SWITCH_EXTRUDER
  703. }
  704. else {
  705. tmp_extruder = ad_getAlternative(tmp_extruder);
  706. }
  707. lcd_update_enable(false);
  708. lcd_clear();
  709. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  710. lcd_print(" ");
  711. lcd_print(tmp_extruder + 1);
  712. snmm_filaments_used |= (1 << tmp_extruder); //for stop print
  713. // printf_P(PSTR("T code: %d \n"), tmp_extruder);
  714. // mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
  715. mmu_command(MMU_CMD_T0 + tmp_extruder);
  716. manage_response(false, true, MMU_LOAD_MOVE);
  717. mmu_continue_loading();
  718. mmu_extruder = tmp_extruder; //filament change is finished
  719. mmu_load_to_nozzle();
  720. load_filament_final_feed();
  721. st_synchronize();
  722. }
  723. #ifdef SNMM
  724. void extr_mov(float shift, float feed_rate)
  725. { //move extruder no matter what the current heater temperature is
  726. set_extrude_min_temp(.0);
  727. current_position[E_AXIS] += shift;
  728. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feed_rate, active_extruder);
  729. set_extrude_min_temp(EXTRUDE_MINTEMP);
  730. }
  731. #endif //SNMM
  732. void change_extr(int
  733. #ifdef SNMM
  734. extr
  735. #endif //SNMM
  736. ) { //switches multiplexer for extruders
  737. #ifdef SNMM
  738. st_synchronize();
  739. delay(100);
  740. disable_e0();
  741. disable_e1();
  742. disable_e2();
  743. mmu_extruder = extr;
  744. pinMode(E_MUX0_PIN, OUTPUT);
  745. pinMode(E_MUX1_PIN, OUTPUT);
  746. switch (extr) {
  747. case 1:
  748. WRITE(E_MUX0_PIN, HIGH);
  749. WRITE(E_MUX1_PIN, LOW);
  750. break;
  751. case 2:
  752. WRITE(E_MUX0_PIN, LOW);
  753. WRITE(E_MUX1_PIN, HIGH);
  754. break;
  755. case 3:
  756. WRITE(E_MUX0_PIN, HIGH);
  757. WRITE(E_MUX1_PIN, HIGH);
  758. break;
  759. default:
  760. WRITE(E_MUX0_PIN, LOW);
  761. WRITE(E_MUX1_PIN, LOW);
  762. break;
  763. }
  764. delay(100);
  765. #endif
  766. }
  767. int get_ext_nr()
  768. { //reads multiplexer input pins and return current extruder number (counted from 0)
  769. #ifndef SNMM
  770. return(mmu_extruder); //update needed
  771. #else
  772. return(2 * READ(E_MUX1_PIN) + READ(E_MUX0_PIN));
  773. #endif
  774. }
  775. void display_loading()
  776. {
  777. switch (mmu_extruder)
  778. {
  779. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  780. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  781. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  782. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  783. }
  784. }
  785. void extr_adj(int extruder) //loading filament for SNMM
  786. {
  787. #ifndef SNMM
  788. uint8_t cmd = MMU_CMD_L0 + extruder;
  789. if (cmd > MMU_CMD_L4)
  790. {
  791. printf_P(PSTR("Filament out of range %d \n"),extruder);
  792. return;
  793. }
  794. mmu_command(cmd);
  795. //show which filament is currently loaded
  796. lcd_update_enable(false);
  797. lcd_clear();
  798. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  799. //if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd.setCursor(0, 1);
  800. //else lcd.print(" ");
  801. lcd_print(" ");
  802. lcd_print(extruder + 1);
  803. // get response
  804. manage_response(false, false);
  805. lcd_update_enable(true);
  806. //lcd_return_to_status();
  807. #else
  808. bool correct;
  809. max_feedrate[E_AXIS] =80;
  810. //max_feedrate[E_AXIS] = 50;
  811. START:
  812. lcd_clear();
  813. lcd_set_cursor(0, 0);
  814. switch (extruder) {
  815. case 1: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T1)); break;
  816. case 2: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T2)); break;
  817. case 3: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T3)); break;
  818. default: lcd_display_message_fullscreen_P(_T(MSG_FILAMENT_LOADING_T0)); break;
  819. }
  820. KEEPALIVE_STATE(PAUSED_FOR_USER);
  821. do{
  822. extr_mov(0.001,1000);
  823. delay_keep_alive(2);
  824. } while (!lcd_clicked());
  825. //delay_keep_alive(500);
  826. KEEPALIVE_STATE(IN_HANDLER);
  827. st_synchronize();
  828. //correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
  829. //if (!correct) goto START;
  830. //extr_mov(BOWDEN_LENGTH/2.f, 500); //dividing by 2 is there because of max. extrusion length limitation (x_max + y_max)
  831. //extr_mov(BOWDEN_LENGTH/2.f, 500);
  832. extr_mov(bowden_length[extruder], 500);
  833. lcd_clear();
  834. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  835. if(strlen(_T(MSG_LOADING_FILAMENT))>18) lcd_set_cursor(0, 1);
  836. else lcd_print(" ");
  837. lcd_print(mmu_extruder + 1);
  838. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  839. st_synchronize();
  840. max_feedrate[E_AXIS] = 50;
  841. lcd_update_enable(true);
  842. lcd_return_to_status();
  843. lcdDrawUpdate = 2;
  844. #endif
  845. }
  846. struct E_step
  847. {
  848. float extrude; //!< extrude distance in mm
  849. float feed_rate; //!< feed rate in mm/s
  850. };
  851. static const E_step ramming_sequence[] PROGMEM =
  852. {
  853. {1.0, 1000.0/60},
  854. {1.0, 1500.0/60},
  855. {2.0, 2000.0/60},
  856. {1.5, 3000.0/60},
  857. {2.5, 4000.0/60},
  858. {-15.0, 5000.0/60},
  859. {-14.0, 1200.0/60},
  860. {-6.0, 600.0/60},
  861. {10.0, 700.0/60},
  862. {-10.0, 400.0/60},
  863. {-50.0, 2000.0/60},
  864. };
  865. //! @brief Unload sequence to optimize shape of the tip of the unloaded filament
  866. static void filament_ramming()
  867. {
  868. for(uint8_t i = 0; i < (sizeof(ramming_sequence)/sizeof(E_step));++i)
  869. {
  870. current_position[E_AXIS] += pgm_read_float(&(ramming_sequence[i].extrude));
  871. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  872. current_position[E_AXIS], pgm_read_float(&(ramming_sequence[i].feed_rate)), active_extruder);
  873. st_synchronize();
  874. }
  875. }
  876. void extr_unload()
  877. { //unload just current filament for multimaterial printers
  878. #ifdef SNMM
  879. float tmp_motor[3] = DEFAULT_PWM_MOTOR_CURRENT;
  880. float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  881. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  882. #endif
  883. if (degHotend0() > EXTRUDE_MINTEMP)
  884. {
  885. #ifndef SNMM
  886. st_synchronize();
  887. //show which filament is currently unloaded
  888. lcd_update_enable(false);
  889. lcd_clear();
  890. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  891. lcd_print(" ");
  892. if (mmu_extruder == MMU_FILAMENT_UNKNOWN) lcd_print(" ");
  893. else lcd_print(mmu_extruder + 1);
  894. filament_ramming();
  895. mmu_command(MMU_CMD_U0);
  896. // get response
  897. manage_response(false, true, MMU_UNLOAD_MOVE);
  898. lcd_update_enable(true);
  899. #else //SNMM
  900. lcd_clear();
  901. lcd_display_message_fullscreen_P(PSTR(""));
  902. max_feedrate[E_AXIS] = 50;
  903. lcd_set_cursor(0, 0); lcd_puts_P(_T(MSG_UNLOADING_FILAMENT));
  904. lcd_print(" ");
  905. lcd_print(mmu_extruder + 1);
  906. lcd_set_cursor(0, 2); lcd_puts_P(_T(MSG_PLEASE_WAIT));
  907. if (current_position[Z_AXIS] < 15) {
  908. current_position[Z_AXIS] += 15; //lifting in Z direction to make space for extrusion
  909. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 25, active_extruder);
  910. }
  911. current_position[E_AXIS] += 10; //extrusion
  912. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 10, active_extruder);
  913. st_current_set(2, E_MOTOR_HIGH_CURRENT);
  914. if (current_temperature[0] < 230) { //PLA & all other filaments
  915. current_position[E_AXIS] += 5.4;
  916. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2800 / 60, active_extruder);
  917. current_position[E_AXIS] += 3.2;
  918. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  919. current_position[E_AXIS] += 3;
  920. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3400 / 60, active_extruder);
  921. }
  922. else { //ABS
  923. current_position[E_AXIS] += 3.1;
  924. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2000 / 60, active_extruder);
  925. current_position[E_AXIS] += 3.1;
  926. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  927. current_position[E_AXIS] += 4;
  928. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
  929. /*current_position[X_AXIS] += 23; //delay
  930. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay
  931. current_position[X_AXIS] -= 23; //delay
  932. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 600 / 60, active_extruder); //delay*/
  933. delay_keep_alive(4700);
  934. }
  935. max_feedrate[E_AXIS] = 80;
  936. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  937. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  938. current_position[E_AXIS] -= (bowden_length[mmu_extruder] + 60 + FIL_LOAD_LENGTH) / 2;
  939. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 500, active_extruder);
  940. st_synchronize();
  941. //st_current_init();
  942. if (SilentMode != SILENT_MODE_OFF) st_current_set(2, tmp_motor[2]); //set back to normal operation currents
  943. else st_current_set(2, tmp_motor_loud[2]);
  944. lcd_update_enable(true);
  945. lcd_return_to_status();
  946. max_feedrate[E_AXIS] = 50;
  947. #endif //SNMM
  948. }
  949. else
  950. {
  951. show_preheat_nozzle_warning();
  952. }
  953. //lcd_return_to_status();
  954. }
  955. //wrapper functions for loading filament
  956. void extr_adj_0()
  957. {
  958. #ifndef SNMM
  959. enquecommand_P(PSTR("M701 E0"));
  960. #else
  961. change_extr(0);
  962. extr_adj(0);
  963. #endif
  964. }
  965. void extr_adj_1()
  966. {
  967. #ifndef SNMM
  968. enquecommand_P(PSTR("M701 E1"));
  969. #else
  970. change_extr(1);
  971. extr_adj(1);
  972. #endif
  973. }
  974. void extr_adj_2()
  975. {
  976. #ifndef SNMM
  977. enquecommand_P(PSTR("M701 E2"));
  978. #else
  979. change_extr(2);
  980. extr_adj(2);
  981. #endif
  982. }
  983. void extr_adj_3()
  984. {
  985. #ifndef SNMM
  986. enquecommand_P(PSTR("M701 E3"));
  987. #else
  988. change_extr(3);
  989. extr_adj(3);
  990. #endif
  991. }
  992. void extr_adj_4()
  993. {
  994. #ifndef SNMM
  995. enquecommand_P(PSTR("M701 E4"));
  996. #else
  997. change_extr(4);
  998. extr_adj(4);
  999. #endif
  1000. }
  1001. void mmu_load_to_nozzle_0()
  1002. {
  1003. lcd_mmu_load_to_nozzle(0);
  1004. }
  1005. void mmu_load_to_nozzle_1()
  1006. {
  1007. lcd_mmu_load_to_nozzle(1);
  1008. }
  1009. void mmu_load_to_nozzle_2()
  1010. {
  1011. lcd_mmu_load_to_nozzle(2);
  1012. }
  1013. void mmu_load_to_nozzle_3()
  1014. {
  1015. lcd_mmu_load_to_nozzle(3);
  1016. }
  1017. void mmu_load_to_nozzle_4()
  1018. {
  1019. lcd_mmu_load_to_nozzle(4);
  1020. }
  1021. void mmu_eject_fil_0()
  1022. {
  1023. mmu_eject_filament(0, true);
  1024. }
  1025. void mmu_eject_fil_1()
  1026. {
  1027. mmu_eject_filament(1, true);
  1028. }
  1029. void mmu_eject_fil_2()
  1030. {
  1031. mmu_eject_filament(2, true);
  1032. }
  1033. void mmu_eject_fil_3()
  1034. {
  1035. mmu_eject_filament(3, true);
  1036. }
  1037. void mmu_eject_fil_4()
  1038. {
  1039. mmu_eject_filament(4, true);
  1040. }
  1041. void load_all()
  1042. {
  1043. #ifndef SNMM
  1044. enquecommand_P(PSTR("M701 E0"));
  1045. enquecommand_P(PSTR("M701 E1"));
  1046. enquecommand_P(PSTR("M701 E2"));
  1047. enquecommand_P(PSTR("M701 E3"));
  1048. enquecommand_P(PSTR("M701 E4"));
  1049. #else
  1050. for (int i = 0; i < 4; i++)
  1051. {
  1052. change_extr(i);
  1053. extr_adj(i);
  1054. }
  1055. #endif
  1056. }
  1057. //wrapper functions for changing extruders
  1058. void extr_change_0()
  1059. {
  1060. change_extr(0);
  1061. lcd_return_to_status();
  1062. }
  1063. void extr_change_1()
  1064. {
  1065. change_extr(1);
  1066. lcd_return_to_status();
  1067. }
  1068. void extr_change_2()
  1069. {
  1070. change_extr(2);
  1071. lcd_return_to_status();
  1072. }
  1073. void extr_change_3()
  1074. {
  1075. change_extr(3);
  1076. lcd_return_to_status();
  1077. }
  1078. #ifdef SNMM
  1079. //wrapper functions for unloading filament
  1080. void extr_unload_all()
  1081. {
  1082. if (degHotend0() > EXTRUDE_MINTEMP)
  1083. {
  1084. for (int i = 0; i < 4; i++)
  1085. {
  1086. change_extr(i);
  1087. extr_unload();
  1088. }
  1089. }
  1090. else
  1091. {
  1092. show_preheat_nozzle_warning();
  1093. lcd_return_to_status();
  1094. }
  1095. }
  1096. //unloading just used filament (for snmm)
  1097. void extr_unload_used()
  1098. {
  1099. if (degHotend0() > EXTRUDE_MINTEMP) {
  1100. for (int i = 0; i < 4; i++) {
  1101. if (snmm_filaments_used & (1 << i)) {
  1102. change_extr(i);
  1103. extr_unload();
  1104. }
  1105. }
  1106. snmm_filaments_used = 0;
  1107. }
  1108. else {
  1109. show_preheat_nozzle_warning();
  1110. lcd_return_to_status();
  1111. }
  1112. }
  1113. #endif //SNMM
  1114. void extr_unload_0()
  1115. {
  1116. change_extr(0);
  1117. extr_unload();
  1118. }
  1119. void extr_unload_1()
  1120. {
  1121. change_extr(1);
  1122. extr_unload();
  1123. }
  1124. void extr_unload_2()
  1125. {
  1126. change_extr(2);
  1127. extr_unload();
  1128. }
  1129. void extr_unload_3()
  1130. {
  1131. change_extr(3);
  1132. extr_unload();
  1133. }
  1134. void extr_unload_4()
  1135. {
  1136. change_extr(4);
  1137. extr_unload();
  1138. }
  1139. bool mmu_check_version()
  1140. {
  1141. return (mmu_buildnr >= MMU_REQUIRED_FW_BUILDNR);
  1142. }
  1143. void mmu_show_warning()
  1144. {
  1145. printf_P(PSTR("MMU2 firmware version invalid. Required version: build number %d or higher."), MMU_REQUIRED_FW_BUILDNR);
  1146. kill(_i("Please update firmware in your MMU2. Waiting for reset."));
  1147. }
  1148. void lcd_mmu_load_to_nozzle(uint8_t filament_nr)
  1149. {
  1150. if (degHotend0() > EXTRUDE_MINTEMP)
  1151. {
  1152. tmp_extruder = filament_nr;
  1153. lcd_update_enable(false);
  1154. lcd_clear();
  1155. lcd_set_cursor(0, 1); lcd_puts_P(_T(MSG_LOADING_FILAMENT));
  1156. lcd_print(" ");
  1157. lcd_print(tmp_extruder + 1);
  1158. mmu_command(MMU_CMD_T0 + tmp_extruder);
  1159. manage_response(true, true, MMU_TCODE_MOVE);
  1160. mmu_continue_loading();
  1161. mmu_extruder = tmp_extruder; //filament change is finished
  1162. mmu_load_to_nozzle();
  1163. load_filament_final_feed();
  1164. st_synchronize();
  1165. custom_message_type = CUSTOM_MSG_TYPE_F_LOAD;
  1166. lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
  1167. lcd_return_to_status();
  1168. lcd_update_enable(true);
  1169. lcd_load_filament_color_check();
  1170. lcd_setstatuspgm(_T(WELCOME_MSG));
  1171. custom_message_type = CUSTOM_MSG_TYPE_STATUS;
  1172. }
  1173. else
  1174. {
  1175. show_preheat_nozzle_warning();
  1176. }
  1177. }
  1178. void mmu_eject_filament(uint8_t filament, bool recover)
  1179. {
  1180. if (filament < 5)
  1181. {
  1182. if (degHotend0() > EXTRUDE_MINTEMP)
  1183. {
  1184. st_synchronize();
  1185. {
  1186. LcdUpdateDisabler disableLcdUpdate;
  1187. lcd_clear();
  1188. lcd_set_cursor(0, 1); lcd_puts_P(_i("Ejecting filament"));
  1189. current_position[E_AXIS] -= 80;
  1190. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 2500 / 60, active_extruder);
  1191. st_synchronize();
  1192. mmu_command(MMU_CMD_E0 + filament);
  1193. manage_response(false, false, MMU_UNLOAD_MOVE);
  1194. if (recover)
  1195. {
  1196. lcd_show_fullscreen_message_and_wait_P(_i("Please remove filament and then press the knob."));
  1197. mmu_command(MMU_CMD_R0);
  1198. manage_response(false, false);
  1199. }
  1200. }
  1201. }
  1202. else
  1203. {
  1204. show_preheat_nozzle_warning();
  1205. }
  1206. }
  1207. else
  1208. {
  1209. puts_P(PSTR("Filament nr out of range!"));
  1210. }
  1211. }
  1212. void mmu_continue_loading()
  1213. {
  1214. if (mmu_idler_sensor_detected) {
  1215. for (uint8_t i = 0; i < MMU_IDLER_SENSOR_ATTEMPTS_NR; i++) {
  1216. if (PIN_GET(MMU_IDLER_SENSOR_PIN) == 0) return;
  1217. #ifdef MMU_DEBUG
  1218. printf_P(PSTR("Additional load attempt nr. %d\n"), i);
  1219. #endif // MMU_DEBUG
  1220. mmu_command(MMU_CMD_C0);
  1221. manage_response(true, true, MMU_LOAD_MOVE);
  1222. }
  1223. if (PIN_GET(MMU_IDLER_SENSOR_PIN) != 0) {
  1224. uint8_t mmu_load_fail = eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL);
  1225. uint16_t mmu_load_fail_tot = eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT);
  1226. if(mmu_load_fail < 255) eeprom_update_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, mmu_load_fail + 1);
  1227. if(mmu_load_fail_tot < 65535) eeprom_update_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, mmu_load_fail_tot + 1);
  1228. char cmd[3];
  1229. //pause print, show error message and then repeat last T-code
  1230. stop_and_save_print_to_ram(0, 0);
  1231. //lift z
  1232. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  1233. if (current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
  1234. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 15, active_extruder);
  1235. st_synchronize();
  1236. //Move XY to side
  1237. current_position[X_AXIS] = X_PAUSE_POS;
  1238. current_position[Y_AXIS] = Y_PAUSE_POS;
  1239. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 50, active_extruder);
  1240. st_synchronize();
  1241. //set nozzle target temperature to 0
  1242. setAllTargetHotends(0);
  1243. lcd_setstatuspgm(_i("MMU load failed "));////MSG_RECOVERING_PRINT c=20 r=1
  1244. mmu_fil_loaded = false; //so we can retry same T-code again
  1245. isPrintPaused = true;
  1246. }
  1247. }
  1248. else { //mmu_idler_sensor_detected == false
  1249. mmu_command(MMU_CMD_C0);
  1250. }
  1251. }