mmu2.cpp 22 KB

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