mmu2.h 11 KB

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