mmu2.cpp 23 KB

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