mmu.cpp 44 KB

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