mmu.cpp 40 KB

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