mmu.cpp 38 KB

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