mmu2.cpp 23 KB

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