mmu.cpp 44 KB

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