mmu2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 previous active filament slot index (0-4) or 0xff in case of no active tool at boot-up
  117. inline uint8_t get_previous_tool() const { return previous_extruder; };
  118. /// @returns The filament slot index (0 to 4) that will be loaded next, 0xff in case of no active tool change
  119. uint8_t get_tool_change_tool() const;
  120. bool set_filament_type(uint8_t index, uint8_t type);
  121. /// Issue a "button" click into the MMU - to be used from Error screens of the MMU
  122. /// to select one of the 3 possible options to resolve the issue
  123. void Button(uint8_t index);
  124. /// Issue an explicit "homing" command into the MMU
  125. void Home(uint8_t mode);
  126. /// @returns current state of FINDA (true=filament present, false=filament not present)
  127. inline bool FindaDetectsFilament()const { return logic.FindaPressed(); }
  128. inline uint16_t TotalFailStatistics()const { return logic.FailStatistics(); }
  129. /// @returns Current error code
  130. inline ErrorCode MMUCurrentErrorCode() const { return logic.Error(); }
  131. /// @returns the version of the connected MMU FW.
  132. /// In the future we'll return the trully detected FW version
  133. Version GetMMUFWVersion()const {
  134. if( State() == xState::Active ){
  135. return { logic.MmuFwVersionMajor(), logic.MmuFwVersionMinor(), logic.MmuFwVersionRevision() };
  136. } else {
  137. return { 0, 0, 0};
  138. }
  139. }
  140. // Helper variable to monitor knob in MMU error screen in blocking functions e.g. manage_response
  141. bool is_mmu_error_monitor_active;
  142. /// Method to read-only mmu_print_saved
  143. bool MMU_PRINT_SAVED() const { return mmu_print_saved != SavedState::None; }
  144. /// Automagically "press" a Retry button if we have any retry attempts left
  145. bool RetryIfPossible(uint16_t ec);
  146. /// Decrement the retry attempts, if in a retry.
  147. // Called by the MMU protocol when a sent button is acknowledged.
  148. void DecrementRetryAttempts();
  149. private:
  150. /// Reset the retryAttempts back to the default value
  151. void ResetRetryAttempts();
  152. /// Perform software self-reset of the MMU (sends an X0 command)
  153. void ResetX0();
  154. /// Trigger reset pin of the MMU
  155. void TriggerResetPin();
  156. /// Perform power cycle of the MMU (cold boot)
  157. /// Please note this is a blocking operation (sleeps for some time inside while doing the power cycle)
  158. void PowerCycle();
  159. /// Stop the communication, but keep the MMU powered on (for scenarios with incorrect FW version)
  160. void StopKeepPowered();
  161. /// Along with the mmu_loop method, this loops until a response from the MMU is received and acts upon.
  162. /// In case of an error, it parks the print head and turns off nozzle heating
  163. void manage_response(const bool move_axes, const bool turn_off_nozzle);
  164. /// Performs one step of the protocol logic state machine
  165. /// and reports progress and errors if needed to attached ExtUIs.
  166. /// Updates the global state of MMU (Active/Connecting/Stopped) at runtime, see @ref State
  167. StepStatus LogicStep();
  168. void filament_ramming();
  169. void execute_extruder_sequence(const E_Step *sequence, uint8_t steps);
  170. /// Reports an error into attached ExtUIs
  171. /// @param ec error code, see ErrorCode
  172. /// @param res reporter error source, is either Printer (0) or MMU (1)
  173. void ReportError(ErrorCode ec, uint8_t res);
  174. /// Reports progress of operations into attached ExtUIs
  175. /// @param pc progress code, see ProgressCode
  176. void ReportProgress(ProgressCode pc);
  177. /// Responds to a change of MMU's progress
  178. /// - plans additional steps, e.g. starts the E-motor after fsensor trigger
  179. void OnMMUProgressMsg(ProgressCode pc);
  180. /// Progress code changed - act accordingly
  181. void OnMMUProgressMsgChanged(ProgressCode pc);
  182. /// Repeated calls when progress code remains the same
  183. void OnMMUProgressMsgSame(ProgressCode pc);
  184. /// Save print and park the print head
  185. void SaveAndPark(bool move_axes, bool turn_off_nozzle);
  186. /// Resume hotend temperature, if it was cooled. Safe to call if we aren't saved.
  187. void ResumeHotendTemp();
  188. /// Resume position, if the extruder was parked. Safe to all if state was not saved.
  189. void ResumeUnpark();
  190. /// Check for any button/user input coming from the printer's UI
  191. void CheckUserInput();
  192. /// Entry check of all external commands.
  193. /// It can wait until the MMU becomes ready.
  194. /// Optionally, it can also emit/display an error screen and the user can decide what to do next.
  195. /// @returns false if the MMU is not ready to perform the command (for whatever reason)
  196. bool WaitForMMUReady();
  197. ProtocolLogic logic; ///< implementation of the protocol logic layer
  198. uint8_t extruder; ///< currently active slot in the MMU ... somewhat... not sure where to get it from yet
  199. uint8_t previous_extruder; ///< last active slot in the MMU, useful for M600
  200. uint8_t tool_change_extruder; ///< only used for UI purposes
  201. xyz_pos_t resume_position;
  202. int16_t resume_hotend_temp;
  203. ProgressCode lastProgressCode = ProgressCode::OK;
  204. ErrorCode lastErrorCode = ErrorCode::MMU_NOT_RESPONDING;
  205. Buttons lastButton = Buttons::NoButton;
  206. StepStatus logicStepLastStatus;
  207. enum xState state;
  208. uint8_t mmu_print_saved;
  209. bool loadFilamentStarted;
  210. bool unloadFilamentStarted;
  211. friend struct LoadingToNozzleRAII;
  212. /// true in case we are doing the LoadToNozzle operation - that means the filament shall be loaded all the way down to the nozzle
  213. /// unlike the mid-print ToolChange commands, which only load the first ~30mm and then the G-code takes over.
  214. bool loadingToNozzle;
  215. uint8_t retryAttempts;
  216. bool inAutoRetry;
  217. };
  218. /// following Marlin's way of doing stuff - one and only instance of MMU implementation in the code base
  219. /// + avoiding buggy singletons on the AVR platform
  220. extern MMU2 mmu2;
  221. } // namespace MMU2