mmu2.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. CooldownPending = 4,
  55. };
  56. /// Source of operation error
  57. enum ReportErrorSource: uint8_t {
  58. ErrorSourcePrinter = 0,
  59. ErrorSourceMMU = 1,
  60. };
  61. /// Perform a reset of the MMU
  62. /// @param level physical form of the reset
  63. void Reset(ResetForm level);
  64. /// Power off the MMU (cut the power)
  65. void PowerOff();
  66. /// Power on the MMU
  67. void PowerOn();
  68. /// The main loop of MMU processing.
  69. /// Doesn't loop (block) inside, performs just one step of logic state machines.
  70. /// Also, internally it prevents recursive entries.
  71. void mmu_loop();
  72. /// The main MMU command - select a different slot
  73. /// @param index of the slot to be selected
  74. /// @returns false if the operation cannot be performed (Stopped)
  75. bool tool_change(uint8_t index);
  76. /// Handling of special Tx, Tc, T? commands
  77. bool tool_change(char code, uint8_t slot);
  78. /// Unload of filament in collaboration with the MMU.
  79. /// That includes rotating the printer's extruder in order to release filament.
  80. /// @returns false if the operation cannot be performed (Stopped or cold extruder)
  81. bool unload();
  82. /// Load (insert) filament just into the MMU (not into printer's nozzle)
  83. /// @returns false if the operation cannot be performed (Stopped)
  84. bool load_filament(uint8_t index);
  85. /// Load (push) filament from the MMU into the printer's nozzle
  86. /// @returns false if the operation cannot be performed (Stopped or cold extruder)
  87. bool load_filament_to_nozzle(uint8_t index);
  88. /// Move MMU's selector aside and push the selected filament forward.
  89. /// Usable for improving filament's tip or pulling the remaining piece of filament out completely.
  90. bool eject_filament(uint8_t index, bool recover);
  91. /// Issue a Cut command into the MMU
  92. /// Requires unloaded filament from the printer (obviously)
  93. /// @returns false if the operation cannot be performed (Stopped)
  94. bool cut_filament(uint8_t index);
  95. /// Issue a Try-Load command
  96. /// It behaves very similarly like a ToolChange, but it doesn't load the filament
  97. /// all the way down to the nozzle. The sole purpose of this operation
  98. /// is to check, that the filament will be ready for printing.
  99. bool load_to_bondtech(uint8_t index);
  100. /// @returns the active filament slot index (0-4) or 0xff in case of no active tool
  101. uint8_t get_current_tool() const;
  102. /// @returns the previous active filament slot index (0-4) or 0xff in case of no active tool at boot-up
  103. inline uint8_t get_previous_tool() const { return previous_extruder; };
  104. /// @returns The filament slot index (0 to 4) that will be loaded next, 0xff in case of no active tool change
  105. uint8_t get_tool_change_tool() const;
  106. bool set_filament_type(uint8_t index, uint8_t type);
  107. /// Issue a "button" click into the MMU - to be used from Error screens of the MMU
  108. /// to select one of the 3 possible options to resolve the issue
  109. void Button(uint8_t index);
  110. /// Issue an explicit "homing" command into the MMU
  111. void Home(uint8_t mode);
  112. /// @returns current state of FINDA (true=filament present, false=filament not present)
  113. inline bool FindaDetectsFilament()const { return logic.FindaPressed(); }
  114. /// @returns Current error code
  115. inline ErrorCode MMUCurrentErrorCode() const { return logic.Error(); }
  116. /// @returns the version of the connected MMU FW.
  117. /// In the future we'll return the trully detected FW version
  118. Version GetMMUFWVersion()const {
  119. if( State() == xState::Active ){
  120. return { logic.MmuFwVersionMajor(), logic.MmuFwVersionMinor(), logic.MmuFwVersionBuild() };
  121. } else {
  122. return { 0, 0, 0};
  123. }
  124. }
  125. // Helper variable to monitor knob in MMU error screen in blocking functions e.g. manage_response
  126. bool is_mmu_error_monitor_active;
  127. /// Method to read-only mmu_print_saved
  128. bool MMU_PRINT_SAVED() const { return mmu_print_saved != SavedState::None; }
  129. private:
  130. /// Perform software self-reset of the MMU (sends an X0 command)
  131. void ResetX0();
  132. /// Trigger reset pin of the MMU
  133. void TriggerResetPin();
  134. /// Perform power cycle of the MMU (cold boot)
  135. /// Please note this is a blocking operation (sleeps for some time inside while doing the power cycle)
  136. void PowerCycle();
  137. /// Stop the communication, but keep the MMU powered on (for scenarios with incorrect FW version)
  138. void StopKeepPowered();
  139. /// Along with the mmu_loop method, this loops until a response from the MMU is received and acts upon.
  140. /// In case of an error, it parks the print head and turns off nozzle heating
  141. void manage_response(const bool move_axes, const bool turn_off_nozzle);
  142. /// Performs one step of the protocol logic state machine
  143. /// and reports progress and errors if needed to attached ExtUIs.
  144. /// Updates the global state of MMU (Active/Connecting/Stopped) at runtime, see @ref State
  145. StepStatus LogicStep();
  146. void filament_ramming();
  147. void execute_extruder_sequence(const E_Step *sequence, uint8_t steps);
  148. void SetActiveExtruder(uint8_t ex);
  149. /// Reports an error into attached ExtUIs
  150. /// @param ec error code, see ErrorCode
  151. /// @param res reporter error source, is either Printer (0) or MMU (1)
  152. void ReportError(ErrorCode ec, uint8_t res);
  153. /// Reports progress of operations into attached ExtUIs
  154. /// @param pc progress code, see ProgressCode
  155. void ReportProgress(ProgressCode pc);
  156. /// Responds to a change of MMU's progress
  157. /// - plans additional steps, e.g. starts the E-motor after fsensor trigger
  158. void OnMMUProgressMsg(ProgressCode pc);
  159. /// Report the msg into the general logging subsystem (through Marlin's SERIAL_ECHO stuff)
  160. void LogErrorEvent(const char *msg);
  161. /// Report the msg into the general logging subsystem (through Marlin's SERIAL_ECHO stuff)
  162. void LogEchoEvent(const char *msg);
  163. /// Save print and park the print head
  164. void SaveAndPark(bool move_axes, bool turn_off_nozzle);
  165. /// Resume hotend temperature, if it was cooled. Safe to call if we aren't saved.
  166. void ResumeHotendTemp();
  167. /// Resume position, if the extruder was parked. Safe to all if state was not saved.
  168. void ResumeUnpark();
  169. /// Check for any button/user input coming from the printer's UI
  170. void CheckUserInput();
  171. /// Entry check of all external commands.
  172. /// It can wait until the MMU becomes ready.
  173. /// Optionally, it can also emit/display an error screen and the user can decide what to do next.
  174. /// @returns false if the MMU is not ready to perform the command (for whatever reason)
  175. bool WaitForMMUReady();
  176. ProtocolLogic logic; ///< implementation of the protocol logic layer
  177. int extruder; ///< currently active slot in the MMU ... somewhat... not sure where to get it from yet
  178. uint8_t previous_extruder; ///< last active slot in the MMU, useful for M600
  179. uint8_t tool_change_extruder; ///< only used for UI purposes
  180. xyz_pos_t resume_position;
  181. int16_t resume_hotend_temp;
  182. ProgressCode lastProgressCode = ProgressCode::OK;
  183. ErrorCode lastErrorCode = ErrorCode::MMU_NOT_RESPONDING;
  184. Buttons lastButton = Buttons::NoButton;
  185. StepStatus logicStepLastStatus;
  186. enum xState state;
  187. uint8_t mmu_print_saved;
  188. bool loadFilamentStarted;
  189. friend struct LoadingToNozzleRAII;
  190. /// true in case we are doing the LoadToNozzle operation - that means the filament shall be loaded all the way down to the nozzle
  191. /// unlike the mid-print ToolChange commands, which only load the first ~30mm and then the G-code takes over.
  192. bool loadingToNozzle;
  193. };
  194. /// following Marlin's way of doing stuff - one and only instance of MMU implementation in the code base
  195. /// + avoiding buggy singletons on the AVR platform
  196. extern MMU2 mmu2;
  197. } // namespace MMU2