mmu.cpp 38 KB

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