mmu.cpp 37 KB

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