mmu.cpp 34 KB

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