mmu.cpp 43 KB

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