mmu.cpp 39 KB

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