mmu.cpp 39 KB

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