mmu2.cpp 22 KB

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