mmu.cpp 46 KB

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