mmu.cpp 27 KB

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