My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mixing.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. //#define MIXER_NORMALIZER_DEBUG
  25. #if !defined(__AVR__) // || DUAL_MIXING_EXTRUDER
  26. // Use 16-bit (or fastest) data for the integer mix factors
  27. typedef uint_fast16_t mixer_comp_t;
  28. typedef uint_fast16_t mixer_accu_t;
  29. #define COLOR_A_MASK 0x8000
  30. #define COLOR_MASK 0x7FFF
  31. #else
  32. // Use 8-bit data for the integer mix factors
  33. // Exactness is sacrificed for speed
  34. #define MIXER_ACCU_SIGNED
  35. typedef uint8_t mixer_comp_t;
  36. typedef int8_t mixer_accu_t;
  37. #define COLOR_A_MASK 0x80
  38. #define COLOR_MASK 0x7F
  39. #endif
  40. typedef int8_t mixer_perc_t;
  41. #ifndef MIXING_VIRTUAL_TOOLS
  42. #define MIXING_VIRTUAL_TOOLS 1
  43. #endif
  44. enum MixTool {
  45. FIRST_USER_VIRTUAL_TOOL = 0,
  46. LAST_USER_VIRTUAL_TOOL = MIXING_VIRTUAL_TOOLS - 1,
  47. NR_USER_VIRTUAL_TOOLS,
  48. #ifdef RETRACT_SYNC_MIXING
  49. MIXER_AUTORETRACT_TOOL = NR_USER_VIRTUAL_TOOLS,
  50. NR_MIXING_VIRTUAL_TOOLS
  51. #else
  52. NR_MIXING_VIRTUAL_TOOLS = NR_USER_VIRTUAL_TOOLS
  53. #endif
  54. };
  55. #ifdef RETRACT_SYNC_MIXING
  56. static_assert(NR_MIXING_VIRTUAL_TOOLS <= 254, "MIXING_VIRTUAL_TOOLS must be <= 254!");
  57. #else
  58. static_assert(NR_MIXING_VIRTUAL_TOOLS <= 255, "MIXING_VIRTUAL_TOOLS must be <= 255!");
  59. #endif
  60. #define MIXER_STEPPER_LOOP(VAR) \
  61. for (uint_fast8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
  62. #define MIXER_BLOCK_FIELD mixer_comp_t b_color[MIXING_STEPPERS]
  63. #define MIXER_POPULATE_BLOCK() mixer.populate_block(block->b_color)
  64. #define MIXER_STEPPER_SETUP() mixer.stepper_setup(current_block->b_color)
  65. #if ENABLED(GRADIENT_MIX)
  66. typedef struct {
  67. bool enabled; // This gradient is enabled
  68. mixer_comp_t color[MIXING_STEPPERS]; // The current gradient color
  69. float start_z, end_z; // Region for gradient
  70. int8_t start_vtool, end_vtool; // Start and end virtual tools
  71. mixer_perc_t start_mix[MIXING_STEPPERS], // Start and end mixes from those tools
  72. end_mix[MIXING_STEPPERS];
  73. #if ENABLED(GRADIENT_VTOOL)
  74. int8_t vtool_index; // Use this virtual tool number as index
  75. #endif
  76. } gradient_t;
  77. #endif
  78. /**
  79. * @brief Mixer class
  80. * @details Contains data and behaviors for a Mixing Extruder
  81. */
  82. class Mixer {
  83. public:
  84. static float collector[MIXING_STEPPERS]; // M163 components, also editable from LCD
  85. static void init(); // Populate colors at boot time
  86. static void reset_vtools();
  87. static void refresh_collector(const float proportion=1.0, const uint8_t t=selected_vtool);
  88. // Used up to Planner level
  89. FORCE_INLINE static void set_collector(const uint8_t c, const float f) { collector[c] = MAX(f, 0.0f); }
  90. static void normalize(const uint8_t tool_index);
  91. FORCE_INLINE static void normalize() { normalize(selected_vtool); }
  92. FORCE_INLINE static uint8_t get_current_vtool() { return selected_vtool; }
  93. FORCE_INLINE static void T(const uint_fast8_t c) {
  94. selected_vtool = c;
  95. #if ENABLED(GRADIENT_VTOOL)
  96. refresh_gradient();
  97. #endif
  98. #if DUAL_MIXING_EXTRUDER
  99. update_mix_from_vtool();
  100. #endif
  101. }
  102. // Used when dealing with blocks
  103. FORCE_INLINE static void populate_block(mixer_comp_t b_color[MIXING_STEPPERS]) {
  104. #if ENABLED(GRADIENT_MIX)
  105. if (gradient.enabled) {
  106. MIXER_STEPPER_LOOP(i) b_color[i] = gradient.color[i];
  107. return;
  108. }
  109. #endif
  110. MIXER_STEPPER_LOOP(i) b_color[i] = color[selected_vtool][i];
  111. }
  112. FORCE_INLINE static void stepper_setup(mixer_comp_t b_color[MIXING_STEPPERS]) {
  113. MIXER_STEPPER_LOOP(i) s_color[i] = b_color[i];
  114. }
  115. #if DUAL_MIXING_EXTRUDER || ENABLED(GRADIENT_MIX)
  116. static mixer_perc_t mix[MIXING_STEPPERS]; // Scratch array for the Mix in proportion to 100
  117. static inline void copy_mix_to_color(mixer_comp_t (&tcolor)[MIXING_STEPPERS]) {
  118. // Scale each component to the largest one in terms of COLOR_A_MASK
  119. // So the largest component will be COLOR_A_MASK and the other will be in proportion to it
  120. const float scale = (COLOR_A_MASK) * RECIPROCAL(float(MAX(mix[0], mix[1])));
  121. // Scale all values so their maximum is COLOR_A_MASK
  122. MIXER_STEPPER_LOOP(i) tcolor[i] = mix[i] * scale;
  123. #ifdef MIXER_NORMALIZER_DEBUG
  124. SERIAL_ECHOLNPAIR("Mix [", int(mix[0]), ", ", int(mix[1]), "] to Color [", int(tcolor[0]), ", ", int(tcolor[1]), "]");
  125. #endif
  126. }
  127. static inline void update_mix_from_vtool(const uint8_t j=selected_vtool) {
  128. float ctot = 0;
  129. MIXER_STEPPER_LOOP(i) ctot += color[j][i];
  130. //MIXER_STEPPER_LOOP(i) mix[i] = 100.0f * color[j][i] / ctot;
  131. mix[0] = mixer_perc_t(100.0f * color[j][0] / ctot);
  132. mix[1] = 100 - mix[0];
  133. #ifdef MIXER_NORMALIZER_DEBUG
  134. SERIAL_ECHOLNPAIR("V-tool ", int(j), " [", int(color[j][0]), ", ", int(color[j][1]), "] to Mix [", int(mix[0]), ", ", int(mix[1]), "]");
  135. #endif
  136. }
  137. #endif // DUAL_MIXING_EXTRUDER || GRADIENT_MIX
  138. #if DUAL_MIXING_EXTRUDER
  139. // Update the virtual tool from an edited mix
  140. static inline void update_vtool_from_mix() {
  141. copy_mix_to_color(color[selected_vtool]);
  142. #if ENABLED(GRADIENT_MIX)
  143. refresh_gradient();
  144. #endif
  145. // MIXER_STEPPER_LOOP(i) collector[i] = mix[i];
  146. // normalize();
  147. }
  148. #endif // DUAL_MIXING_EXTRUDER
  149. #if ENABLED(GRADIENT_MIX)
  150. static gradient_t gradient;
  151. static float prev_z;
  152. // Update the current mix from the gradient for a given Z
  153. static void update_gradient_for_z(const float z);
  154. static void update_gradient_for_planner_z();
  155. static inline void gradient_control(const float z) {
  156. if (gradient.enabled) {
  157. if (z >= gradient.end_z)
  158. T(gradient.end_vtool);
  159. else
  160. update_gradient_for_z(z);
  161. }
  162. }
  163. static inline void update_mix_from_gradient() {
  164. float ctot = 0;
  165. MIXER_STEPPER_LOOP(i) ctot += gradient.color[i];
  166. mix[0] = (mixer_perc_t)CEIL(100.0f * gradient.color[0] / ctot);
  167. mix[1] = 100 - mix[0];
  168. #ifdef MIXER_NORMALIZER_DEBUG
  169. SERIAL_ECHOLNPAIR("Gradient [", int(gradient.color[0]), ", ", int(gradient.color[1]), "] to Mix [", int(mix[0]), ", ", int(mix[1]), "]");
  170. #endif
  171. }
  172. // Refresh the gradient after a change
  173. static void refresh_gradient() {
  174. #if ENABLED(GRADIENT_VTOOL)
  175. const bool is_grd = (selected_vtool == gradient.vtool_index || gradient.vtool_index == -1);
  176. #else
  177. constexpr bool is_grd = true;
  178. #endif
  179. gradient.enabled = is_grd && gradient.start_vtool != gradient.end_vtool && gradient.start_z < gradient.end_z;
  180. if (gradient.enabled) {
  181. mixer_perc_t mix_bak[MIXING_STEPPERS];
  182. COPY(mix_bak, mix);
  183. update_mix_from_vtool(gradient.start_vtool);
  184. COPY(gradient.start_mix, mix);
  185. update_mix_from_vtool(gradient.end_vtool);
  186. COPY(gradient.end_mix, mix);
  187. update_gradient_for_planner_z();
  188. COPY(mix, mix_bak);
  189. prev_z = -1;
  190. }
  191. }
  192. #endif // GRADIENT_MIX
  193. // Used in Stepper
  194. FORCE_INLINE static uint8_t get_stepper() { return runner; }
  195. FORCE_INLINE static uint8_t get_next_stepper() {
  196. for (;;) {
  197. if (--runner < 0) runner = MIXING_STEPPERS - 1;
  198. accu[runner] += s_color[runner];
  199. if (
  200. #ifdef MIXER_ACCU_SIGNED
  201. accu[runner] < 0
  202. #else
  203. accu[runner] & COLOR_A_MASK
  204. #endif
  205. ) {
  206. accu[runner] &= COLOR_MASK;
  207. return runner;
  208. }
  209. }
  210. }
  211. private:
  212. // Used up to Planner level
  213. static uint_fast8_t selected_vtool;
  214. static mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  215. // Used in Stepper
  216. static int_fast8_t runner;
  217. static mixer_comp_t s_color[MIXING_STEPPERS];
  218. static mixer_accu_t accu[MIXING_STEPPERS];
  219. };
  220. extern Mixer mixer;