mmu2.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. #include "mmu2.h"
  2. #include "mmu2_error_converter.h"
  3. #include "mmu2_fsensor.h"
  4. #include "mmu2_log.h"
  5. #include "mmu2_power.h"
  6. #include "mmu2_progress_converter.h"
  7. #include "mmu2_reporting.h"
  8. #include "Marlin.h"
  9. #include "language.h"
  10. #include "messages.h"
  11. #include "sound.h"
  12. #include "stepper.h"
  13. #include "strlen_cx.h"
  14. #include "temperature.h"
  15. #include "ultralcd.h"
  16. // Settings for filament load / unload from the LCD menu.
  17. // This is for Prusa MK3-style extruders. Customize for your hardware.
  18. #define MMU2_FILAMENTCHANGE_EJECT_FEED 80.0
  19. #define MMU2_LOAD_TO_NOZZLE_SEQUENCE \
  20. { 7.2, 562 }, \
  21. { 14.4, 871 }, \
  22. { 36.0, 1393 }, \
  23. { 14.4, 871 }, \
  24. { 50.0, 198 }
  25. #define NOZZLE_PARK_XY_FEEDRATE 50
  26. #define NOZZLE_PARK_Z_FEEDRATE 15
  27. // Nominal distance from the extruder gear to the nozzle tip is 87mm
  28. // However, some slipping may occur and we need separate distances for
  29. // LoadToNozzle and ToolChange.
  30. // - +5mm seemed good for LoadToNozzle,
  31. // - but too much (made blobs) for a ToolChange
  32. static constexpr float MMU2_LOAD_TO_NOZZLE_LENGTH = 87.0F + 5.0F;
  33. // As discussed with our PrusaSlicer profile specialist
  34. // - ToolChange shall not try to push filament into the very tip of the nozzle
  35. // to have some space for additional G-code to tune the extruded filament length
  36. // in the profile
  37. static constexpr float MMU2_TOOL_CHANGE_LOAD_LENGTH = 30.0F;
  38. static constexpr float MMU2_LOAD_TO_NOZZLE_FEED_RATE = 20.0F;
  39. static constexpr uint8_t MMU2_NO_TOOL = 99;
  40. static constexpr uint32_t MMU_BAUD = 115200;
  41. struct E_Step {
  42. float extrude; ///< extrude distance in mm
  43. float feedRate; ///< feed rate in mm/s
  44. };
  45. static constexpr E_Step ramming_sequence[] PROGMEM = {
  46. { 1.0F, 1000.0F / 60.F},
  47. { 1.0F, 1500.0F / 60.F},
  48. { 2.0F, 2000.0F / 60.F},
  49. { 1.5F, 3000.0F / 60.F},
  50. { 2.5F, 4000.0F / 60.F},
  51. {-15.0F, 5000.0F / 60.F},
  52. {-14.0F, 1200.0F / 60.F},
  53. {-6.0F, 600.0F / 60.F},
  54. { 10.0F, 700.0F / 60.F},
  55. {-10.0F, 400.0F / 60.F},
  56. {-50.0F, 2000.0F / 60.F},
  57. };
  58. static constexpr E_Step load_to_nozzle_sequence[] PROGMEM = { MMU2_LOAD_TO_NOZZLE_SEQUENCE };
  59. namespace MMU2 {
  60. void execute_extruder_sequence(const E_Step *sequence, int steps);
  61. template<typename F>
  62. void waitForHotendTargetTemp(uint16_t delay, F f){
  63. while (((degTargetHotend(active_extruder) - degHotend(active_extruder)) > 5)) {
  64. f();
  65. delay_keep_alive(delay);
  66. }
  67. }
  68. void WaitForHotendTargetTempBeep(){
  69. waitForHotendTargetTemp(3000, []{ Sound_MakeSound(e_SOUND_TYPE_StandardPrompt); } );
  70. }
  71. MMU2 mmu2;
  72. MMU2::MMU2()
  73. : logic(&mmu2Serial)
  74. , extruder(MMU2_NO_TOOL)
  75. , resume_position()
  76. , resume_hotend_temp(0)
  77. , logicStepLastStatus(StepStatus::Finished)
  78. , state(xState::Stopped)
  79. , mmu_print_saved(false)
  80. , loadFilamentStarted(false)
  81. , loadingToNozzle(false)
  82. {
  83. }
  84. void MMU2::Start() {
  85. #ifdef MMU_HWRESET
  86. WRITE(MMU_RST_PIN, 1);
  87. SET_OUTPUT(MMU_RST_PIN); // setup reset pin
  88. #endif //MMU_HWRESET
  89. mmu2Serial.begin(MMU_BAUD);
  90. PowerOn();
  91. mmu2Serial.flush(); // make sure the UART buffer is clear before starting communication
  92. extruder = MMU2_NO_TOOL;
  93. state = xState::Connecting;
  94. // start the communication
  95. logic.Start();
  96. }
  97. void MMU2::Stop() {
  98. StopKeepPowered();
  99. PowerOff();
  100. }
  101. void MMU2::StopKeepPowered(){
  102. state = xState::Stopped;
  103. logic.Stop();
  104. mmu2Serial.close();
  105. }
  106. void MMU2::Reset(ResetForm level){
  107. switch (level) {
  108. case Software: ResetX0(); break;
  109. case ResetPin: TriggerResetPin(); break;
  110. case CutThePower: PowerCycle(); break;
  111. default: break;
  112. }
  113. }
  114. void MMU2::ResetX0() {
  115. logic.ResetMMU(); // Send soft reset
  116. }
  117. void MMU2::TriggerResetPin(){
  118. reset();
  119. }
  120. void MMU2::PowerCycle(){
  121. // cut the power to the MMU and after a while restore it
  122. // Sadly, MK3/S/+ cannot do this
  123. PowerOff();
  124. delay_keep_alive(1000);
  125. PowerOn();
  126. }
  127. void MMU2::PowerOff(){
  128. power_off();
  129. }
  130. void MMU2::PowerOn(){
  131. power_on();
  132. }
  133. void MMU2::mmu_loop() {
  134. // We only leave this method if the current command was successfully completed - that's the Marlin's way of blocking operation
  135. // Atomic compare_exchange would have been the most appropriate solution here, but this gets called only in Marlin's task,
  136. // so thread safety should be kept
  137. static bool avoidRecursion = false;
  138. if (avoidRecursion)
  139. return;
  140. avoidRecursion = true;
  141. logicStepLastStatus = LogicStep(); // it looks like the mmu_loop doesn't need to be a blocking call
  142. avoidRecursion = false;
  143. }
  144. struct ReportingRAII {
  145. CommandInProgress cip;
  146. inline ReportingRAII(CommandInProgress cip):cip(cip){
  147. BeginReport(cip, (uint16_t)ProgressCode::EngagingIdler);
  148. }
  149. inline ~ReportingRAII(){
  150. EndReport(cip, (uint16_t)ProgressCode::OK);
  151. }
  152. };
  153. bool MMU2::WaitForMMUReady(){
  154. switch(State()){
  155. case xState::Stopped:
  156. return false;
  157. case xState::Connecting:
  158. // shall we wait until the MMU reconnects?
  159. // fire-up a fsm_dlg and show "MMU not responding"?
  160. default:
  161. return true;
  162. }
  163. }
  164. bool MMU2::tool_change(uint8_t index) {
  165. if( ! WaitForMMUReady())
  166. return false;
  167. if (index != extruder) {
  168. ReportingRAII rep(CommandInProgress::ToolChange);
  169. BlockRunoutRAII blockRunout;
  170. st_synchronize();
  171. logic.ToolChange(index); // let the MMU pull the filament out and push a new one in
  172. manage_response(false, false); // true, true);
  173. // reset current position to whatever the planner thinks it is
  174. plan_set_e_position(current_position[E_AXIS]);
  175. extruder = index; //filament change is finished
  176. SetActiveExtruder(0);
  177. // @@TODO really report onto the serial? May be for the Octoprint? Not important now
  178. // SERIAL_ECHO_START();
  179. // SERIAL_ECHOLNPAIR(MSG_ACTIVE_EXTRUDER, int(extruder));
  180. }
  181. return true;
  182. }
  183. /// Handle special T?/Tx/Tc commands
  184. ///
  185. ///- T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
  186. ///- Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load.
  187. ///- Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
  188. bool MMU2::tool_change(char code, uint8_t slot) {
  189. if( ! WaitForMMUReady())
  190. return false;
  191. BlockRunoutRAII blockRunout;
  192. switch (code) {
  193. case '?': {
  194. waitForHotendTargetTemp(100, []{});
  195. load_filament_to_nozzle(slot);
  196. } break;
  197. case 'x': {
  198. st_synchronize();
  199. logic.ToolChange(slot);
  200. manage_response(false, false);
  201. extruder = slot;
  202. SetActiveExtruder(0);
  203. } break;
  204. case 'c': {
  205. waitForHotendTargetTemp(100, []{});
  206. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, sizeof(load_to_nozzle_sequence) / sizeof (load_to_nozzle_sequence[0]));
  207. } break;
  208. }
  209. return true;
  210. }
  211. uint8_t MMU2::get_current_tool() const {
  212. return extruder == MMU2_NO_TOOL ? -1 : extruder;
  213. }
  214. bool MMU2::set_filament_type(uint8_t index, uint8_t type) {
  215. if( ! WaitForMMUReady())
  216. return false;
  217. // @@TODO - this is not supported in the new MMU yet
  218. // cmd_arg = filamentType;
  219. // command(MMU_CMD_F0 + index);
  220. manage_response(false, false); // true, true);
  221. return true;
  222. }
  223. bool MMU2::unload() {
  224. if( ! WaitForMMUReady())
  225. return false;
  226. WaitForHotendTargetTempBeep();
  227. {
  228. ReportingRAII rep(CommandInProgress::UnloadFilament);
  229. filament_ramming();
  230. logic.UnloadFilament();
  231. manage_response(false, false); // false, true);
  232. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  233. // no active tool
  234. extruder = MMU2_NO_TOOL;
  235. }
  236. return true;
  237. }
  238. bool MMU2::cut_filament(uint8_t index){
  239. if( ! WaitForMMUReady())
  240. return false;
  241. ReportingRAII rep(CommandInProgress::CutFilament);
  242. logic.CutFilament(index);
  243. manage_response(false, false); // false, true);
  244. return true;
  245. }
  246. bool MMU2::load_filament(uint8_t index) {
  247. if( ! WaitForMMUReady())
  248. return false;
  249. ReportingRAII rep(CommandInProgress::LoadFilament);
  250. logic.LoadFilament(index);
  251. manage_response(false, false);
  252. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  253. return true;
  254. }
  255. struct LoadingToNozzleRAII {
  256. MMU2 &mmu2;
  257. explicit inline LoadingToNozzleRAII(MMU2 &mmu2):mmu2(mmu2){
  258. mmu2.loadingToNozzle = true;
  259. }
  260. inline ~LoadingToNozzleRAII(){
  261. mmu2.loadingToNozzle = false;
  262. }
  263. };
  264. bool MMU2::load_filament_to_nozzle(uint8_t index) {
  265. if( ! WaitForMMUReady())
  266. return false;
  267. LoadingToNozzleRAII ln(*this);
  268. WaitForHotendTargetTempBeep();
  269. {
  270. // used for MMU-menu operation "Load to Nozzle"
  271. ReportingRAII rep(CommandInProgress::ToolChange);
  272. BlockRunoutRAII blockRunout;
  273. if( extruder != MMU2_NO_TOOL ){ // we already have some filament loaded - free it + shape its tip properly
  274. filament_ramming();
  275. }
  276. logic.ToolChange(index);
  277. manage_response(false, false); // true, true);
  278. // reset current position to whatever the planner thinks it is
  279. plan_set_e_position(current_position[E_AXIS]);
  280. extruder = index;
  281. SetActiveExtruder(0);
  282. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  283. return true;
  284. }
  285. }
  286. bool MMU2::eject_filament(uint8_t index, bool recover) {
  287. if( ! WaitForMMUReady())
  288. return false;
  289. WaitForHotendTargetTempBeep();
  290. ReportingRAII rep(CommandInProgress::EjectFilament);
  291. current_position[E_AXIS] -= MMU2_FILAMENTCHANGE_EJECT_FEED;
  292. plan_buffer_line_curposXYZE(2500.F / 60.F);
  293. st_synchronize();
  294. logic.EjectFilament(index);
  295. manage_response(false, false);
  296. if (recover) {
  297. // LCD_MESSAGEPGM(MSG_MMU2_EJECT_RECOVER);
  298. Sound_MakeSound(e_SOUND_TYPE_StandardPrompt);
  299. //@@TODO wait_for_user = true;
  300. //#if ENABLED(HOST_PROMPT_SUPPORT)
  301. // host_prompt_do(PROMPT_USER_CONTINUE, PSTR("MMU2 Eject Recover"), PSTR("Continue"));
  302. //#endif
  303. //#if ENABLED(EXTENSIBLE_UI)
  304. // ExtUI::onUserConfirmRequired_P(PSTR("MMU2 Eject Recover"));
  305. //#endif
  306. //@@TODO while (wait_for_user) idle(true);
  307. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  308. // logic.Command(); //@@TODO command(MMU_CMD_R0);
  309. manage_response(false, false);
  310. }
  311. // no active tool
  312. extruder = MMU2_NO_TOOL;
  313. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  314. // disable_E0();
  315. return true;
  316. }
  317. void MMU2::Button(uint8_t index){
  318. logic.Button(index);
  319. }
  320. void MMU2::Home(uint8_t mode){
  321. logic.Home(mode);
  322. }
  323. void MMU2::SaveAndPark(bool move_axes, bool turn_off_nozzle) {
  324. if (!mmu_print_saved) { // First occurrence. Save current position, park print head, disable nozzle heater.
  325. LogEchoEvent("Saving and parking");
  326. st_synchronize();
  327. mmu_print_saved = true;
  328. resume_hotend_temp = degTargetHotend(active_extruder);
  329. if (move_axes){
  330. // save current pos
  331. for(uint8_t i = 0; i < 3; ++i){
  332. resume_position.xyz[i] = current_position[i];
  333. }
  334. // lift Z
  335. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  336. if (current_position[Z_AXIS] > Z_MAX_POS)
  337. current_position[Z_AXIS] = Z_MAX_POS;
  338. plan_buffer_line_curposXYZE(NOZZLE_PARK_Z_FEEDRATE);
  339. st_synchronize();
  340. // move XY aside
  341. current_position[X_AXIS] = X_PAUSE_POS;
  342. current_position[Y_AXIS] = Y_PAUSE_POS;
  343. plan_buffer_line_curposXYZE(NOZZLE_PARK_XY_FEEDRATE);
  344. st_synchronize();
  345. }
  346. if (turn_off_nozzle){
  347. LogEchoEvent("Heater off");
  348. setAllTargetHotends(0);
  349. }
  350. }
  351. // keep the motors powered forever (until some other strategy is chosen)
  352. // @@TODO do we need that in 8bit?
  353. // gcode.reset_stepper_timeout();
  354. }
  355. void MMU2::ResumeAndUnPark(bool move_axes, bool turn_off_nozzle) {
  356. if (mmu_print_saved) {
  357. LogEchoEvent("Resuming print");
  358. if (turn_off_nozzle && resume_hotend_temp) {
  359. MMU2_ECHO_MSG("Restoring hotend temperature ");
  360. SERIAL_ECHOLN(resume_hotend_temp);
  361. setTargetHotend(resume_hotend_temp, active_extruder);
  362. waitForHotendTargetTemp(3000, []{
  363. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature...")); // better report the event and let the GUI do its work somewhere else
  364. });
  365. LogEchoEvent("Hotend temperature reached");
  366. }
  367. if (move_axes) {
  368. LogEchoEvent("Resuming XYZ");
  369. current_position[X_AXIS] = resume_position.xyz[X_AXIS];
  370. current_position[Y_AXIS] = resume_position.xyz[Y_AXIS];
  371. plan_buffer_line_curposXYZE(NOZZLE_PARK_XY_FEEDRATE);
  372. st_synchronize();
  373. current_position[Z_AXIS] = resume_position.xyz[Z_AXIS];
  374. plan_buffer_line_curposXYZE(NOZZLE_PARK_Z_FEEDRATE);
  375. st_synchronize();
  376. } else {
  377. LogEchoEvent("NOT resuming XYZ");
  378. }
  379. }
  380. }
  381. void MMU2::CheckUserInput(){
  382. auto btn = ButtonPressed((uint16_t)lastErrorCode);
  383. switch (btn) {
  384. case Left:
  385. case Middle:
  386. case Right:
  387. Button(btn);
  388. break;
  389. case RestartMMU:
  390. Reset(CutThePower);
  391. break;
  392. case StopPrint:
  393. // @@TODO not sure if we shall handle this high level operation at this spot
  394. break;
  395. default:
  396. break;
  397. }
  398. }
  399. /// Originally, this was used to wait for response and deal with timeout if necessary.
  400. /// The new protocol implementation enables much nicer and intense reporting, so this method will boil down
  401. /// just to verify the result of an issued command (which was basically the original idea)
  402. ///
  403. /// It is closely related to mmu_loop() (which corresponds to our ProtocolLogic::Step()), which does NOT perform any blocking wait for a command to finish.
  404. /// But - in case of an error, the command is not yet finished, but we must react accordingly - move the printhead elsewhere, stop heating, eat a cat or so.
  405. /// That's what's being done here...
  406. void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
  407. mmu_print_saved = false;
  408. KEEPALIVE_STATE(PAUSED_FOR_USER);
  409. for (;;) {
  410. // in our new implementation, we know the exact state of the MMU at any moment, we do not have to wait for a timeout
  411. // So in this case we shall decide if the operation is:
  412. // - still running -> wait normally in idle()
  413. // - failed -> then do the safety moves on the printer like before
  414. // - finished ok -> proceed with reading other commands
  415. manage_heater();
  416. manage_inactivity(true); // calls LogicStep() and remembers its return status
  417. lcd_update(0);
  418. switch (logicStepLastStatus) {
  419. case Finished:
  420. // command/operation completed, let Marlin continue its work
  421. // the E may have some more moves to finish - wait for them
  422. st_synchronize();
  423. return;
  424. case VersionMismatch: // this basically means the MMU will be disabled until reconnected
  425. return;
  426. case CommunicationTimeout:
  427. case CommandError:
  428. case ProtocolError:
  429. SaveAndPark(move_axes, turn_off_nozzle); // and wait for the user to resolve the problem
  430. CheckUserInput();
  431. break;
  432. case CommunicationRecovered: // @@TODO communication recovered and may be an error recovered as well
  433. // may be the logic layer can detect the change of state a respond with one "Recovered" to be handled here
  434. ResumeAndUnPark(move_axes, turn_off_nozzle);
  435. break;
  436. case Processing: // wait for the MMU to respond
  437. default:
  438. break;
  439. }
  440. }
  441. }
  442. StepStatus MMU2::LogicStep() {
  443. StepStatus ss = logic.Step();
  444. switch (ss) {
  445. case Finished:
  446. case Processing:
  447. OnMMUProgressMsg(logic.Progress());
  448. break;
  449. case CommandError:
  450. ReportError(logic.Error());
  451. break;
  452. case CommunicationTimeout:
  453. state = xState::Connecting;
  454. ReportError(ErrorCode::MMU_NOT_RESPONDING);
  455. break;
  456. case ProtocolError:
  457. state = xState::Connecting;
  458. ReportError(ErrorCode::PROTOCOL_ERROR);
  459. break;
  460. case VersionMismatch:
  461. StopKeepPowered();
  462. ReportError(ErrorCode::VERSION_MISMATCH);
  463. break;
  464. default:
  465. break;
  466. }
  467. if( logic.Running() ){
  468. state = xState::Active;
  469. }
  470. return ss;
  471. }
  472. void MMU2::filament_ramming() {
  473. execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step));
  474. }
  475. void MMU2::execute_extruder_sequence(const E_Step *sequence, uint8_t steps) {
  476. st_synchronize();
  477. const E_Step *step = sequence;
  478. for (uint8_t i = 0; i < steps; i++) {
  479. current_position[E_AXIS] += pgm_read_float(&(step->extrude));
  480. plan_buffer_line_curposXYZE(pgm_read_float(&(step->feedRate)));
  481. st_synchronize();
  482. step++;
  483. }
  484. }
  485. void MMU2::SetActiveExtruder(uint8_t ex){
  486. active_extruder = ex;
  487. }
  488. void MMU2::ReportError(ErrorCode ec) {
  489. // Due to a potential lossy error reporting layers linked to this hook
  490. // we'd better report everything to make sure especially the error states
  491. // do not get lost.
  492. // - The good news here is the fact, that the MMU reports the errors repeatedly until resolved.
  493. // - The bad news is, that MMU not responding may repeatedly occur on printers not having the MMU at all.
  494. //
  495. // Not sure how to properly handle this situation, options:
  496. // - skip reporting "MMU not responding" (at least for now)
  497. // - report only changes of states (we can miss an error message)
  498. // - may be some combination of MMUAvailable + UseMMU flags and decide based on their state
  499. // Right now the filtering of MMU_NOT_RESPONDING is done in ReportErrorHook() as it is not a problem if mmu2.cpp
  500. ReportErrorHook((CommandInProgress)logic.CommandInProgress(), (uint16_t)ec);
  501. if( ec != lastErrorCode ){ // deduplicate: only report changes in error codes into the log
  502. lastErrorCode = ec;
  503. SERIAL_ECHO_START;
  504. SERIAL_ECHOLNRPGM( PrusaErrorTitle(PrusaErrorCodeIndex((uint16_t)ec)) );
  505. }
  506. static_assert(mmu2Magic[0] == 'M'
  507. && mmu2Magic[1] == 'M'
  508. && mmu2Magic[2] == 'U'
  509. && mmu2Magic[3] == '2'
  510. && mmu2Magic[4] == ':'
  511. && strlen_constexpr(mmu2Magic) == 5,
  512. "MMU2 logging prefix mismatch, must be updated at various spots"
  513. );
  514. }
  515. void MMU2::ReportProgress(ProgressCode pc) {
  516. ReportProgressHook((CommandInProgress)logic.CommandInProgress(), (uint16_t)pc);
  517. // Log progress - example: MMU2:P=123 EngageIdler
  518. char msg[64];
  519. int len = snprintf(msg, sizeof(msg), "MMU2:P=%hu ", (uint16_t)pc);
  520. // Append a human readable form of the progress code
  521. TranslateProgress((uint16_t)pc, &msg[len], 64 - len);
  522. SERIAL_ECHO_START;
  523. SERIAL_ECHOLN(msg);
  524. }
  525. void MMU2::OnMMUProgressMsg(ProgressCode pc){
  526. if( pc != lastProgressCode){
  527. ReportProgress(pc);
  528. lastProgressCode = pc;
  529. // Act accordingly - one-time handling
  530. switch(pc){
  531. case ProgressCode::FeedingToBondtech:
  532. // prepare for the movement of the E-motor
  533. st_synchronize();
  534. loadFilamentStarted = true;
  535. break;
  536. case ProgressCode::FeedingToNozzle:
  537. // Nothing yet
  538. break;
  539. default:
  540. // do nothing yet
  541. break;
  542. }
  543. } else {
  544. // Act accordingly - every status change (even the same state)
  545. switch(pc){
  546. case ProgressCode::FeedingToBondtech:
  547. if ( loadFilamentStarted )
  548. {
  549. switch ( WhereIsFilament() )
  550. {
  551. case FilamentState::AT_FSENSOR:
  552. // fsensor triggered, stop moving the extruder
  553. loadFilamentStarted = false;
  554. // TODO: continue to ProgressCode::FeedingToNozzle?
  555. break;
  556. case FilamentState::NOT_PRESENT:
  557. // fsensor not triggered, continue moving extruder
  558. // TODO: Verify what's the best speed here?
  559. current_position[E_AXIS] += MMU2_LOAD_TO_NOZZLE_FEED_RATE;
  560. plan_buffer_line_curposXYZE(MMU2_LOAD_TO_NOZZLE_FEED_RATE);
  561. st_synchronize(); // Wait for the steps to be done, otherwise the moves will just add up
  562. default:
  563. // Abort here?
  564. break;
  565. }
  566. }
  567. break;
  568. case ProgressCode::FeedingToNozzle:
  569. // Nothing yet
  570. break;
  571. default:
  572. // do nothing yet
  573. break;
  574. }
  575. }
  576. }
  577. void MMU2::LogErrorEvent(const char *msg){
  578. MMU2_ERROR_MSG(msg);
  579. SERIAL_ECHOLN();
  580. }
  581. void MMU2::LogEchoEvent(const char *msg){
  582. MMU2_ECHO_MSG(msg);
  583. SERIAL_ECHOLN();
  584. }
  585. } // namespace MMU2