mmu.cpp 39 KB

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