mmu2.h 9.8 KB

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