mmu.cpp 43 KB

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