mmu.cpp 47 KB

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