mmu.cpp 41 KB

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