mmu.cpp 44 KB

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