mmu.cpp 41 KB

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