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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. //#define MIXER_NORMALIZER_DEBUG
  25. #ifndef __AVR__ // || HAS_DUAL_MIXING
  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. , MIXER_DIRECT_SET_TOOL = NR_USER_VIRTUAL_TOOLS
  49. #if HAS_MIXER_SYNC_CHANNEL
  50. , MIXER_AUTORETRACT_TOOL
  51. #endif
  52. , NR_MIXING_VIRTUAL_TOOLS
  53. };
  54. #define MAX_VTOOLS TERN(HAS_MIXER_SYNC_CHANNEL, 254, 255)
  55. static_assert(NR_MIXING_VIRTUAL_TOOLS <= MAX_VTOOLS, "MIXING_VIRTUAL_TOOLS must be <= " STRINGIFY(MAX_VTOOLS) "!");
  56. #define MIXER_BLOCK_FIELD mixer_comp_t b_color[MIXING_STEPPERS]
  57. #define MIXER_POPULATE_BLOCK() mixer.populate_block(block->b_color)
  58. #define MIXER_STEPPER_SETUP() mixer.stepper_setup(current_block->b_color)
  59. #define MIXER_STEPPER_LOOP(VAR) for (uint_fast8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
  60. #if ENABLED(GRADIENT_MIX)
  61. typedef struct {
  62. bool enabled; // This gradient is enabled
  63. mixer_comp_t color[MIXING_STEPPERS]; // The current gradient color
  64. float start_z, end_z; // Region for gradient
  65. int8_t start_vtool, end_vtool; // Start and end virtual tools
  66. mixer_perc_t start_mix[MIXING_STEPPERS], // Start and end mixes from those tools
  67. end_mix[MIXING_STEPPERS];
  68. TERN_(GRADIENT_VTOOL, int8_t vtool_index); // Use this virtual tool number as index
  69. } gradient_t;
  70. #endif
  71. /**
  72. * @brief Mixer class
  73. * @details Contains data and behaviors for a Mixing Extruder
  74. */
  75. class Mixer {
  76. public:
  77. static float collector[MIXING_STEPPERS]; // M163 components, also editable from LCD
  78. static void init(); // Populate colors at boot time
  79. static void reset_vtools();
  80. static void refresh_collector(const float proportion=1.0, const uint8_t t=selected_vtool, float (&c)[MIXING_STEPPERS]=collector);
  81. // Used up to Planner level
  82. FORCE_INLINE static void set_collector(const uint8_t c, const float f) { collector[c] = _MAX(f, 0.0f); }
  83. static void normalize(const uint8_t tool_index);
  84. FORCE_INLINE static void normalize() { normalize(selected_vtool); }
  85. FORCE_INLINE static uint8_t get_current_vtool() { return selected_vtool; }
  86. FORCE_INLINE static void T(const uint_fast8_t c) {
  87. selected_vtool = c;
  88. TERN_(GRADIENT_VTOOL, refresh_gradient());
  89. TERN_(HAS_DUAL_MIXING, update_mix_from_vtool());
  90. }
  91. // Used when dealing with blocks
  92. FORCE_INLINE static void populate_block(mixer_comp_t b_color[MIXING_STEPPERS]) {
  93. #if ENABLED(GRADIENT_MIX)
  94. if (gradient.enabled) {
  95. MIXER_STEPPER_LOOP(i) b_color[i] = gradient.color[i];
  96. return;
  97. }
  98. #endif
  99. MIXER_STEPPER_LOOP(i) b_color[i] = color[selected_vtool][i];
  100. }
  101. FORCE_INLINE static void stepper_setup(mixer_comp_t b_color[MIXING_STEPPERS]) {
  102. MIXER_STEPPER_LOOP(i) s_color[i] = b_color[i];
  103. }
  104. #if EITHER(HAS_DUAL_MIXING, GRADIENT_MIX)
  105. static mixer_perc_t mix[MIXING_STEPPERS]; // Scratch array for the Mix in proportion to 100
  106. static inline void copy_mix_to_color(mixer_comp_t (&tcolor)[MIXING_STEPPERS]) {
  107. // Scale each component to the largest one in terms of COLOR_A_MASK
  108. // So the largest component will be COLOR_A_MASK and the other will be in proportion to it
  109. const float scale = (COLOR_A_MASK) * RECIPROCAL(_MAX(
  110. LIST_N(MIXING_STEPPERS, mix[0], mix[1], mix[2], mix[3], mix[4], mix[5])
  111. ));
  112. // Scale all values so their maximum is COLOR_A_MASK
  113. MIXER_STEPPER_LOOP(i) tcolor[i] = mix[i] * scale;
  114. #ifdef MIXER_NORMALIZER_DEBUG
  115. SERIAL_ECHOPGM("Mix [ ");
  116. SERIAL_ECHOLIST_N(MIXING_STEPPERS, mix[0], mix[1], mix[2], mix[3], mix[4], mix[5]);
  117. SERIAL_ECHOPGM(" ] to Color [ ");
  118. SERIAL_ECHOLIST_N(MIXING_STEPPERS, tcolor[0], tcolor[1], tcolor[2], tcolor[3], tcolor[4], tcolor[5]);
  119. SERIAL_ECHOLNPGM(" ]");
  120. #endif
  121. }
  122. static inline void update_mix_from_vtool(const uint8_t j=selected_vtool) {
  123. float ctot = 0;
  124. MIXER_STEPPER_LOOP(i) ctot += color[j][i];
  125. //MIXER_STEPPER_LOOP(i) mix[i] = 100.0f * color[j][i] / ctot;
  126. MIXER_STEPPER_LOOP(i) mix[i] = mixer_perc_t(100.0f * color[j][i] / ctot);
  127. #ifdef MIXER_NORMALIZER_DEBUG
  128. SERIAL_ECHOPAIR("V-tool ", j, " [ ");
  129. SERIAL_ECHOLIST_N(MIXING_STEPPERS, color[j][0], color[j][1], color[j][2], color[j][3], color[j][4], color[j][5]);
  130. SERIAL_ECHOPGM(" ] to Mix [ ");
  131. SERIAL_ECHOLIST_N(MIXING_STEPPERS, mix[0], mix[1], mix[2], mix[3], mix[4], mix[5]);
  132. SERIAL_ECHOLNPGM(" ]");
  133. #endif
  134. }
  135. #endif // HAS_DUAL_MIXING || GRADIENT_MIX
  136. #if HAS_DUAL_MIXING
  137. // Update the virtual tool from an edited mix
  138. static inline void update_vtool_from_mix() {
  139. copy_mix_to_color(color[selected_vtool]);
  140. TERN_(GRADIENT_MIX, refresh_gradient());
  141. // MIXER_STEPPER_LOOP(i) collector[i] = mix[i];
  142. // normalize();
  143. }
  144. #endif // HAS_DUAL_MIXING
  145. #if ENABLED(GRADIENT_MIX)
  146. static gradient_t gradient;
  147. static float prev_z;
  148. // Update the current mix from the gradient for a given Z
  149. static void update_gradient_for_z(const float z);
  150. static void update_gradient_for_planner_z();
  151. static inline void gradient_control(const float z) {
  152. if (gradient.enabled) {
  153. if (z >= gradient.end_z)
  154. T(gradient.end_vtool);
  155. else
  156. update_gradient_for_z(z);
  157. }
  158. }
  159. static inline void update_mix_from_gradient() {
  160. float ctot = 0;
  161. MIXER_STEPPER_LOOP(i) ctot += gradient.color[i];
  162. MIXER_STEPPER_LOOP(i) mix[i] = (mixer_perc_t)CEIL(100.0f * gradient.color[i] / ctot);
  163. #ifdef MIXER_NORMALIZER_DEBUG
  164. SERIAL_ECHOPGM("Gradient [ ");
  165. SERIAL_ECHOLIST_N(MIXING_STEPPERS, gradient.color[0], gradient.color[1], gradient.color[2], gradient.color[3], gradient.color[4], gradient.color[5]);
  166. SERIAL_ECHOPGM(" ] to Mix [ ");
  167. SERIAL_ECHOLIST_N(MIXING_STEPPERS, mix[0], mix[1], mix[2], mix[3], mix[4], mix[5]);
  168. SERIAL_ECHOLNPGM(" ]");
  169. #endif
  170. }
  171. // Refresh the gradient after a change
  172. static void refresh_gradient() {
  173. #if ENABLED(GRADIENT_VTOOL)
  174. const bool is_grd = (gradient.vtool_index == -1 || selected_vtool == (uint8_t)gradient.vtool_index);
  175. #else
  176. constexpr bool is_grd = true;
  177. #endif
  178. gradient.enabled = is_grd && gradient.start_vtool != gradient.end_vtool && gradient.start_z < gradient.end_z;
  179. if (gradient.enabled) {
  180. mixer_perc_t mix_bak[MIXING_STEPPERS];
  181. COPY(mix_bak, mix);
  182. update_mix_from_vtool(gradient.start_vtool);
  183. COPY(gradient.start_mix, mix);
  184. update_mix_from_vtool(gradient.end_vtool);
  185. COPY(gradient.end_mix, mix);
  186. update_gradient_for_planner_z();
  187. COPY(mix, mix_bak);
  188. prev_z = -1;
  189. }
  190. }
  191. #endif // GRADIENT_MIX
  192. // Used in Stepper
  193. FORCE_INLINE static uint8_t get_stepper() { return runner; }
  194. FORCE_INLINE static uint8_t get_next_stepper() {
  195. for (;;) {
  196. if (--runner < 0) runner = MIXING_STEPPERS - 1;
  197. accu[runner] += s_color[runner];
  198. if (
  199. #ifdef MIXER_ACCU_SIGNED
  200. accu[runner] < 0
  201. #else
  202. accu[runner] & COLOR_A_MASK
  203. #endif
  204. ) {
  205. accu[runner] &= COLOR_MASK;
  206. return runner;
  207. }
  208. }
  209. }
  210. private:
  211. // Used up to Planner level
  212. static uint_fast8_t selected_vtool;
  213. static mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  214. // Used in Stepper
  215. static int_fast8_t runner;
  216. static mixer_comp_t s_color[MIXING_STEPPERS];
  217. static mixer_accu_t accu[MIXING_STEPPERS];
  218. };
  219. extern Mixer mixer;