mmu2.cpp 22 KB

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