mmu2.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /// @file
  2. #pragma once
  3. #include "mmu2_protocol_logic.h"
  4. struct E_Step;
  5. namespace MMU2 {
  6. struct xyz_pos_t {
  7. uint16_t xyz[3]; // @@TODO
  8. xyz_pos_t()=default;
  9. };
  10. // general MMU setup for MK3
  11. enum : uint8_t {
  12. FILAMENT_UNKNOWN = 0xffU
  13. };
  14. /// Top-level interface between Logic and Marlin.
  15. /// Intentionally named MMU2 to be (almost) a drop-in replacement for the previous implementation.
  16. /// Most of the public methods share the original naming convention as well.
  17. class MMU2 {
  18. public:
  19. MMU2();
  20. /// Powers ON the MMU, then initializes the UART and protocol logic
  21. void Start();
  22. /// Stops the protocol logic, closes the UART, powers OFF the MMU
  23. void Stop();
  24. /// States of a printer with the MMU:
  25. /// - Active
  26. /// - Connecting
  27. /// - Stopped
  28. ///
  29. /// When the printer's FW starts, the MMU2 mode is either Stopped or NotResponding (based on user's preference).
  30. /// When the MMU successfully establishes communication, the state changes to Active.
  31. enum class xState : uint_fast8_t {
  32. Active, ///< MMU has been detected, connected, communicates and is ready to be worked with.
  33. Connecting, ///< MMU is connected but it doesn't communicate (yet). The user wants the MMU, but it is not ready to be worked with.
  34. Stopped ///< The user doesn't want the printer to work with the MMU. The MMU itself is not powered and does not work at all.
  35. };
  36. inline xState State() const { return state; }
  37. // @@TODO temporary wrappers to make old gcc survive the code
  38. inline bool Enabled()const { return State() == xState::Active; }
  39. /// Different levels of resetting the MMU
  40. enum ResetForm : uint8_t {
  41. Software = 0, ///< sends a X0 command into the MMU, the MMU will watchdog-reset itself
  42. ResetPin = 1, ///< trigger the reset pin of the MMU
  43. CutThePower = 2 ///< power off and power on (that includes +5V and +24V power lines)
  44. };
  45. /// Perform a reset of the MMU
  46. /// @param level physical form of the reset
  47. void Reset(ResetForm level);
  48. /// Power off the MMU (cut the power)
  49. void PowerOff();
  50. /// Power on the MMU
  51. void PowerOn();
  52. /// The main loop of MMU processing.
  53. /// Doesn't loop (block) inside, performs just one step of logic state machines.
  54. /// Also, internally it prevents recursive entries.
  55. void mmu_loop();
  56. /// The main MMU command - select a different slot
  57. /// @param index of the slot to be selected
  58. /// @returns false if the operation cannot be performed (Stopped)
  59. bool tool_change(uint8_t index);
  60. /// Handling of special Tx, Tc, T? commands
  61. bool tool_change(const char *special);
  62. /// Unload of filament in collaboration with the MMU.
  63. /// That includes rotating the printer's extruder in order to release filament.
  64. /// @returns false if the operation cannot be performed (Stopped or cold extruder)
  65. bool unload();
  66. /// Load (insert) filament just into the MMU (not into printer's nozzle)
  67. /// @returns false if the operation cannot be performed (Stopped)
  68. bool load_filament(uint8_t index);
  69. /// Load (push) filament from the MMU into the printer's nozzle
  70. /// @returns false if the operation cannot be performed (Stopped or cold extruder)
  71. bool load_filament_to_nozzle(uint8_t index);
  72. /// Move MMU's selector aside and push the selected filament forward.
  73. /// Usable for improving filament's tip or pulling the remaining piece of filament out completely.
  74. bool eject_filament(uint8_t index, bool recover);
  75. /// Issue a Cut command into the MMU
  76. /// Requires unloaded filament from the printer (obviously)
  77. /// @returns false if the operation cannot be performed (Stopped)
  78. bool cut_filament(uint8_t index);
  79. /// @returns the active filament slot index (0-4) or 0xff in case of no active tool
  80. uint8_t get_current_tool();
  81. bool set_filament_type(uint8_t index, uint8_t type);
  82. /// Issue a "button" click into the MMU - to be used from Error screens of the MMU
  83. /// to select one of the 3 possible options to resolve the issue
  84. void Button(uint8_t index);
  85. /// Issue an explicit "homing" command into the MMU
  86. void Home(uint8_t mode);
  87. /// @returns current state of FINDA (true=filament present, false=filament not present)
  88. inline bool FindaDetectsFilament()const { return logic.FindaPressed(); }
  89. private:
  90. /// Perform software self-reset of the MMU (sends an X0 command)
  91. void ResetX0();
  92. /// Trigger reset pin of the MMU
  93. void TriggerResetPin();
  94. /// Perform power cycle of the MMU (cold boot)
  95. /// Please note this is a blocking operation (sleeps for some time inside while doing the power cycle)
  96. void PowerCycle();
  97. /// Stop the communication, but keep the MMU powered on (for scenarios with incorrect FW version)
  98. void StopKeepPowered();
  99. /// Along with the mmu_loop method, this loops until a response from the MMU is received and acts upon.
  100. /// In case of an error, it parks the print head and turns off nozzle heating
  101. void manage_response(const bool move_axes, const bool turn_off_nozzle);
  102. /// Performs one step of the protocol logic state machine
  103. /// and reports progress and errors if needed to attached ExtUIs.
  104. /// Updates the global state of MMU (Active/Connecting/Stopped) at runtime, see @ref State
  105. StepStatus LogicStep();
  106. void filament_ramming();
  107. void execute_extruder_sequence(const E_Step *sequence, int steps);
  108. void SetActiveExtruder(uint8_t ex);
  109. /// Reports an error into attached ExtUIs
  110. /// @param ec error code, see ErrorCode
  111. void ReportError(ErrorCode ec);
  112. /// Reports progress of operations into attached ExtUIs
  113. /// @param pc progress code, see ProgressCode
  114. void ReportProgress(ProgressCode pc);
  115. /// Responds to a change of MMU's progress
  116. /// - plans additional steps, e.g. starts the E-motor after fsensor trigger
  117. void OnMMUProgressMsg(ProgressCode pc);
  118. /// Report the msg into the general logging subsystem (through Marlin's SERIAL_ECHO stuff)
  119. void LogErrorEvent(const char *msg);
  120. /// Report the msg into the general logging subsystem (through Marlin's SERIAL_ECHO stuff)
  121. void LogEchoEvent(const char *msg);
  122. /// Save print and park the print head
  123. void SaveAndPark(bool move_axes, bool turn_off_nozzle);
  124. /// Resume print (unpark, turn on heating etc.)
  125. void ResumeAndUnPark(bool move_axes, bool turn_off_nozzle);
  126. /// Check for any button/user input coming from the printer's UI
  127. void CheckUserInput();
  128. /// Entry check of all external commands.
  129. /// It can wait until the MMU becomes ready.
  130. /// Optionally, it can also emit/display an error screen and the user can decide what to do next.
  131. /// @returns false if the MMU is not ready to perform the command (for whatever reason)
  132. bool WaitForMMUReady();
  133. ProtocolLogic logic; ///< implementation of the protocol logic layer
  134. int extruder; ///< currently active slot in the MMU ... somewhat... not sure where to get it from yet
  135. xyz_pos_t resume_position;
  136. int16_t resume_hotend_temp;
  137. ProgressCode lastProgressCode = ProgressCode::OK;
  138. ErrorCode lastErrorCode = ErrorCode::MMU_NOT_RESPONDING;
  139. StepStatus logicStepLastStatus;
  140. enum xState state;
  141. bool mmu_print_saved;
  142. bool loadFilamentStarted;
  143. friend struct LoadingToNozzleRAII;
  144. /// true in case we are doing the LoadToNozzle operation - that means the filament shall be loaded all the way down to the nozzle
  145. /// unlike the mid-print ToolChange commands, which only load the first ~30mm and then the G-code takes over.
  146. bool loadingToNozzle;
  147. };
  148. /// following Marlin's way of doing stuff - one and only instance of MMU implementation in the code base
  149. /// + avoiding buggy singletons on the AVR platform
  150. extern MMU2 mmu2;
  151. } // namespace MMU2