mmu2.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. mmu2Serial.begin(MMU_BAUD);
  86. PowerOn();
  87. mmu2Serial.flush(); // make sure the UART buffer is clear before starting communication
  88. extruder = MMU2_NO_TOOL;
  89. state = xState::Connecting;
  90. // start the communication
  91. logic.Start();
  92. }
  93. void MMU2::Stop() {
  94. StopKeepPowered();
  95. PowerOff();
  96. }
  97. void MMU2::StopKeepPowered(){
  98. state = xState::Stopped;
  99. logic.Stop();
  100. mmu2Serial.close();
  101. }
  102. void MMU2::Reset(ResetForm level){
  103. switch (level) {
  104. case Software: ResetX0(); break;
  105. case ResetPin: TriggerResetPin(); break;
  106. case CutThePower: PowerCycle(); break;
  107. default: break;
  108. }
  109. }
  110. void MMU2::ResetX0() {
  111. logic.ResetMMU(); // Send soft reset
  112. }
  113. void MMU2::TriggerResetPin(){
  114. reset();
  115. }
  116. void MMU2::PowerCycle(){
  117. // cut the power to the MMU and after a while restore it
  118. // Sadly, MK3/S/+ cannot do this
  119. PowerOff();
  120. delay_keep_alive(1000);
  121. PowerOn();
  122. }
  123. void MMU2::PowerOff(){
  124. power_off();
  125. }
  126. void MMU2::PowerOn(){
  127. power_on();
  128. }
  129. void MMU2::mmu_loop() {
  130. // We only leave this method if the current command was successfully completed - that's the Marlin's way of blocking operation
  131. // Atomic compare_exchange would have been the most appropriate solution here, but this gets called only in Marlin's task,
  132. // so thread safety should be kept
  133. static bool avoidRecursion = false;
  134. if (avoidRecursion)
  135. return;
  136. avoidRecursion = true;
  137. logicStepLastStatus = LogicStep(); // it looks like the mmu_loop doesn't need to be a blocking call
  138. avoidRecursion = false;
  139. }
  140. struct ReportingRAII {
  141. CommandInProgress cip;
  142. inline ReportingRAII(CommandInProgress cip):cip(cip){
  143. BeginReport(cip, (uint16_t)ProgressCode::EngagingIdler);
  144. }
  145. inline ~ReportingRAII(){
  146. EndReport(cip, (uint16_t)ProgressCode::OK);
  147. }
  148. };
  149. bool MMU2::WaitForMMUReady(){
  150. switch(State()){
  151. case xState::Stopped:
  152. return false;
  153. case xState::Connecting:
  154. // shall we wait until the MMU reconnects?
  155. // fire-up a fsm_dlg and show "MMU not responding"?
  156. default:
  157. return true;
  158. }
  159. }
  160. bool MMU2::tool_change(uint8_t index) {
  161. if( ! WaitForMMUReady())
  162. return false;
  163. if (index != extruder) {
  164. ReportingRAII rep(CommandInProgress::ToolChange);
  165. BlockRunoutRAII blockRunout;
  166. st_synchronize();
  167. logic.ToolChange(index); // let the MMU pull the filament out and push a new one in
  168. manage_response(false, false); // true, true);
  169. // reset current position to whatever the planner thinks it is
  170. plan_set_e_position(current_position[E_AXIS]);
  171. extruder = index; //filament change is finished
  172. SetActiveExtruder(0);
  173. // @@TODO really report onto the serial? May be for the Octoprint? Not important now
  174. // SERIAL_ECHO_START();
  175. // SERIAL_ECHOLNPAIR(MSG_ACTIVE_EXTRUDER, int(extruder));
  176. }
  177. return true;
  178. }
  179. /// Handle special T?/Tx/Tc commands
  180. ///
  181. ///- T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
  182. ///- 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.
  183. ///- Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
  184. bool MMU2::tool_change(char code, uint8_t slot) {
  185. if( ! WaitForMMUReady())
  186. return false;
  187. BlockRunoutRAII blockRunout;
  188. switch (code) {
  189. case '?': {
  190. waitForHotendTargetTemp(100, []{});
  191. load_filament_to_nozzle(slot);
  192. } break;
  193. case 'x': {
  194. st_synchronize();
  195. logic.ToolChange(slot);
  196. manage_response(false, false);
  197. extruder = slot;
  198. SetActiveExtruder(0);
  199. } break;
  200. case 'c': {
  201. waitForHotendTargetTemp(100, []{});
  202. execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, sizeof(load_to_nozzle_sequence) / sizeof (load_to_nozzle_sequence[0]));
  203. } break;
  204. }
  205. return true;
  206. }
  207. uint8_t MMU2::get_current_tool() const {
  208. return extruder == MMU2_NO_TOOL ? -1 : extruder;
  209. }
  210. bool MMU2::set_filament_type(uint8_t index, uint8_t type) {
  211. if( ! WaitForMMUReady())
  212. return false;
  213. // @@TODO - this is not supported in the new MMU yet
  214. // cmd_arg = filamentType;
  215. // command(MMU_CMD_F0 + index);
  216. manage_response(false, false); // true, true);
  217. return true;
  218. }
  219. bool MMU2::unload() {
  220. if( ! WaitForMMUReady())
  221. return false;
  222. WaitForHotendTargetTempBeep();
  223. {
  224. ReportingRAII rep(CommandInProgress::UnloadFilament);
  225. filament_ramming();
  226. logic.UnloadFilament();
  227. manage_response(false, false); // false, true);
  228. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  229. // no active tool
  230. extruder = MMU2_NO_TOOL;
  231. }
  232. return true;
  233. }
  234. bool MMU2::cut_filament(uint8_t index){
  235. if( ! WaitForMMUReady())
  236. return false;
  237. ReportingRAII rep(CommandInProgress::CutFilament);
  238. logic.CutFilament(index);
  239. manage_response(false, false); // false, true);
  240. return true;
  241. }
  242. bool MMU2::load_filament(uint8_t index) {
  243. if( ! WaitForMMUReady())
  244. return false;
  245. ReportingRAII rep(CommandInProgress::LoadFilament);
  246. logic.LoadFilament(index);
  247. manage_response(false, false);
  248. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  249. return true;
  250. }
  251. struct LoadingToNozzleRAII {
  252. MMU2 &mmu2;
  253. inline LoadingToNozzleRAII(MMU2 &mmu2):mmu2(mmu2){
  254. mmu2.loadingToNozzle = true;
  255. }
  256. inline ~LoadingToNozzleRAII(){
  257. mmu2.loadingToNozzle = false;
  258. }
  259. };
  260. bool MMU2::load_filament_to_nozzle(uint8_t index) {
  261. if( ! WaitForMMUReady())
  262. return false;
  263. LoadingToNozzleRAII ln(*this);
  264. WaitForHotendTargetTempBeep();
  265. {
  266. // used for MMU-menu operation "Load to Nozzle"
  267. ReportingRAII rep(CommandInProgress::ToolChange);
  268. BlockRunoutRAII blockRunout;
  269. if( extruder != MMU2_NO_TOOL ){ // we already have some filament loaded - free it + shape its tip properly
  270. filament_ramming();
  271. }
  272. logic.ToolChange(index);
  273. manage_response(false, false); // true, true);
  274. // reset current position to whatever the planner thinks it is
  275. plan_set_e_position(current_position[E_AXIS]);
  276. extruder = index;
  277. SetActiveExtruder(0);
  278. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  279. return true;
  280. }
  281. }
  282. bool MMU2::eject_filament(uint8_t index, bool recover) {
  283. if( ! WaitForMMUReady())
  284. return false;
  285. WaitForHotendTargetTempBeep();
  286. ReportingRAII rep(CommandInProgress::EjectFilament);
  287. current_position[E_AXIS] -= MMU2_FILAMENTCHANGE_EJECT_FEED;
  288. plan_buffer_line_curposXYZE(2500.F / 60.F);
  289. st_synchronize();
  290. logic.EjectFilament(index);
  291. manage_response(false, false);
  292. if (recover) {
  293. // LCD_MESSAGEPGM(MSG_MMU2_EJECT_RECOVER);
  294. Sound_MakeSound(e_SOUND_TYPE_StandardPrompt);
  295. //@@TODO wait_for_user = true;
  296. //#if ENABLED(HOST_PROMPT_SUPPORT)
  297. // host_prompt_do(PROMPT_USER_CONTINUE, PSTR("MMU2 Eject Recover"), PSTR("Continue"));
  298. //#endif
  299. //#if ENABLED(EXTENSIBLE_UI)
  300. // ExtUI::onUserConfirmRequired_P(PSTR("MMU2 Eject Recover"));
  301. //#endif
  302. //@@TODO while (wait_for_user) idle(true);
  303. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  304. // logic.Command(); //@@TODO command(MMU_CMD_R0);
  305. manage_response(false, false);
  306. }
  307. // no active tool
  308. extruder = MMU2_NO_TOOL;
  309. Sound_MakeSound(e_SOUND_TYPE_StandardConfirm);
  310. // disable_E0();
  311. return true;
  312. }
  313. void MMU2::Button(uint8_t index){
  314. logic.Button(index);
  315. }
  316. void MMU2::Home(uint8_t mode){
  317. logic.Home(mode);
  318. }
  319. void MMU2::SaveAndPark(bool move_axes, bool turn_off_nozzle) {
  320. if (!mmu_print_saved) { // First occurrence. Save current position, park print head, disable nozzle heater.
  321. LogEchoEvent("Saving and parking");
  322. st_synchronize();
  323. mmu_print_saved = true;
  324. resume_hotend_temp = degTargetHotend(active_extruder);
  325. if (move_axes){
  326. // save current pos
  327. for(uint8_t i = 0; i < 3; ++i){
  328. resume_position.xyz[i] = current_position[i];
  329. }
  330. // lift Z
  331. current_position[Z_AXIS] += Z_PAUSE_LIFT;
  332. if (current_position[Z_AXIS] > Z_MAX_POS)
  333. current_position[Z_AXIS] = Z_MAX_POS;
  334. plan_buffer_line_curposXYZE(NOZZLE_PARK_Z_FEEDRATE);
  335. st_synchronize();
  336. // move XY aside
  337. current_position[X_AXIS] = X_PAUSE_POS;
  338. current_position[Y_AXIS] = Y_PAUSE_POS;
  339. plan_buffer_line_curposXYZE(NOZZLE_PARK_XY_FEEDRATE);
  340. st_synchronize();
  341. }
  342. if (turn_off_nozzle){
  343. LogEchoEvent("Heater off");
  344. setAllTargetHotends(0);
  345. }
  346. }
  347. // keep the motors powered forever (until some other strategy is chosen)
  348. // @@TODO do we need that in 8bit?
  349. // gcode.reset_stepper_timeout();
  350. }
  351. void MMU2::ResumeAndUnPark(bool move_axes, bool turn_off_nozzle) {
  352. if (mmu_print_saved) {
  353. LogEchoEvent("Resuming print");
  354. if (turn_off_nozzle && resume_hotend_temp) {
  355. MMU2_ECHO_MSG("Restoring hotend temperature ");
  356. SERIAL_ECHOLN(resume_hotend_temp);
  357. setTargetHotend(resume_hotend_temp, active_extruder);
  358. waitForHotendTargetTemp(3000, []{
  359. lcd_display_message_fullscreen_P(_i("MMU OK. Resuming temperature...")); // better report the event and let the GUI do its work somewhere else
  360. });
  361. LogEchoEvent("Hotend temperature reached");
  362. }
  363. if (move_axes) {
  364. LogEchoEvent("Resuming XYZ");
  365. current_position[X_AXIS] = resume_position.xyz[X_AXIS];
  366. current_position[Y_AXIS] = resume_position.xyz[Y_AXIS];
  367. plan_buffer_line_curposXYZE(NOZZLE_PARK_XY_FEEDRATE);
  368. st_synchronize();
  369. current_position[Z_AXIS] = resume_position.xyz[Z_AXIS];
  370. plan_buffer_line_curposXYZE(NOZZLE_PARK_Z_FEEDRATE);
  371. st_synchronize();
  372. } else {
  373. LogEchoEvent("NOT resuming XYZ");
  374. }
  375. }
  376. }
  377. void MMU2::CheckUserInput(){
  378. auto btn = ButtonPressed((uint16_t)lastErrorCode);
  379. switch (btn) {
  380. case Left:
  381. case Middle:
  382. case Right:
  383. Button(btn);
  384. break;
  385. case RestartMMU:
  386. Reset(CutThePower);
  387. break;
  388. case StopPrint:
  389. // @@TODO not sure if we shall handle this high level operation at this spot
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. /// Originally, this was used to wait for response and deal with timeout if necessary.
  396. /// The new protocol implementation enables much nicer and intense reporting, so this method will boil down
  397. /// just to verify the result of an issued command (which was basically the original idea)
  398. ///
  399. /// 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.
  400. /// 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.
  401. /// That's what's being done here...
  402. void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
  403. mmu_print_saved = false;
  404. KEEPALIVE_STATE(PAUSED_FOR_USER);
  405. for (;;) {
  406. // in our new implementation, we know the exact state of the MMU at any moment, we do not have to wait for a timeout
  407. // So in this case we shall decide if the operation is:
  408. // - still running -> wait normally in idle()
  409. // - failed -> then do the safety moves on the printer like before
  410. // - finished ok -> proceed with reading other commands
  411. // @@TODO this needs verification - we need something which matches Marlin2's idle()
  412. manage_inactivity(true); // calls LogicStep() and remembers its return status
  413. switch (logicStepLastStatus) {
  414. case Finished:
  415. // command/operation completed, let Marlin continue its work
  416. // the E may have some more moves to finish - wait for them
  417. st_synchronize();
  418. return;
  419. case VersionMismatch: // this basically means the MMU will be disabled until reconnected
  420. return;
  421. case CommunicationTimeout:
  422. case CommandError:
  423. case ProtocolError:
  424. SaveAndPark(move_axes, turn_off_nozzle); // and wait for the user to resolve the problem
  425. CheckUserInput();
  426. break;
  427. case CommunicationRecovered: // @@TODO communication recovered and may be an error recovered as well
  428. // may be the logic layer can detect the change of state a respond with one "Recovered" to be handled here
  429. ResumeAndUnPark(move_axes, turn_off_nozzle);
  430. break;
  431. case Processing: // wait for the MMU to respond
  432. default:
  433. break;
  434. }
  435. }
  436. }
  437. StepStatus MMU2::LogicStep() {
  438. StepStatus ss = logic.Step();
  439. switch (ss) {
  440. case Finished:
  441. case Processing:
  442. OnMMUProgressMsg(logic.Progress());
  443. break;
  444. case CommandError:
  445. ReportError(logic.Error());
  446. break;
  447. case CommunicationTimeout:
  448. state = xState::Connecting;
  449. ReportError(ErrorCode::MMU_NOT_RESPONDING);
  450. break;
  451. case ProtocolError:
  452. state = xState::Connecting;
  453. ReportError(ErrorCode::PROTOCOL_ERROR);
  454. break;
  455. case VersionMismatch:
  456. StopKeepPowered();
  457. ReportError(ErrorCode::VERSION_MISMATCH);
  458. break;
  459. default:
  460. break;
  461. }
  462. if( logic.Running() ){
  463. state = xState::Active;
  464. }
  465. return ss;
  466. }
  467. void MMU2::filament_ramming() {
  468. execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step));
  469. }
  470. void MMU2::execute_extruder_sequence(const E_Step *sequence, uint8_t steps) {
  471. st_synchronize();
  472. const E_Step *step = sequence;
  473. for (uint8_t i = 0; i < steps; i++) {
  474. current_position[E_AXIS] += pgm_read_float(&(step->extrude));
  475. plan_buffer_line_curposXYZE(pgm_read_float(&(step->feedRate)));
  476. st_synchronize();
  477. step++;
  478. }
  479. }
  480. void MMU2::SetActiveExtruder(uint8_t ex){
  481. active_extruder = ex;
  482. }
  483. void MMU2::ReportError(ErrorCode ec) {
  484. // Due to a potential lossy error reporting layers linked to this hook
  485. // we'd better report everything to make sure especially the error states
  486. // do not get lost.
  487. // - The good news here is the fact, that the MMU reports the errors repeatedly until resolved.
  488. // - The bad news is, that MMU not responding may repeatedly occur on printers not having the MMU at all.
  489. //
  490. // Not sure how to properly handle this situation, options:
  491. // - skip reporting "MMU not responding" (at least for now)
  492. // - report only changes of states (we can miss an error message)
  493. // - may be some combination of MMUAvailable + UseMMU flags and decide based on their state
  494. // Right now the filtering of MMU_NOT_RESPONDING is done in ReportErrorHook() as it is not a problem if mmu2.cpp
  495. ReportErrorHook((CommandInProgress)logic.CommandInProgress(), (uint16_t)ec);
  496. if( ec != lastErrorCode ){ // deduplicate: only report changes in error codes into the log
  497. lastErrorCode = ec;
  498. // Log error format: MMU2:E=32766 TextDescription
  499. char msg[64];
  500. snprintf(msg, sizeof(msg), "MMU2:E=%hu", (uint16_t)ec);
  501. // Append a human readable form of the error code(s)
  502. TranslateErr((uint16_t)ec, msg, sizeof(msg));
  503. // beware - the prefix in the message ("MMU2") will get stripped by the logging subsystem
  504. // and a correct MMU2 component will be assigned accordingly - see appmain.cpp
  505. // Therefore I'm not calling MMU2_ERROR_MSG or MMU2_ECHO_MSG here
  506. SERIAL_ECHO_START;
  507. SERIAL_ECHOLN(msg);
  508. }
  509. static_assert(mmu2Magic[0] == 'M'
  510. && mmu2Magic[1] == 'M'
  511. && mmu2Magic[2] == 'U'
  512. && mmu2Magic[3] == '2'
  513. && mmu2Magic[4] == ':'
  514. && strlen_constexpr(mmu2Magic) == 5,
  515. "MMU2 logging prefix mismatch, must be updated at various spots"
  516. );
  517. }
  518. void MMU2::ReportProgress(ProgressCode pc) {
  519. ReportProgressHook((CommandInProgress)logic.CommandInProgress(), (uint16_t)pc);
  520. // Log progress - example: MMU2:P=123 EngageIdler
  521. char msg[64];
  522. snprintf(msg, sizeof(msg), "MMU2:P=%hu", (uint16_t)pc);
  523. // Append a human readable form of the progress code
  524. TranslateProgress((uint16_t)pc, msg, sizeof(msg));
  525. SERIAL_ECHO_START;
  526. SERIAL_ECHOLN(msg);
  527. }
  528. void MMU2::OnMMUProgressMsg(ProgressCode pc){
  529. if( pc != lastProgressCode){
  530. ReportProgress(pc);
  531. lastProgressCode = pc;
  532. // Act accordingly - one-time handling
  533. switch(pc){
  534. case ProgressCode::FeedingToBondtech:
  535. // prepare for the movement of the E-motor
  536. st_synchronize();
  537. loadFilamentStarted = true;
  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( WhereIsFilament() == FilamentState::AT_FSENSOR && loadFilamentStarted){// fsensor triggered, move the extruder to help loading
  548. // rotate the extruder motor - no planner sync, just add more moves - as long as they are roughly at the same speed as the MMU is pushing,
  549. // it really doesn't matter
  550. current_position[E_AXIS] += (loadingToNozzle ? MMU2_LOAD_TO_NOZZLE_LENGTH : MMU2_TOOL_CHANGE_LOAD_LENGTH) / extruder_multiplier[0];
  551. plan_buffer_line_curposXYZE(MMU2_LOAD_TO_NOZZLE_FEED_RATE);
  552. loadFilamentStarted = false;
  553. }
  554. break;
  555. default:
  556. // do nothing yet
  557. break;
  558. }
  559. }
  560. }
  561. void MMU2::LogErrorEvent(const char *msg){
  562. MMU2_ERROR_MSG(msg);
  563. SERIAL_ECHOLN();
  564. }
  565. void MMU2::LogEchoEvent(const char *msg){
  566. MMU2_ECHO_MSG(msg);
  567. SERIAL_ECHOLN();
  568. }
  569. } // namespace MMU2