mmu2.h 11 KB

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